Question:

What is the difference between char a[ ] = "string"; and char *p = "string"; in c++? ?

by  |  earlier

0 LIKES UnLike

What is the difference between char a[ ] = "string"; and char *p = "string"; in c++? ?

 Tags:

   Report

2 ANSWERS


  1. we have been studying that too. maybe if we already discussed it, i may answer your answer. but for now, this will do.

    http://en.wikipedia.org/wiki/C++


  2. char a[] allocates an array of storage and copies the string into this array.

    char *p allocates a pointer type variable and sets it to the address of the string constant.

    If you're doing this in automatic storage the char * version is much more efficient, since the pointer type can be put into a register and simply assigned a value, whereas the char array needs to be allocated on the stack each time and then copied into in order to initialize.

    However the char * version is not mutable as it points into the string constant table.

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.