开发者

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

#include <stdio.h>

#include <stdlib.h>
int mai开发者_JAVA百科n()
{
    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. So sprintf is trying to write data to who-knows-where.

You should use char test[8] instead, which allocates an array of 8 char, and then sprintf(test, "%d", num);.

UPDATE: If you want to use char * pointers, you should allocate space:

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 *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.

Also, as mentioned in the comments, if your compiler supports it you should use snprintf(). It's like sprintf but it takes an extra parameter which is the size of the buffer:

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.

Note: some will say this should be malloc(8 * sizeof(char)) (or 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.

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. I am one of these people, but in my opinion you will rarely need to upgrade a char * to another type. 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.


sprintf() does not allocate space for the string; 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. You must call malloc and store the result in test[0] before you can write to the memory pointed to by test[0]. You free it at the end.

A useful function, present in GNU and BSD, is asprintf, which will call malloc for you to allocate enough memory for the formatted string:

#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].)


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. Because user space memory access.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜