943,675 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 6289
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 1st, 2008
0

Null Char

Expand Post »
Hi,
When we define any integer array like
int a[3]={1,2,3};
then it will assign the values as a[0]=1, a[1]=2, and a[2]=3
But, when we define any character array as
char a[3]={'a','b'};
then compiler here automatically consider '\0' null character to represent the end of the string


So, my question here is:
As compiler do not needs to use any such terminator to indicate that the int array or float array is terminated. Then, why compiler needs to assign '\0' null terminator in char array & why not in others?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
cutedipti is offline Offline
45 posts
since Sep 2008
Oct 1st, 2008
0

Re: Null Char

Click to Expand / Collapse  Quote originally posted by cutedipti ...
Hi,
But, when we define any character array as
char a[3]={'a','b'};
then compiler here automatically consider '\0' null character to represent the end of the string
That's not true. You have explicitly initialised two elements of three-element array. The standards go about it in a round-about way (there's a logic train to follow to get to the conclusion, and the C and C++ standards have different logic trains) but the end result is that a[2] in your example will get a value of zero.

You will get the same effect (other than type of elements) with
  1. int a[3] = {1, 2};
ie a[2] will get the value of zero.



Click to Expand / Collapse  Quote originally posted by cutedipti ...
So, my question here is:
As compiler do not needs to use any such terminator to indicate that the int array or float array is terminated. Then, why compiler needs to assign '\0' null terminator in char array & why not in others?
It doesn't. If the number of explicitly initialised elements of an array is less that the size of the array, the standards specify how the other elements are initialised.

There is an anomaly in C (and C++) that allows char arrays to be initialised using strings (and arrays of wchar_t to be initialised with wide strings). Strings, by convention, are zero terminated. There is no equivalent to this style of initialisation for other types. But this anomaly is a different thing from what you're asking about.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Oct 3rd, 2008
0

Re: Null Char

Click to Expand / Collapse  Quote originally posted by grumpier ...
That's not true. You have explicitly initialised two elements of three-element array. The standards go about it in a round-about way (there's a logic train to follow to get to the conclusion, and the C and C++ standards have different logic trains) but the end result is that a[2] in your example will get a value of zero.

You will get the same effect (other than type of elements) with
  1. int a[3] = {1, 2};
ie a[2] will get the value of zero.




It doesn't. If the number of explicitly initialised elements of an array is less that the size of the array, the standards specify how the other elements are initialised.

There is an anomaly in C (and C++) that allows char arrays to be initialised using strings (and arrays of wchar_t to be initialised with wide strings). Strings, by convention, are zero terminated. There is no equivalent to this style of initialisation for other types. But this anomaly is a different thing from what you're asking about.




Hi,
Ya it's absolutely right that when we initialize array as:
a[3]={1,2} then it will assign last element to zero automatically.
But while considering as a string it must be terminated by '\0' null character.
when we write a code like:

int i;
char a[3]={'a','b','c'};
for(i=0;i<=2;i++)
printf("\t%c",a[i]);

then output for this code is as
a b c


but if it is like
int i;
char a[3]={'a','b','c'};
printf("%s",a);

then the output will be
abc with some garbage value means it's not 100% correct one

and if it is like
int i;
char a[3]={'a','b'};
printf("%s",a);

then the ouput will be 100% correct one as
ab

So, my query is, why this is happening only in case of strings and not for the others( like int, float, single character)?
Reputation Points: 10
Solved Threads: 0
Light Poster
cutedipti is offline Offline
45 posts
since Sep 2008
Oct 3rd, 2008
1

Re: Null Char

When you are using the printf function:
  1. printf("%s", a);

The function is treating variable "a" as a pointer to the beginning of a stream of characters. However, it doesn't have the information about how long the stream of characters is, it has no knowledge that "a" is defined as an array. Thus, the function will simply read from the beginning of the address that "a" points to, byte by byte, until it encounters the null character which indicates the end of a character string.

Integer, float and other types are not used to represent string.
Reputation Points: 118
Solved Threads: 15
Posting Pro in Training
Denniz is offline Offline
428 posts
since Sep 2008
Oct 3rd, 2008
1

Re: Null Char

By char a[3]={'a','b'}; you are implicitly assigning a zero to a[2] which is what makes the array a string.

>So, my query is, why this is happening only in case of strings and not for the others( like int, float, single character)?
Because you can't do this printf("%s",a); if variable a were an array of floats or ints.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Oct 3rd, 2008
1

Re: Null Char

Click to Expand / Collapse  Quote originally posted by cutedipti ...
So, my query is, why this is happening only in case of strings and not for the others( like int, float, single character)?
Because the %s format specifier tells printf() and related function that the corresponding argument is a pointer to char and to keep printing chars until it finds a zero.

When an array is passed to a function (be it array of char, double, or structs) only a pointer to the first element is passed. No information about the number of elements in the array is passed. So the function has to use some convention (as printf() does with the %s specifier) or make an assumption (eg it assumes the array is of length 5).
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Oct 4th, 2008
0

Re: Null Char

There is a difference between a character array and a string. A character array
char a[3] = {'a', 'b', 'c'}; is not a string. Just because it happens to end in a '\0' when you don't define all the elements
char a[3] = {'a', 'b'}; does not make it a string. Don't use it as such.
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,717 posts
since May 2006
Oct 4th, 2008
0

Re: Null Char

Click to Expand / Collapse  Quote originally posted by WaltP ...
There is a difference between a character array and a string. A character array
char a[3] = {'a', 'b', 'c'}; is not a string. Just because it happens to end in a '\0' when you don't define all the elements
char a[3] = {'a', 'b'}; does not make it a string. Don't use it as such.
I admit that I don't know if you are talking semantic definitions over here, but I disagree. What it makes an string a string is that '\0', and as long as the automatic assignment is honored by the compiler, that is for all intend and purposes a string. Whether the '\0' falls in the last subscript or before that.

Is it a lousy way of making a string? Sure. But not more than assigning the elements individually as a[0] = 'a'; a[1] = 'b'; a[2] = '\0';
So, please, explain "There is a difference between a character array and a string."
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Oct 4th, 2008
0

Re: Null Char

A c-style string (eg a string literal) is an array of char that, by convention, is terminated with '\0'.
Last edited by grumpier; Oct 4th, 2008 at 5:34 am.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Feb 28th, 2011
-1
Re: Null Char
hi im vikram my question is
WEATHER A NULL CHARACTER OCCUPY ANY SPACE IN MEMORY IF YES THAN HOW MUCH SPACE IT NEEDED?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
VIKRAM KARKI is offline Offline
1 posts
since Feb 2011

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Cosine Function
Next Thread in C Forum Timeline: Finding the degree of each vertex.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC