if i declare size 5 array

char ch[5]={'1','2','3','4','5'};

Then the null will store at the address ch+5 ? even i didn't declare the size for the null to place.
If so mean it may overlap value on that memory address?

if i declare size 5 array

char ch[5]={'1','2','3','4','5'};

Then the null will store at the address ch+5 ? even i didn't declare the size for the null to place.
If so mean it may overlap value on that memory address?

No. The compiler will ATTEMPT to put a null value directly following your initialization. However, if you fill all the space in the array, it will not do so. However, if you were to do:

char ch[5] = {'1','2','3','4'};

Then it will place null in the 5th spot.

Moreover, if you do not specify a size... i.e.

char ch[] = {'1','2','3','4','5'};

It will allocate an extra space for you, making an array of 6 characters containing your 5 values and a null value at the end.

@Greywolf: that's wrong!

@murnesty: by NULL you really mean '\0'...right? that's actually called NUL (single L)...
What you've declared is just a simple character array and NOT a C-string. The compiler won't make that a C-string automatically. So if you really want to make it a C-string you'll have to declare it like this:

char ch[5] = {'1','2','3','4','\0'};

Or this will also do:

char ch[] = "hello";

This time the compiler will automatically convert it into a C-string by appending the NUL character at the end.
So internally your array will look like this:
{'h', 'e', 'l', 'l', 'o', '\0'}

Hope it helps...:)

Edited 5 Years Ago by NP-complete: fixed typos:)

@Greywolf: that's wrong!

@murnesty: by NULL you really mean '\0'...right? that's actually called NUL (single L)...
What you've declared is just a simple character array and NOT a C-string. The compiler won't make that a C-string automatically. So if you really want to make it a C-string you'll have to declare it like this:

char ch[5] = {'1','2','3','4','\0'};

Or this will also do:

char ch[] = "hello";

This time the compiler will automatically convert it into a C-string by appending the NUL character at the end.
So internally your array will look like this:
{'h', 'e', 'l', 'l', 'o', '\0'}

Hope it helps...:)

I don't mind being wrong--it happens to me a lot. But... where, and why?

@Greywolf: that's wrong!

@murnesty: by NULL you really mean '\0'...right? that's actually called NUL (single L)...
What you've declared is just a simple character array and NOT a C-string. The compiler won't make that a C-string automatically. So if you really want to make it a C-string you'll have to declare it like this:

char ch[5] = {'1','2','3','4','\0'};

Or this will also do:

char ch[] = "hello";

This time the compiler will automatically convert it into a C-string by appending the NUL character at the end.
So internally your array will look like this:
{'h', 'e', 'l', 'l', 'o', '\0'}

Hope it helps...:)

from :
http://en.wikipedia.org/wiki/Null_character
http://en.wikipedia.org/wiki/Null_string
http://en.wikipedia.org/wiki/Null_pointer#Null_pointer
http://www.cplusplus.com/reference/clibrary/cstring/NULL/
http://www.cplusplus.com/forum/beginner/5604/
they all spell NULL. and from http://en.wikipedia.org/wiki/Null_character it said "The null character (also null terminator), abbreviated NUL, is a control character with the value zero." by wikipedia. But still thanks for answering.

Ok. Actually I just want to use char array to store my data for rs232. From your answer mean there is no risk somehow compiler put the NULL or '/0' or NUL in the 5th location right?

from :
http://en.wikipedia.org/wiki/Null_character
http://en.wikipedia.org/wiki/Null_string
http://en.wikipedia.org/wiki/Null_pointer#Null_pointer
http://www.cplusplus.com/reference/clibrary/cstring/NULL/
http://www.cplusplus.com/forum/beginner/5604/
they all spell NULL. and from http://en.wikipedia.org/wiki/Null_character it said "The null character (also null terminator), abbreviated NUL, is a control character with the value zero." by wikipedia. But still thanks for answering.

Ok. Actually I just want to use char array to store my data for rs232. From your answer mean there is no risk somehow compiler put the NULL or '/0' or NUL in the 5th location right?

Nope, it won't overwrite anything with a null value. And yes, I can say null value (like I did in my original post) and I don't think it's wrong to do so hehe.

I didn't say he was wrong only to spell NUL as NULL...well, if you check the ASCII table, '\0' is called NUL and has been named so because it has no resemblance with the NULL pointer.

Having said that, where I think Greywolf333 was wrong is that the compiler doesn't automatically appends a NUL at the end of the char array, if it isn't explicitly mentioned. I think i mentioned(read, explained) this in my post.
For example,
if you declare something like this:

char ch[] = {'a', 'b', 'c'};

the compiler will NOT insert a NUL ('\0') at the end.

If you try any standard sting library functions such as strlen(), etc. you'll get (probably) wrong results.
Why probably? Well, in most cases you'll get an array overflow...and it'll invoke undefined behavior which will be left to your compiler to interpret and it will interpret it as it deems fit. So maybe, in some compiler, your code will "work" but under the hood there will remain a very subtle bug.

Moral of the story: Always declare your C-strings either like:

char str[] = "this is a C string, and the compiler knows it, because of the inverted commas, so puts a NUL character at the end";

or, like this:

char str[] = {'h', 'e', 'l', 'l', 'o', '\0'};

Note the NUL at the end.

Hope it helps...:)

Edited 5 Years Ago by NP-complete: added an example

if you check the ASCII table, '\0' is called NUL and has been named so because it has no resemblance with the NULL pointer.

This is a sticky area. Technically, '\0' has a numeric value of 0, which means it's valid as a null pointer constant, and thus equivalent to NULL. The difference is NULL may be cast as a pointer to void, which means the equivalency isn't mutual:

void *p = '\0'; /* Confusing, but valid */
char ch = NULL; /* Also confusing, but may not compile cleanly */

As far as naming, '\0' has been called NUL historically in an unofficial capacity, but as far as official terminology goes it's a representation for the "null character". NUL is mentioned in the standard only once in reference to the ASCII character set, and that reference is in a note unrelated to the null character. The correct name is "null character", but realistically you can call it whatever you want as long as the meaning is understood. ;)

Having said that, where I think Greywolf333 was wrong is that the compiler doesn't automatically appends a NUL at the end of the char array, if it isn't explicitly mentioned.

Correct. If you use an initializer list on an array of char, there's no implicit assumption that the array is being used as a string. That particular magic only happens if you initialize the array with a string literal:

char a[] = {'a','b','c'}; /* What you see is what you get, there's no extra hidden '\0' */
char b[] = "abc"; /* Equivalent to {'a','b','c','\0'} */

On a side note, using a string literal as an initializer is one of the three places in C--the so called "object" contexts--where an array is not converted to a pointer to its first element.

Thank you, Narue, for the clarifications...:)

In my original post, the NUL part was insignificant and i really wanted to highlight that C-string thing.

I wasn't too sure, because a few codes were "working", but somehow i felt it was wrong and hence asked for your confirmation...much thanks once again...:)

Edited 5 Years Ago by NP-complete: n/a

On a side note, using a string literal as an initializer is one of the three places in C--the so called "object" contexts--where an array is not converted to a pointer to its first element.

You mean if i declare

char array[] = "abc";

Then I can't like

cout<<*array<<*(array+1)<<*(array+2)<<endl;

something like this?

You mean if i declare

char array[] = "abc";

Then I can't like

cout<<*array<<*(array+1)<<*(array+2)<<endl;

something like this?

Yes you can, though I don't know why you wouldn't use the [] operator to access the elements. i.e. array[0] array[1] array[2]. The null character will be placed in array[3] so it will not affect your initialized values.

Thanks to Narue (lookin good as always) and NP for pointing out my mistake.

because Narue said "where an array is not converted to a pointer to its first element."..

because Narue said "where an array is not converted to a pointer to its first element."..

It was a side note only. The general rule is that when an array is used, it's converted to a pointer to the first element. That's why things such as the following are allowed:

int a[10];
int *p = a; // No need to explicitly say &a[0]
void foo(int *p);

int a[10];

foo(a); // Fine, a and p are compatible types here
int a[] = {1,2,3,4,5};

cout << *(a + 2) << '\n'; // Non-modifying pointer arithmetic is OK

These are all value contexts, you're interested in the elements rather than the array object as a whole. Most uses of arrays fall under value context, which is why you'll occasionally hear confused programmers say that arrays and pointers are the same. That's because most of the time they can be used as if they were the same. However, there are three object contexts, where you truly do want to work with the array object rather than its elements (cases where arrays and pointers are not the same):

  • As an operand to the address-of operator (&). You want a pointer to an array, not a pointer to a pointer to the first element:
    int a[10];
    int (*p)[10] = &a; // Correct
    int **p = &a; // Incorrect, incompatible types
  • As an operand to the sizeof operator. You want the size of the array in bytes, not the size of a pointer:
    char a[100];
    
    cout << sizeof a << '\n'; // Prints 100 instead of sizeof(char*)
  • As a string literal initializer:
    char a[] = "abcd"; // OK, "abcd" here does not prompt a pointer conversion

    You want to initialize the array with the contents of another array (the string literal). If the pointer conversion took place, you would be trying to assign a pointer to an array, which is not legal:

    char *p = "abcd";
    char a[] = p; // Erm, no. Incompatible types
Comments
useful info...:)
This question has already been answered. Start a new discussion instead.