1

I want to know the difference between character array and string in c++.

Can any one answer to this?? Please,

Thanks Vishnukumar

Vishnu Lal
  • 189
  • 1
  • 4
  • 13

2 Answers2

3

string is a class/object, with methods and encapsulated data.

A char array is simply a contiguous block of memory meant to hold chars.

  • thank you... in char array we have to specify the size, in string it takes default 4bytes.. am i right? is this a different? – Vishnu Lal Mar 02 '13 at 06:25
  • "in string it takes default 4bytes": I don't know what you mean. –  Mar 02 '13 at 06:34
  • i m not sure how many bytes of memory string takes. i meant 4bytes of memory. – Vishnu Lal Mar 02 '13 at 06:39
  • It depends how long your string is, but you're getting into a separate question, and stackoverflow is a question and answer site, not a discussion forum, so it would be better for you to search to see if your question has already been answered, or ask a new question yourself. –  Mar 02 '13 at 06:40
  • when i search while asking question i didnt find duplicate one.. so i came to ask new .. i will mind this.. thank you – Vishnu Lal Mar 02 '13 at 06:45
2

(1) char array is just a block of char type data:
e.g. char c[100]; // 100 continuous bytes are allotted to c

(2a) By string, if you mean char string then, it's little similar to array but it's allocated in the readonly segment of the memory and should be assigned to a const char*:
e.g. const char *p = "hello"; // "hello" resides in continuous character buffer

[note: char c[] = "hello"; belongs to category (1) and not to (2a)]

(2b) By string if yo umean std::string then, it's a standard library class from header and you may want to refer its documentation or search on web

iammilind
  • 68,093
  • 33
  • 169
  • 336