mkeles84 wrote:
The other responses you've received have explained why 'name' needs to
contain a Null-Terminated Character String (NTCS). However, it
occurred to me that you might not be aware of why creating a NTCS is
normally not very difficult.
For instance, the second "MEHMET" in your program causes a piece of
memory to be allocated for an array of 7 chars, with the contents
{'M', 'E', 'H', 'M', 'E', 'T', '\0'}. "MEHMET" itself is replaced in
your code with the address of the first character of that array. As a
result, it is automatically null-terminated, without you having to say
so.
The same thing did NOT happen with the initialization of the 'name'
array, because you explicitly declared it as having a length of 6. As
a result, the definition is equivalent to:
char name[6] = {'M', 'E', 'H', 'M', 'E', 'T'};
Note that there is no terminating null character in that array. If,
instead, you had either explicity made it big enough to hold the
terminating null character:
char name[7] = "MEHMET";
or, more simply, just left the array length unspecified:
char name[] = "MEHMET";
your definition would have been equivalent to:
char name[7] = {'M', 'E', 'H', 'M', 'E', 'T', '\0'};
Cross-posted to comp.lang.c, followups redirected there, since
comp.lang.c is more appropriate for questions about how to use C.
I have a problem about NULL's.
for example, a variable is name :
--------------
char name[6]="MEHMET"; /* 6 characters */
if (strcmp(name,"M EHMET") == 0){
printf("true");
}else{
printf("false") ;
}
I think result must bt "true" but I saw "false" on screen.
how can I compare it. I don't want to use NULL characker after variable.
for example, a variable is name :
--------------
char name[6]="MEHMET"; /* 6 characters */
if (strcmp(name,"M EHMET") == 0){
printf("true");
}else{
printf("false") ;
}
I think result must bt "true" but I saw "false" on screen.
how can I compare it. I don't want to use NULL characker after variable.
contain a Null-Terminated Character String (NTCS). However, it
occurred to me that you might not be aware of why creating a NTCS is
normally not very difficult.
For instance, the second "MEHMET" in your program causes a piece of
memory to be allocated for an array of 7 chars, with the contents
{'M', 'E', 'H', 'M', 'E', 'T', '\0'}. "MEHMET" itself is replaced in
your code with the address of the first character of that array. As a
result, it is automatically null-terminated, without you having to say
so.
The same thing did NOT happen with the initialization of the 'name'
array, because you explicitly declared it as having a length of 6. As
a result, the definition is equivalent to:
char name[6] = {'M', 'E', 'H', 'M', 'E', 'T'};
Note that there is no terminating null character in that array. If,
instead, you had either explicity made it big enough to hold the
terminating null character:
char name[7] = "MEHMET";
or, more simply, just left the array length unspecified:
char name[] = "MEHMET";
your definition would have been equivalent to:
char name[7] = {'M', 'E', 'H', 'M', 'E', 'T', '\0'};
Cross-posted to comp.lang.c, followups redirected there, since
comp.lang.c is more appropriate for questions about how to use C.
Comment