Re: Array of char pointers.



On Mon, 14 Apr 2008 09:31:13 -0700 (PDT), goodTweetieBird
<goodTweetieBird@xxxxxxxxxxx> wrote:

On Apr 13, 9:57 pm, Barry Schwarz <schwa...@xxxxxxxx> wrote:
On Sun, 13 Apr 2008 09:25:11 -0700 (PDT), goodTweetieBird

<goodTweetieB...@xxxxxxxxxxx> wrote:

If I had to say what "arr" is, I would say it is an array of pointers
to chars. If so why is it intialized with strings instead of
addresses?

   char *arr[] =
   {
           "a", "b", "c","dd", "eee"
   };

It is not initialized with strings.  It is initialized with the
address of strings.

I misspoke, I should have said why does it appear to be initialized
with strings as I am providing strings and not addresses. But you
figured out what I meant.

Instead of

char *arr[] =

should I write

char const *arr[] =

If the elements of arr will always point to sting literals, then the
const modifier is appropriate. String literals are not modifiable and
the const modifier compels the compiler to issue a diagnostic if you
try to change any part of them.

However, the const modifier is completely unrelated to your question.
It only appears you are initializing the array with strings. Each of
the string literals is actually an array. In this context, an
expression of array type is converted to the address of the first
array element with type pointer to element. So the "a" is actually an
array of two char ('a' and '\0') which is evaluated as the address of
the 'a' with type char*. This is exactly the type that should be
assigned to arr[0].


Remove del for email
.



Relevant Pages

  • Re: files and directories into an array of arrays
    ... can use an array of arrays of char: ... out that you in fact need 43 strings, ... struct filename * next; ... size array in struct filename is wasteful and that you can easily replace it ...
    (comp.lang.c)
  • Re: two dimensional arrays passed to functions
    ... > array and then send it down as a single dimmensional array. ... x is an array of char pointers. ... to a pointer to the first element of this array, ... need to copy the strings and not just assign pointers to them). ...
    (comp.lang.c)
  • Re: a string, a string array and character array
    ... which defined its "char" to be in the range -128 to 127. ... with strings, like sorting. ... A character array is an array of character type; ...
    (comp.soft-sys.matlab)
  • Re: Help - JNI and JMS - data conversion problems
    ... DataOutputStream in a raw byte array. ... Note when you do this your Strings will be counted UTF-8 format. ... You could write them an char[] if you wanted the simplicity of 16-bit ...
    (comp.lang.java.programmer)
  • Re: Returning an array of strings in C
    ... fucntion and return char** type. ... you can't return an array from a C function. ... Now for strings it gets a bit more ... take when allocating memory dynamically. ...
    (comp.lang.c)