char[4] C++ arrays, question

wantedSpidy

Senior member
Nov 16, 2006
557
0
0
Is this a segfault error?

char hex[4];
hex[4]='\0';

I mean if it was int hex[4], you could use hex[4]=0, or whatever.
I always thought you couldn't use the last byte of a character array, because the last byte was set to null, so why can't I do that myself in the program?

Comments?

Thanks
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Counters/indexes start at 0 - you are accessing the fifth byte.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
char hex[4];

This makes a char array named "hex" that is four characters long. The last MUST be a null to avoid buffer overruns.

strcpy(hex, "str");

That's as much as you can fit in it. Not sure if the last byte is automatically NULL after you declare it or not but perhaps someone else can clarify that. To my knowledge it is not necessarily automatically zero, so you'd do hex[3] = NULL; to terminate it (strcpy might do that anyway).

And yes hex[4] only has FOUR total elements: hex[0], [1], [2], and [3], there is no [4] as that would be accessing a nonexistent 5th element (and running into memory after that with no warning, BTW).
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
The last byte does not have to be NULL. It is convienent when using character strings.

All you have allocated is 4 consectutive bytes in memory. You need to ensure that you access them proplery.
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
Originally posted by: SoundTheSurrender
I thought you had to do something like hex + 3 = '\0';

to point to the last slot in the array.

I'm probably wrong.

If memory serves me correct, that should be: *(hex + 3) = '\0';

Originally posted by: wantedSpidy
Thanks.
I wasn't aware that the compiler wouldn't warn me!!

C++ doesn't warn you about such things. That's why it's not usually recommended for newer programmers... well, unless you have a nice IDE that will try to warn you of possibly erroneous code.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Depending on how you play with your memory design, C & C++ can allow you to exceed the assigned memory when using a pointer.

As pointed above *(hex+3) could point to another valid mamory address; but not give you the result that you are looking for.
 

Matthias99

Diamond Member
Oct 7, 2003
8,808
0
0
Originally posted by: Aikouka
Originally posted by: SoundTheSurrender
I thought you had to do something like hex + 3 = '\0';

to point to the last slot in the array.

I'm probably wrong.

If memory serves me correct, that should be: *(hex + 3) = '\0';

Yes, which is equivalent to " hex[3] = '\0' " (which would be the normal way of doing such an assignment). " (hex + 3) = '\0' " would be trying to assign a character value to a static pointer, which won't work (and should not compile).

Overrunning the array by trying to read/write hex[4] or *(hex+4) will give undefined behavior. Reading from there will probably give you garbage back, and going far enough past will cause a protection fault. Writing past the end of arrays allocated on the stack will frequently corrupt stack frames and cause a segmentation fault later when you try to return from the function call. At best you'll be overwriting some other stack variable and causing very strange bugs.

strcpy() copies the null terminator as well. String literals are implicitly null-terminated in C. So strcpy(hex, "str") will result in hex[0] holding 's', hex[1] holding 't', hex[2] holding 'r', and hex[3] having a '\0'.