返回失败的字符串 [英] Return of a string that fails

查看:81
本文介绍了返回失败的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的作业是创建一个简单的函数,给定一个unsigned char,

应该返回它的位字符串表示。


我试图下拉一些代码(如下所示)但它不起作用。

编译成功,但print8bit()没有返回任何内容。


是想知道我在哪里误会。在此先感谢您的帮助。


/ *代码从这里开始* /


char * print8bit(unsigned char n)

{

char s [9]; / *分配一个8 + 1字符的数组* /

int i,j; / *计数器变量* /


for(j = 7,i = 0; j> = 0,i< = 7; j - ,i ++)

{

if(n&(1<< i))

s [j] =''1'';

否则

s [j] =''0'';

}


s [8] =''\'0' ; / *字符串结尾的标记* /


返回s; / *返回char数组的第一个地址? * /

}


int main(无效)

{


char * str = print8bit(128); / * str []现在应该是''1000000''* /


printf("%d:%s",128,str); / *在stdout上只打印''128:''* /


返回0;

}


/ *代码在这里结束* /

My homework is to make a simple function that, given a unsigned char,
should returns its bit string representation.

I tried to drop down some code (as follows) but it doesn''t work.
Compiling is successfully, but print8bit() doesn''t return anything.

Was wondering where I''m mistaking. Thanks in advance for your help.

/* code starts here */

char* print8bit (unsigned char n)
{
char s[9]; /* Allocate an array of 8+1 char */

int i, j; /* Counter variables */

for (j=7,i=0; j>=0,i<=7 ; j--,i++)
{
if(n & (1<<i))
s[j]=''1'';
else
s[j]=''0'';
}

s[8]=''\0''; /* Marker of end of string */

return s; /* Return the first address of char array? */
}

int main(void)
{

char* str = print8bit(128); /* str[] now should be ''1000000'' */

printf("%d: %s", 128, str); /* on stdout is printed only ''128:'' */

return 0;
}

/* code ends here */

推荐答案

5月20日下午1:52,nembo kid< nembo @ kidwrote:
On May 20, 1:52 pm, nembo kid <nembo@kidwrote:

我的作业是创建一个简单的函数,给定一个unsigned char,

应该返回它的位串表示。
My homework is to make a simple function that, given a unsigned char,
should returns its bit string representation.



HW问题,嗯......

HW questions, hmmmmm...


我试图删除一些代码(如下所示)但它不起作用。

编译成功,但print8bit()没有返回任何内容。


想知道我在哪里误。在此先感谢您的帮助。
I tried to drop down some code (as follows) but it doesn''t work.
Compiling is successfully, but print8bit() doesn''t return anything.

Was wondering where I''m mistaking. Thanks in advance for your help.



返回指向生命周期结束的对象的指针。这是

未定义的行为。

< snip code>

You return a pointer to an object whose lifetime is over. That''s
undefined behavior.
<snip code>


vi ****** @ gmail.com ha scritto:
vi******@gmail.com ha scritto:

你返回一个指向生命周期结束的对象的指针。这是'/ b $ b未定义的行为。
You return a pointer to an object whose lifetime is over. That''s
undefined behavior.



好​​的,发现..,我很傻。


因为分配了'[]''在堆栈上。但是如果我在

堆上分配s []我就不会有任何问题....似乎同样的情况发生在

链表(在堆上分配的。)


所以我要么使用限定符''static''来保持活着。从

退出函数或传递其地址作为参数(指向

a指针)。


两种解决方案都是相当于?


再次感谢。

Ok, found..,.that silly I am.

Because ''s[]'' is allocated on the stack. But if I''d allocate s[] on the
heap I shouldn''t have any issue....seems that the same happens with
linked list (that are allocated on the heap).

So either I''d use the qualificator ''static'' to "keep on alive" s after
exiting from the function or pass its address as parameter (a pointer to
a pointer).

Both solutions are equivalent?

Thanks again.


5月20日下午2:12,nembo kid< nembo @ kidwrote:
On May 20, 2:12 pm, nembo kid <nembo@kidwrote:

vipps ... @ gmail.com ha scritto:
vipps...@gmail.com ha scritto:

你返回一个指向一个对象的指针一生都结束了。这是'/ b $ b未定义的行为。
You return a pointer to an object whose lifetime is over. That''s
undefined behavior.



好​​的,发现..,我很傻。


因为分配了'[]''在堆栈上。但是如果我在

堆上分配s []我就不会有任何问题....似乎同样的情况发生在

链表(在堆上分配的。)


所以我要么使用限定符''static''来保持活着。从

退出函数或传递其地址作为参数(指向

a指针)。


两种解决方案都是当量?


Ok, found..,.that silly I am.

Because ''s[]'' is allocated on the stack. But if I''d allocate s[] on the
heap I shouldn''t have any issue....seems that the same happens with
linked list (that are allocated on the heap).

So either I''d use the qualificator ''static'' to "keep on alive" s after
exiting from the function or pass its address as parameter (a pointer to
a pointer).

Both solutions are equivalent?



不,第二个解决方案,假设无效f(unsigned char c,char ** s)

将不起作用,除非你指定*一个静态数组或来自

malloc / calloc / realloc的值。

这里是最好的解决方案:


void f(unsigned char c,char * s)


并写入s。您可以假设s至少是CHAR_BIT + 1长,

并且该函数的用户有责任确保它是。

No, the second solution, assuming void f(unsigned char c, char **s)
will not work unless you assign *s a static array or a value from
malloc/calloc/realloc.
Here''s the best solution:

void f(unsigned char c, char *s)

And write to s. You may assume that ''s'' is at least CHAR_BIT+1 long,
and the user of the function is responsible to make sure it is.

这篇关于返回失败的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆