开发者

Best way to "declare" strings in C

I'm new to C world (coming from PHP). I'm playing with strings (I know that there is no such type of data).

My question is about which is the best way to "declare" strings ?

With some research I came to that.

char str[40] = "Here is my text";
char str[]   = "Here is my text";
char *str    = "Here is my text";
开发者_开发知识库


It depends on what your needs are.

char str[40] = "Here is my text";

This will allocate an array of 40 characters. First 15 characters will be set according to the specified string. The rest will be set to nulls. This is useful if you need to modify the string later on, but know that it will not exceed 40 characters (or 39 characters followed by a null terminator, depending on context).

char str[] = "Here is my text";

This is identical to the example above, except that str is now limited to holding 16 characters (15 for the string + a null terminator).

char *str = "Here is my text";

str is now a pointer to a string of 15 characters (plus a null terminator). You cannot modify the string itself, but you can make str point somewhere else. In some environments this is not enforced and you can actually modify the string contents, but it's not a good idea. If you do need to use a pointer and modify the string itself, you can copy it:

char *str = strdup("Here is my text");

But you need to free(str) or your code will leak memory.


char str[40] = "Here is my text";
char str[]   = "Here is my text";

str is modifiable. So,

str[0] = 'M'; 

But,

char *str    = "Here is my text";

str[0] = 'M'; // Not possible.

str pointing data resides on read only segment and is not modifiable. It all depends on what you want, whether the string needs to be modifiable or not.


I. char *str = "Here is my text";

a. The string will be created in read-only memory portion.

b. The str pointer variable will be created in stack portion of memory (assuming that all of these happens inside a function)

c. The str will point to the memory address of the string created in step 'a'

Best way to "declare" strings in C

So, you can't modify a string created this way since it gets created in read-only portion of memory. If you try to modify this string after it is created the operation is undefined and you will get a runtime error.

One way to overcome this problem is strdup. strdup will create a copy of the string in heap memory which allows you to modify the string freely.

For eg:

char *fruit = apple //This will be created in const portion of memory,can't modify
char *copy = strdup(fruit) //This will create a copy of the string pointed by fruit in heap

Remember to free the memory in heap: free(copy)

II. char str[40] = "Here is my text";

This will create an array of 40 characters. First 15 will be set based on the value given and the rest will be null. This will be created in stack memory and hence you can modify the value later on.

for eg.,

str[0] = 'T'
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜