Array of pointers

S

Slain

I have more of a conceptual question now. Let us say I do this:-

char *str[10]; --> create an array of pointers
str[1]= "John";

I thought this would automatically put John at some memory space and
point str[1] to it.
My colleague argues that in flat bed memory this should not be done. A
new should be used to allocate the memory and then point to it. Of
course he didnt do the best job explaining, hence my question to you
guys, if you can shed some more light on this.

Thanks in anticipation
 
M

Micah Cowan

Slain said:
I have more of a conceptual question now. Let us say I do this:-

char *str[10]; --> create an array of pointers
str[1]= "John";

I thought this would automatically put John at some memory space and
point str[1] to it.
My colleague argues that in flat bed memory this should not be done. A
new should be used to allocate the memory and then point to it. Of
course he didnt do the best job explaining, hence my question to you
guys, if you can shed some more light on this.

Well, AIUI, in typical implementations, the values for string literals
loaded into memory before the program is even run, so you probably
won't need to worry about that.

What I'm more concerned with, is that the memory for string literals
is not modifiable: in fact, the type for a string literal (aside from
initializers, which doesn't apply to your case) is "array of const
char", whereas you're assigning it to something that wants "pointer
to (non-const) char". This is currently a legal conversion, but is
deprecated and so may not be legal in future versions of C++ (it
appears, at least for now, that it is being kept for the next version
of C++); in any case, it is likely unwise. Assigning to a const char *
is preferable, as it helps the compiler to detect and prevent attempts
to modify the string literal.

Basically, if you don't expect to be modifying the strings pointed at
in the elements of str, you should declare it as an array of const
char *s. OTOH, if you do expect to modify some of the strings, you
should not be initializing them directly from string literals (if you
expect to be modifying the strings' sizes--particularly, if you may
need to write larger strings there--you should probably be using
std::string's, which you may prefer to use in any case).
 
U

utab

I have more of a conceptual question now. Let us say I do this:-

char *str[10]; --> create an array of pointers
str[1]= "John";

I thought this would automatically put John at some memory space and
point str[1] to it.

Correct me if I am wrong but str[1] is used to dereference. str[1]
actually is the character array itself.

AFAIK, str points to the start address of the array and index notation
provides offsets to the character arrays which are dereferenced
through these addresses of the character arrays.

Hope I did not make big mistakes above.
 
S

Szabolcs Ferenczi

I have more of a conceptual question now. Let us say I do this:-

char *str[10]; --> create an array of pointers
str[1]= "John";

I thought this would automatically put John at some memory space and
point str[1] to it.

I think your reading is correct and by a quick test the compiler also
confirms this. You can check it yourself with help of this guide:

http://www.ericgiguere.com/articles/reading-c-declarations.html

Your declaration reads: "declare str as array 10 of pointer to char"
Since you said you need an array of 10 pointers, a pointer array of
size 10 will be allocated too.
My colleague argues that in flat bed memory this should not be done. A
new should be used to allocate the memory and then point to it.

I think it is a programming language question and as such it is
independent of any physical realization of the storage.

Your colleague would be right, but not because of any kind of storage
implementation, if you declared it like this:

char **str; /* "declare str as pointer to pointer to char" */

In this case you declare a single pointer, so only what is declared
will be allocated.

Best Regards,
Szabolcs
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top