C:这个字符指针赋值有什么问题?

C:这个字符指针赋值有什么问题?,c,string,pointers,variable-assignment,character,C,String,Pointers,Variable Assignment,Character,一个新手问题: 我正在练习一个字符指针的作业,但发现没有打印出来。代码如下: #include <stdio.h> int main (void) { char * option_string = NULL; option_string = (char*)malloc(sizeof(5)); memset(option_string, 0, sizeof(char) * 5); int j; for ( j = 0; j < 5; j

一个新手问题:

我正在练习一个字符指针的作业,但发现没有打印出来。代码如下:

#include <stdio.h>

int main (void)
{
    char * option_string = NULL;
    option_string = (char*)malloc(sizeof(5));
    memset(option_string, 0, sizeof(char) * 5);

    int j;
    for ( j = 0; j < 5; j++)
    {
        *option_string++ = 'a';
    }

    printf("print options_string: %s\n", option_string); //!nothing was printed out!
    free(option_string);
    return 0; 
}
#包括
内部主(空)
{
char*选项_字符串=NULL;
选项_string=(char*)malloc(sizeof(5));
memset(选项_字符串,0,大小(字符)*5);
int j;
对于(j=0;j<5;j++)
{
*选项_string++='a';
}
printf(“打印选项\u字符串:%s\n”,选项\u字符串);/!未打印任何内容!
自由(选项_字符串);
返回0;
}

提前谢谢

增加指针
选项\u string
,使其指向字符串的末尾

试一试 malloc(6)

(j=0;j<5;j++)的

{
选项_字符串[j]=“a”;
}

相反。

您的
malloc(sizeof(5))
似乎是错误的。
5
的大小是多少?(提示:不是5。)

您的问题是,在循环中,您编写
*option\u string++
。这意味着循环完成后,您将指向字符串的末尾:

option_string at start
   |
   V
+----+----+----+----+----+
|    |    |    |    |    |
|    |    |    |    |    |
+----+----+----+----+----+
                             ^
                             |
                     option_string at end
请注意,这揭示了代码的第二个问题:C中的字符串以null结尾,但该字符串最终将包含“aaaaa”,然后…谁知道呢?很可能是垃圾,但你说不出来。你需要一根六长度的绳子。解决第一个问题意味着使用简单的索引:
选项\u字符串[j]=“a”
。如果你真的想使用
*option\u string++
方法,你必须保存并恢复
option\u string
字符*real\u option\u string=option\u string;…option\u string=real\u option\u string;
),但我不建议这样做。修复这两个bug,以及一些风格方面的问题,将为您提供:

#include <stdlib.h>
#include <stdio.h>

int main (void)
{
    char * option_string = calloc(6, sizeof(char));

    int j;
    for ( j = 0; j < 5; j++)
    {
        option_string[j] = 'a';
    }

    printf("print options_string: %s\n", option_string);
    free(option_string);
    return 0; 
}
#包括
#包括
内部主(空)
{
char*option_string=calloc(6,sizeof(char));
int j;
对于(j=0;j<5;j++)
{
选项_字符串[j]=“a”;
}
printf(“打印选项\u字符串:%s\n”,选项\u字符串);
自由(选项_字符串);
返回0;
}

我改变的另一件事是你的
malloc
用法。我觉得在这里,
calloc
是一个更好的风格选择
calloc(count,size)
分配大小为
size
count
对象,并将其归零。这就像
malloc(count*size)
加上一个memset,但对我来说感觉更干净。一般来说,您也不应该对
malloc
/
calloc
/等进行强制转换(它可能会掩盖有用的警告),并且您需要分配六个插槽,如我所说(因此您可以使用空终止符,这是零值字符,因此我们不需要显式设置它)。结合
选项\u string[j]
索引模式,为
calloc
添加缺少的
stdlib.h
(你也应该为
malloc
添加它),我们就可以开始了

我忘了指针改成指向垃圾了。谢谢是的,我再次尝试将初始化位置设置为tem值。太好了,谢谢!这里我添加了一个临时变量来存储选项字符串的初始化位置。在打印之前恢复。@tao:不客气!你使用这种方法有什么特别的原因吗?我认为这是两个选项中最糟糕的一个,尽管它肯定会起作用。很抱歉回复得太晚!没有特别的原因,我只是在练习指针。再次感谢@陶:没有,
5
的大小也不是6。5是一个整数,因此
sizeof(5)=sizeof(int)
。但是,为了保存长度为5的以null结尾的字符串,缓冲区需要有6个字节长,这与整数的大小无关。
#include <stdlib.h>
#include <stdio.h>

int main (void)
{
    char * option_string = calloc(6, sizeof(char));

    int j;
    for ( j = 0; j < 5; j++)
    {
        option_string[j] = 'a';
    }

    printf("print options_string: %s\n", option_string);
    free(option_string);
    return 0; 
}