You're supposed to set it before using it. That's the only rule you have to follow to avoid undefined behaviour. Whether you initialise it at creation time or assign to it just before using it is not relevant.
Personally speaking, I prefer to never have variables set to unknown values myself so I'll usually do the first one unless it's set in close proximity (within a few lines).
In fact, with C99, where you don't have to declare locals at the tops of blocks any more, I'll generally defer creating it until it's needed, at which point it can be initialised as well.
Note that variables are given default values under certain circumstances (for example, if they're static storage duration such as being declared at file level, outside any function).
Local variables do not have this guarantee. So, if your second declaration above (char *str;) is inside a function, it may have rubbish in it and attempting to use it will invoke the afore-mentioned, dreaded, undefined behaviour.
The relevant part of the C99 standard 6.7.8/10:
Answer from paxdiablo on Stack OverflowIf an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:
- if it has pointer type, it is initialized to a null pointer;
- if it has arithmetic type, it is initialized to (positive or unsigned) zero;
- if it is an aggregate, every member is initialized (recursively) according to these rules;
- if it is a union, the first named member is initialized (recursively) according to these rules.
Videos
What is the difference between declaration and initialization of strings in C?
How to initialize a string in C?
How to declare a string in C?
You're supposed to set it before using it. That's the only rule you have to follow to avoid undefined behaviour. Whether you initialise it at creation time or assign to it just before using it is not relevant.
Personally speaking, I prefer to never have variables set to unknown values myself so I'll usually do the first one unless it's set in close proximity (within a few lines).
In fact, with C99, where you don't have to declare locals at the tops of blocks any more, I'll generally defer creating it until it's needed, at which point it can be initialised as well.
Note that variables are given default values under certain circumstances (for example, if they're static storage duration such as being declared at file level, outside any function).
Local variables do not have this guarantee. So, if your second declaration above (char *str;) is inside a function, it may have rubbish in it and attempting to use it will invoke the afore-mentioned, dreaded, undefined behaviour.
The relevant part of the C99 standard 6.7.8/10:
If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:
- if it has pointer type, it is initialized to a null pointer;
- if it has arithmetic type, it is initialized to (positive or unsigned) zero;
- if it is an aggregate, every member is initialized (recursively) according to these rules;
- if it is a union, the first named member is initialized (recursively) according to these rules.
I'm wonder, what is the proper way of initializing a string?
Well, since the second snippet defines an uninitialized pointer to string, I'd say the first one. :)
In general, if you want to play it safe, it's good to initialize to NULL all pointers; in this way, it's easy to spot problems derived from uninitialized pointers, since dereferencing a NULL pointer will yield a crash (actually, as far as the standard is concerned, it's undefined behavior, but on every machine I've seen it's a crash).
However, you should not confuse a NULL pointer to string with an empty string: a NULL pointer to string means that that pointer points to nothing, while an empty string is a "real", zero-length string (i.e. it contains just a NUL character).
char * str=NULL; /* NULL pointer to string - there's no string, just a pointer */
const char * str2 = ""; /* Pointer to a constant empty string */
char str3[] = "random text to reach 15 characters ;)"; /* String allocated (presumably on the stack) that contains some text */
*str3 = 0; /* str3 is emptied by putting a NUL in first position */
Hello there friends
I am having a problem understanding why this code will work with a string initialized as char[] vs an string initialized as char*. In essence I am trying to reverse a string in place and has written this:
#include <stdio.h>
void revstr(char *str1, int strLen)
{
// declare variable
int i, len;
char temp;
for (i = 0; i < strLen/2; i++)
{
temp = str1[i];
str1[i] = str1[strLen - i - 1];
str1[strLen - i - 1] = temp;
}
}
int main()
{
char str[17] = "This is a string";
revstr(str,17);
printf (" After reversing the string: %s", str);
char* str2 = "Also a string";
revstr(str2,13);
printf (" After reversing the string: %s", str2);
}When fed the str, the function behaves nicely. When fed the str2, I get a segmentation and I cannot really understand why :/
Anyone care to explain?
It doesn't work because with the syntax:
ch[50]="manipulation";
You're assigning the string "manipulation" to the 50th element of ch. That's not possible because the array is composed of idividual characters, and you're assigning a string to a individual char. Also, ch has elements from 0 to 49, and there's not a 50th element.
If something's wrong with my explanation, please tell me. And sorry for my bad english.
ch[50] = "manipulation"isn't valid syntax. Closer would bech = "manipulation", but arrays aren't modifiable lvalues, so you can't assign to them. Usestrcpy(3), or declarechas a pointer instead:strcpy(ch, "manipulation");or
char *ch; ch = "manipulation";Your second example is an initialization, not an assignment expression. This form creates an array
chand copies the provided string literal to initialize it.