简体   繁体   English

为什么我在这里遇到了段故障? 需要帮忙。 想要将整数放入char指针数组中

[英]WHY I got seg fault here? need help. Want to put integer into char pointer array

#include <stdio.h>

#include <stdlib.h>
int main()
{
    int num = 1;
    char* test[8];
    sprintf(test[0],"%d",num);
    printf("%s\n",test[0]);

}

char *test[8] is an array of 8 char * , or pointers to strings, and since you don't specify, they're all set to garbage values. char *test[8]是一个包含8个char *或字符串指针的数组,由于你没有指定,它们都被设置为垃圾值。 So sprintf is trying to write data to who-knows-where. 所以sprintf正在尝试将数据写入who-know-where。

You should use char test[8] instead, which allocates an array of 8 char , and then sprintf(test, "%d", num); 你应该使用char test[8]代替,它分配一个8 char的数组,然后是sprintf(test, "%d", num); .

UPDATE: If you want to use char * pointers, you should allocate space: 更新:如果你想使用char *指针,你应该分配空间:

char *test = malloc(8 /* see note below */);
sprintf(test, "%d", num);

If you want to use an array of char * pointers, it works the same: 如果你想使用一个char *指针数组,它的工作原理是一样的:

char *test[8]; // 8 pointers to strings
test[0] = malloc(8); // allocate memory for the first pointer
sprintf(test[0], "%d", num);

Keep in mind you would have to call malloc for each of test[0] through test[7] individually. 请记住,您必须分别为test[0]test[7]每一个调用malloc

Also, as mentioned in the comments, if your compiler supports it you should use snprintf() . 另外,如评论中所述,如果您的编译器支持它,您应该使用snprintf() It's like sprintf but it takes an extra parameter which is the size of the buffer: 它就像sprintf但它需要一个额外的参数,即缓冲区的大小:

snprintf(test, 8, "%d", num);

and guarantees not to use more space than you allow it. 并保证不要使用比你允许的空间更多的空间。 It's safer, and if you need to, snprintf returns the amount of space it actually wanted, so if you gave it too little room you can realloc and try again. 它更安全,如果你需要, snprintf返回它实际需要的空间量,所以如果你给它的空间太小,你可以重新realloc并再试一次。

Note: some will say this should be malloc(8 * sizeof(char)) (or sizeof *test ). 注意:有人会说这应该是malloc(8 * sizeof(char)) (或sizeof *test )。 They are wrong (in my objectively-correct opinion; note the sarcasm)! 他们是错的(在我客观正确的意见;注意讽刺)! sizeof(char) is guaranteed to be 1, so this multiplication is unnecessary. sizeof(char)保证为1,因此这种乘法是不必要的。

Some advocate the usage of TYPE *p = malloc(x * sizeof *p) so that if TYPE changes, you'll only need to change it in one place, and sizeof *p will adapt. 有人提倡使用TYPE *p = malloc(x * sizeof *p)这样如果TYPE发生变化,你只需要在一个地方改变它, sizeof *p就会适应。 I am one of these people, but in my opinion you will rarely need to upgrade a char * to another type. 我是其中一个人,但在我看来,你很少需要将char *升级到另一种类型。 Since so many functions use char * and would need to be changed in such an upgrade, I'm not worried about making malloc lines more flexible. 由于这么多函数使用char *并且需要在这样的升级中进行更改,所以我并不担心使malloc行更灵活。

sprintf() does not allocate space for the string; sprintf()不为字符串分配空间; you must do that yourself beforehand. 你必须事先自己做。

Look at your warnings: 看看你的警告:

test.c: In function 'main':
test.c:8: warning: 'test[0]' is used uninitialized in this function

You allocate an array of 8 pointers, but use one without initializing it. 您分配了一个包含8个指针的数组,但在没有初始化的情况下使用一个指针。 You must call malloc and store the result in test[0] before you can write to the memory pointed to by test[0] . 您必须调用malloc并将结果存储在test[0]然后才能写入test[0]指向的内存。 You free it at the end. 你最后free它。

A useful function, present in GNU and BSD, is asprintf , which will call malloc for you to allocate enough memory for the formatted string: 在GNU和BSD中存在的一个有用的函数是asprintf ,它将调用malloc为你为格式化的字符串分配足够的内存:

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

int main(void) {
    int num = 1;
    char* test[8];
    asprintf(&test[0],"%d",num);
    printf("%s\n",test[0]);
    free(test[0]);
    return 0;
}

(Note that you pass the address of your pointer to asprintf — since your pointer is test[0] , its address is &test[0] .) (注意,你将指针的地址传递给asprintf - 因为你的指针是test[0] ,它的地址是&test[0] 。)

You did allocate space but you you are passing the wrong thing. 你确实分配了空间但是你传递了错误的东西。 Try this: 尝试这个:

#include <stdio.h>

#include <stdlib.h>
int main()
{
    int num = 1;
    char test[8];
    sprintf(test,"%d",num);
    printf("%s\n",test);

}
int main()
{
       char *str[5];
       sprintf(str[0], "%d",55);
       printf("%s\n",str[0]);
       return 0;
}

This will be work. 这将是有效的。 But, if you specify variable instead of integer constant value show the segmentation fault will be occur. 但是,如果指定变量而不是整数常量值,则会发生分段错误。 This error will be happened at the time of sprintf function execution. 此错误将在sprintf函数执行时发生。 Because user space memory access. 因为用户空间内存访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM