9 Replies - 17513 Views - Last Post: 10 July 2011 - 02:27 PM Rate Topic: -----

#1 ezooo  Icon User is offline

  • New D.I.C Head

Reputation: -9
  • View blog
  • Posts: 33
  • Joined: 26-June 11

What is the main difference between char array and string ?

Posted 09 July 2011 - 11:19 PM

What is the obvious differnce between character array and string and what is the role of '\0' for them. I have read about this but can someone explain this to me , is the difference comes whent we intialize them or what and how they look like in memory.
regards
Is This A Good Question/Topic? 0
  • +

Replies To: What is the main difference between char array and string ?

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: What is the main difference between char array and string ?

Posted 09 July 2011 - 11:38 PM

Do this first, for us. Show us an example of a character array, and then an example of a string. (Yes, actual code). Then tell us what you think the differences are.

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?
Was This Post Helpful? 1
  • +
  • -

#3 ezooo  Icon User is offline

  • New D.I.C Head

Reputation: -9
  • View blog
  • Posts: 33
  • Joined: 26-June 11

Re: What is the main difference between char array and string ?

Posted 09 July 2011 - 11:47 PM

View PostOler1s, on 09 July 2011 - 11:38 PM, said:

Do this first, for us. Show us an example of a character array, and then an example of a string. (Yes, actual code). Then tell us what you think the differences are.

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.
Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: What is the main difference between char array and string ?

Posted 09 July 2011 - 11:53 PM

You haven't answered my questions. I asked for the following:

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.
Was This Post Helpful? 0
  • +
  • -

#5 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: What is the main difference between char array and string ?

Posted 10 July 2011 - 12:33 AM

View Postezooo, on 10 July 2011 - 07:47 AM, said:

View PostOler1s, on 09 July 2011 - 11:38 PM, said:

Do this first, for us. Show us an example of a character array, and then an example of a string. (Yes, actual code). Then tell us what you think the differences are.

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
Was This Post Helpful? 2
  • +
  • -

#6 CreamDelight  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 90
  • Joined: 04-July 11

Re: What is the main difference between char array and string ?

Posted 10 July 2011 - 03:05 AM

To simple things up..

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).
Was This Post Helpful? 0
  • +
  • -

#7 ezooo  Icon User is offline

  • New D.I.C Head

Reputation: -9
  • View blog
  • Posts: 33
  • Joined: 26-June 11

Re: What is the main difference between char array and string ?

Posted 10 July 2011 - 03:15 AM

View PostOler1s, on 09 July 2011 - 11:53 PM, said:

You haven't answered my questions. I asked for the following:

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

View PostBench, on 10 July 2011 - 12:33 AM, said:

View Postezooo, on 10 July 2011 - 07:47 AM, said:

View PostOler1s, on 09 July 2011 - 11:38 PM, said:

Do this first, for us. Show us an example of a character array, and then an example of a string. (Yes, actual code). Then tell us what you think the differences are.

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 .:P
Was This Post Helpful? 0
  • +
  • -

#8 stackoverflow  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 164
  • View blog
  • Posts: 545
  • Joined: 06-July 11

Re: What is the main difference between char array and string ?

Posted 10 July 2011 - 05:37 AM

I'm no expert but here is my opinion.

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!
Was This Post Helpful? -1
  • +
  • -

#9 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: What is the main difference between char array and string ?

Posted 10 July 2011 - 09:30 AM

> In C, an array is essentially a pointer

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)...
Was This Post Helpful? 0
  • +
  • -

#10 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: What is the main difference between char array and string ?

Posted 10 July 2011 - 02:27 PM

View Poststackoverflow, on 10 July 2011 - 01:37 PM, said:

I'm no expert but here is my opinion.
Most of what you've said in your post is wrong i'm afraid

View Poststackoverflow, on 10 July 2011 - 01:37 PM, said:

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*.
It's very different to char* for all sorts of reasons. The fact that it 'could' be implemented as such underneath the hood is irrelevant (Otherwise you could argue that C++ only has the same number of built-in types as C, and that is certainly not true). details of std::string, and of most of the other library types (vector, queue, iostreams, etc) are sufficiently hidden away to the point that it doesn't matter what's going on so long as you understand how to use it - standard library names are as much a part of the core language as keywords.

View Poststackoverflow, on 10 July 2011 - 01:37 PM, said:

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'.
in C++, a std::string certainly can be considered to be a string; just because its not a keyword doesn't make it any different. (There are others too from different libraries, e.g. MFC has CString and QT has QString; the same applies to these) - again you do not need to worry about how its implemented, you only need to worry about how to use it, therefore you can (and should) think of it as a complete string in itself.

View Poststackoverflow, on 10 July 2011 - 01:37 PM, said:

A character array isn't a character pointer. Explaining the technical difference is actually quite complicated.
It's not complicated at all. A pointer is a numerical value which represents a memory address. an array is a sequence of objects (all of the same data type) stored together in memory. it really IS that simple. If it seems more complicated than that, then you're probably misunderstanding something.

View Poststackoverflow, on 10 July 2011 - 01:37 PM, said:

In C, an array is essentially a pointer but some slight annoyances come along with arrays.
No that's totally wrong - please read all of the FAQ items in this link
http://c-faq.com/aryptr/index.html


View Poststackoverflow, on 10 July 2011 - 01:37 PM, said:

You could pretend a char* 'string' is an array.
No, you can't

View Poststackoverflow, on 10 July 2011 - 01:37 PM, said:

char* name = "billy";
printf("%c\n", name[2]);
That points to a string literal (to be correct it should really be const char*), and therefore completely non-modifiable. there are very limited things which you can do with the above C string.

View Poststackoverflow, on 10 July 2011 - 01:37 PM, said:

Heck, you can do it in C++:

string name = "johnson";
std::cout << name[3] << endl;

This is different since it creates a string object which owns a chunk of memory representing a string, and then copies the data from the string literal to that memory. The data in the string object is modifiable, resizable, and can be treated pretty much like other built-in types such as int/bool/double/etc.

View Poststackoverflow, on 10 July 2011 - 01:37 PM, said:

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.
What you're describing there is an array and not a pointer. A pointer is just a numerical (integer) value which represents an address. its size is usually a few bytes (generally no more than 4 or 8 bytes on a 32/64 bit platform). There are no arrays involved whatsoever. also char* does not guarantee a string either - it may be a simple byte-iterator.

View Poststackoverflow, on 10 July 2011 - 01:37 PM, said:

C++ made C 'strings' simpler by giving a stronger standard library for working with them.
C++ didn't do much to "C strings" except make them more-or-less obsolete (except for legacy code support). C++ defined a brand new string type which was easier to work with than character arrays, and you can't very elegantly mix the two together in the same code. its actually easier to mix a std::vector<char> with a C-string, because you have more access to the underlying representation of a vector than you do for a string. C++ strings are heavily self-contained and sealed.

View Poststackoverflow, on 10 July 2011 - 01:37 PM, said:

An array is simply a data type.

I say 'string' because C and C++ don't really have strings.
C++ does have strings in the form of std::string and all sorts of other 3rd party string libraries. There's no serious issues or "gotchas" associated with treating a C++ string as a complete standalone object; in fact, the whole point of std::string (and all kinds of other STL libraries and other 3rd party libraries) is that objects may be treated as complete entities without worrying about how they work under the hood.
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

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1