"" notation, does it define strings?

S

Sortie

char a[5] = "abcde";

What happens here? Is the "abcde" treated as a string or is it
just used to initialize the character array? If it is treated as
a string, wouldn't it need an extra byte to store the null
termination character, which would mean that the compiler should
throw a warning about writing beyond the allocated memory?

I checked it and didn't get a warning, I also checked

char a[5] = "abcde";
if (a[5]=='\0') printf("TRUE"); /*TRUE*/

So it appears that the "abcde" was used as a string.
 
B

Ben Bacarisse

Sortie said:
char a[5] = "abcde";

What happens here? Is the "abcde" treated as a string or is it
just used to initialize the character array?

It's both. "abcde" is string literal -- no two way about it. It is
also used to initialise the array.
If it is treated as
a string, wouldn't it need an extra byte to store the null
termination character, which would mean that the compiler should
throw a warning about writing beyond the allocated memory?

No, because the language defines this as a special case with the
meaning that 'a' thought 'e' are used to initialise exactly a[0]
thought a[4]. You are correct that is it is odd, but it is defined
like this for largely historical reasons (look at strncpy for another
antique of the same vintage).

The result, in a, is not a string, just an array of characters.
I checked it and didn't get a warning, I also checked

char a[5] = "abcde";
if (a[5]=='\0') printf("TRUE"); /*TRUE*/

So it appears that the "abcde" was used as a string.

What you saw when you tried this experiment was just an unfortunate
accident. Looking at a[5] is "undefined behaviour" -- pretty much
anything can happen -- and you drew a short straw in that what
happened confirmed an in correct belief. If, as is entirely
possible, you'd seen a 'z' there or your program had stopped with a
"access violation" message, you'd have been forced to think again.

It is almost impossible to find out what the corner cases of C are by
trying it out. The language definition gives the implementation all
sorts of permission to do whatever it likes when your program steps
outside of what is strictly required, so you get very little
information from such experiments.
 
M

Mug

char a[5] = "abcde";

What happens here? Is the "abcde" treated as a string or is it
just used to initialize the character array? If it is treated as
a string, wouldn't it need an extra byte to store the null
termination character, which would mean that the compiler should
throw a warning about writing beyond the allocated memory?

I checked it and didn't get a warning, I also checked

char a[5] = "abcde";
if (a[5]=='\0') printf("TRUE"); /*TRUE*/

So it appears that the "abcde" was used as a string.

hi;
on my system it doesn't print TRUE, i'm using gcc on linux,
i know there are some diiferent behavious on windows, either with
visual studio or dev c++, they like to initialize all allocated memory
of program by 0,
that's why a[5] is '\0'(a[5] is index of bound,the max of a is 4, but
as u know C compiler doesn't check
those stuffs,so a[5] is points at an unknow memory zone which have
been unnaturally initilized with 0, or otherwise u are
luckly fall on a zero each time).
here is the program:

int main()
{
char a[5]="abcde";
printf("%s",a);
fflush(stdout);
if (a[5]=='\0') printf("TRUE"); /*TRUE*/
return 0;
}

the result:

zsh/3 4140 % gcc main.c
[Sun 09/08/30 03:30 UTC][pts/2][x86_64/linux-gnu/2.6.30-ARCH][4.3.10]
<mug@arch:~/test>
zsh/3 4141 % ./a.out
abcde��%

so u see, the printf stop once it encounter a '\0' in the unknow
memory zone,
 
H

Herbert Rosenau

luckly fall on a zero each time).
here is the program:


int main()
{
char a[5]="abcde";

Setz up the array a with 5 chars a[0] - a[4]
printf("%s",a);

is undefined behavior because a contains no string, so printf tries to
access the array outside its limits.
fflush(stdout);
if (a[5]=='\0') printf("TRUE"); /*TRUE*/

Once again undefined behavior as a[5] is outside the bound of a. a is
defined as an array of 5 chars, not 6.SET USERNAME=HERBERT

return 0;
}


the result:

zsh/3 4140 % gcc main.c
[Sun 09/08/30 03:30 UTC][pts/2][x86_64/linux-gnu/2.6.30-ARCH][4.3.10]
<mug@arch:~/test>
zsh/3 4141 % ./a.out
abcde%

so u see, the printf stop once it encounter a '\0' in the unknow
memory zone,
No, u sees nothing, by that: who is u?

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2R Deutsch ist da!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top