9 Replies - 8969 Views - Last Post: 10 July 2011 - 02:27 PM
#1
What is the main difference between char array and string ?
Posted 09 July 2011 - 11:19 PM
regards
Replies To: What is the main difference between char array and string ?
#2
Re: What is the main difference between char array and string ?
Posted 09 July 2011 - 11:38 PM
Then, tell us what you know about '\0'.
You have been asking for explanations in the past few threads. And it's along the lines of "explain everything to me", which is unacceptable. Why don't you show us what you have tried to learn and where you got confused?
#3
Re: What is the main difference between char array and string ?
Posted 09 July 2011 - 11:47 PM
Oler1s, on 09 July 2011 - 11:38 PM, said:
Then, tell us what you know about '\0'.
You have been asking for explanations in the past few threads. And it's along the lines of "explain everything to me", which is unacceptable. Why don't you show us what you have tried to learn and where you got confused?
i have done a lot ov questions of char arrays and a little bit ov strings. only in strings '\0' comes at the end of the string but the point is how we can ditinguish between a char array nd string because the working of both is same.
#4
Re: What is the main difference between char array and string ?
Posted 09 July 2011 - 11:53 PM
1) Code example of a char array and code example of a string.
2) What you do know about the '\0' character.
If you can't answer the above two questions, clearly, you need to go back to the books. If you can answer, then...answer them.
#5
Re: What is the main difference between char array and string ?
Posted 10 July 2011 - 12:33 AM
ezooo, on 10 July 2011 - 07:47 AM, said:
Oler1s, on 09 July 2011 - 11:38 PM, said:
Then, tell us what you know about '\0'.
You have been asking for explanations in the past few threads. And it's along the lines of "explain everything to me", which is unacceptable. Why don't you show us what you have tried to learn and where you got confused?
i have done a lot ov questions of char arrays and a little bit ov strings. only in strings '\0' comes at the end of the string but the point is how we can ditinguish between a char array nd string because the working of both is same.
If your question is "How do you programmatically tell the difference between a plain char array without a null terminator, and a null-terminated char array", then the answer is simply "you can't".
If you create an array of chars without a null terminator, and then try to use it alongside the C-string functions (strncpy, printf, etc), then your program may start to do all kinds of odd things, it may even crash.
Its the programmer's responsibility to make sure that char arrays include the null terminator whenever they are supposed to represent strings. string literals (i.e. strings in "double quotes") are always null terminated, and sometimes the C functions will append one for you when they read a string (e.g. fgets) But don't always count on it!
If you're in any doubt, then stick a '\0' character at the end of your char array just to be on the safe side
#6
Re: What is the main difference between char array and string ?
Posted 10 July 2011 - 03:05 AM
Just stick with the '\0' character at the end of your char array..
Many functions use this as a sign that it reached the end of character arrays. (or not the end of the character array but the end of the words).
#7
Re: What is the main difference between char array and string ?
Posted 10 July 2011 - 03:15 AM
Oler1s, on 09 July 2011 - 11:53 PM, said:
1) Code example of a char array and code example of a string.
2) What you do know about the '\0' character.
If you can't answer the above two questions, clearly, you need to go back to the books. If you can answer, then...answer them.
okay..lemme do this ;p
Bench, on 10 July 2011 - 12:33 AM, said:
ezooo, on 10 July 2011 - 07:47 AM, said:
Oler1s, on 09 July 2011 - 11:38 PM, said:
Then, tell us what you know about '\0'.
You have been asking for explanations in the past few threads. And it's along the lines of "explain everything to me", which is unacceptable. Why don't you show us what you have tried to learn and where you got confused?
i have done a lot ov questions of char arrays and a little bit ov strings. only in strings '\0' comes at the end of the string but the point is how we can ditinguish between a char array nd string because the working of both is same.
If your question is "How do you programmatically tell the difference between a plain char array without a null terminator, and a null-terminated char array", then the answer is simply "you can't".
If you create an array of chars without a null terminator, and then try to use it alongside the C-string functions (strncpy, printf, etc), then your program may start to do all kinds of odd things, it may even crash.
Its the programmer's responsibility to make sure that char arrays include the null terminator whenever they are supposed to represent strings. string literals (i.e. strings in "double quotes") are always null terminated, and sometimes the C functions will append one for you when they read a string (e.g. fgets) But don't always count on it!
If you're in any doubt, then stick a '\0' character at the end of your char array just to be on the safe side
exactly .this is what i was looking for. thanx ..because yesterday when i was compiling a code of char array and was using string functions as strlen() to find its length , my programm crashed two times and my laptop automatically was shutdown and i was shocked that whatz going on . And i think it was due to '\0' type thing .
#8
Re: What is the main difference between char array and string ?
Posted 10 July 2011 - 05:37 AM
In ANSI C there is no such thing as a string. In C++ there is a fancy string type, but in reality it's nothing different than char*. So, to cut it short in C and C++ there really isn't a string per-say. It's just a pointer to a chunk of memory holding characters ending with a terminator '\0'.
A character array isn't a character pointer. Explaining the technical difference is actually quite complicated. In C, an array is essentially a pointer but some slight annoyances come along with arrays. An array is simply a container data structure. It comes with nothing in it.
You could pretend a char* 'string' is an array.
char* name = "billy";
printf("%c\n", name[2]);
Heck, you can do it in C++:
string name = "johnson"; std::cout << name[3] << endl;
These character pointers aka strings are just chunks of memory the sizeof the number of letters times plus one (terminator) times the size of a character on the given machine. C++ made C 'strings' simpler by giving a stronger standard library for working with them.
An array is simply a data type.
The main difference is you expect a terminator at the end of a 'string.' A 'string' is generally a fixed length and generally doesn't consist of any extra space. A character array can be much longer than the space it is using and generally contains extra space. In the case of a 'string' most functions and/or operations will find the terminator and stop scanning from there. An array is not like that because it doesn't contain a terminator by default though you can include one.
I say 'string' because C and C++ don't really have strings.
Good fun right here!
#9
Re: What is the main difference between char array and string ?
Posted 10 July 2011 - 09:30 AM
No, it isn't.
> You could pretend a char* 'string' is an array.
No, you can't.
> there is a fancy string type, but in reality it's nothing different than char*
Well, aside from that it isn't a primitive and it isn't a pointer (let alone to a char)...
#10
Re: What is the main difference between char array and string ?
Posted 10 July 2011 - 02:27 PM
stackoverflow, on 10 July 2011 - 01:37 PM, said:
stackoverflow, on 10 July 2011 - 01:37 PM, said:
stackoverflow, on 10 July 2011 - 01:37 PM, said:
stackoverflow, on 10 July 2011 - 01:37 PM, said:
stackoverflow, on 10 July 2011 - 01:37 PM, said:
http://c-faq.com/aryptr/index.html
stackoverflow, on 10 July 2011 - 01:37 PM, said:
stackoverflow, on 10 July 2011 - 01:37 PM, said:
char* name = "billy";
printf("%c\n", name[2]);
stackoverflow, on 10 July 2011 - 01:37 PM, said:
string name = "johnson"; std::cout << name[3] << endl;
stackoverflow, on 10 July 2011 - 01:37 PM, said:
stackoverflow, on 10 July 2011 - 01:37 PM, said:
stackoverflow, on 10 July 2011 - 01:37 PM, said:
I say 'string' because C and C++ don't really have strings.
What you're saying would be a bit like "C++ doesn't have resizable arrays because vectors are just ordinary dynamically-allocated arrays under the hood", but that statement would be completely missing the point.
This post has been edited by Bench: 10 July 2011 - 02:41 PM
|
|

New Topic/Question
Reply



MultiQuote





|