char pointer initialization in C

2019-03-16 21:26发布

问题:

I am not so clear on character pointer and how they work.

The program builds, but crashes when I run it.

 char *ab = NULL;
 //ab = "abc123"; // works fine
 sprintf(ab, "abc%d", 123); // this line seems to crash the program

I don't get how this can be wrong, when sprintf takes in a (char * str) as a first argument.

Can anyone please explain this to me?

回答1:

You have allocated no memory to use with ab.

The first assignment works because you are assigning to ab a string constant: "abc123". Memory for constant strings are provided by the compiler on your behalf: you don't need to allocate this memory.

Before you can use ab with e.g. sprintf, you'll need to allocate some memory using malloc, and assign that space to ab:

ab = malloc(sizeof(char) * (NUM_CHARS + 1));

Then your sprintf will work so long as you've made enough space using malloc. Note: the + 1 is for the null terminator.

Alternately you can make some memory for ab by declaring it as an array:

char ab[NUM_CHARS + 1];

Without allocating memory somehow for ab, the sprintf call will try to write to NULL, which is undefined behavior; this is the cause of your crash.



回答2:

You need to allocate memory for your data. Indeed sprintf takes char*, but it doesn't allocate memory for you.

The first line works fine because compiler automatically allocates data for constant tables of chars defined at compile time.



回答3:

Unlike in Java or other higher level languages, many of the C library's string functions don't simply set a string reference, instead they operate on a block of pre-allocated memory called a character array.

Your first line is saying that ab points to a non-existent memory location.

You'd have more luck if, instead of char *ab = NULL; you did either:

char ab[12];

or:

char *ab = (char*)malloc(12);


回答4:

You can do this

char ab[10];  //allocate memory
sprintf(ab, "abc%d", 123);


回答5:

"ab" is null and sprintf is trying to write to it, you have to allocate it first.

char ab[20];

sprintf(ab, "abc%d", 123); //

or

char * ab = malloc(20); // new, whatever

sprintf(ab, "abc%d", 123); //


回答6:

There are several things to think about here. Your original example is below:

char *ab = NULL;
 //ab = "abc123"; // works fine
 sprintf(ab, "abc%d", 123); // this line seems to crash the program

char *ab = NULL; is a pointer to a character and is initialized to NULL;

I don't think ab = "abc123"; worked fine unless it looked like char *ab = "abc123";. That is because you initialized char *ab to a read-only string. The initialization probably took place at compile time.

Your sprintf(ab, "abc%d", 123); line failed, because you did not initialize any memory for the char *ab pointer ahead of time. In other words, you did not do something like:

ab = malloc((sizeof(char) * 3) + 1); /* + 1 allows for null string terminator. */

You can fix your problem one of two ways. Either allocate dynamic memory as shown above, or you can make the string an array of a fixed length, like char ab[25] = {0};. Usually, I create an array of a length like 1024, 256, or some number that will usually cover most of my string length cases. Then I use char pointers for functions that operate on the array.