hi there,
Is there any difference between these two kind of storage of characters in C
Any help is appreciated!!Code:char array_ca[10]={"YES"}; OR char array_ca[0]='Y'; char array_ca[1]='E'; char array_ca[2]='S';
hi there,
Is there any difference between these two kind of storage of characters in C
Any help is appreciated!!Code:char array_ca[10]={"YES"}; OR char array_ca[0]='Y'; char array_ca[1]='E'; char array_ca[2]='S';
This is perfectly alright.Code:char array_ca[10]={"YES"};
This is an error. You're redefining the array_ca again and again.Code:char array_ca[0]='Y'; char array_ca[1]='E'; char array_ca[2]='S';
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
What in case I have an array name different for the second one I mean say,
However the first remains the same thatsCode:char arr1_ca[0]='Y'; char arr1_ca[1]='E'; char arr1_ca[2]='S';
Just wanted to know if these both character arrays are internally considered by C complier as same or they stored differently?Code:char array_ca[10]={"YES"};
Again you're redefining the arr1_ca multiple times. Remove the char keyword.
But I guess I got what you want to ask.
In this case the char array gets automatically terminated by the '\0' chararacter.Code:char array_ca[10]={"YES"};
In the latter case you'll have to do it yourself like
Notice here I haven't redefined arr1_ca. It has got only one definition.Code:char arr1_ca[4]; // only one definition arr1_ca[0]='Y'; arr1_ca[1]='E'; arr1_ca[2]='S'; arr1_ca[3]='\0';
If you would not do the null termination yourself, you'll get garbage printed out after the last character which will not happen in the former case.
I hope I explained it well.
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
Thanks Ben for your reply. Yes I got what you said !
But have certain things still on my mind still. Sorry abt that though!
So if a do a string comparision on both of these character arrays, it would be a 0 using strncmp function for first 3 characters. Right? {Considering fact arr1_ca is null terminated explicitly}
Also, inwhat would be value @array_ca[5] or [6] or for offsets where no value is stored ?Code:array_ca[10]={"YES"};
No need to say sorry for asking questions. People learn by asking questions and assimilating new information.
Coming back to your question, using strcmp on two similar arrays will definitely result in 0.
Secondly the array_ca is stored internally as
So, everything beyond the first NULL terminator gets filled with NULL terminators uptil the last char. You can check this by doingCode:array_ca[0]='Y'; array_ca[1]='E'; array_ca[2]='S'; array_ca[3]='\0'; array_ca[4]='\0'; array_ca[5]='\0'; array_ca[6]='\0'; array_ca[7]='\0'; array_ca[8]='\0'; array_ca[9]='\0';
It'll print a space which the ASCII representation of '\0', or by doing a %d it'll print 0 which the ASCII value of '\0'.Code:printf("%c",array_ca[5]);
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
Thanks a lot Ben that made things clear