Re: initializing table of strings

From: Francis Glassborow (francis_at_robinton.demon.co.uk)
Date: 05/01/04


Date: Fri, 30 Apr 2004 23:40:08 +0100

In message <f74d6851.0404300848.71e0f1c1@posting.google.com>, Rafi Kfir
<rafi.kfir@telrad.co.il> writes
>Hi Francis and thank you so much for the extensive answere.
>
>The missing component in my program was:
>
>>
>> char **p = malloc(sizeof(char*) * n);
>>
>
>After that, in the program body I can do:
>*p = "ABC";
>*(p+1) = "CDE";
>*(p+2) = "FGE";
>I checked it and it works.

And you can also write:

p[0] = "ABC";
p[1] = "CDE";
p[2] = "FGE";

and I prefer that syntax because it emphasises that it is conceptually
an array.

>
>
>The following, however, does not work for me as I need to set the
>pointers several times in the program. Therefore, it does not make
>sense to set strings in decleration.
>>
>> char const *(p)[] = {"Two lovers standing",
>> "looking at the sun-kissed lake.",
>> "The barn door slams shut."};

But you should always initialise a pointer if only to the null pointer
(zero).

The fact that it is an array of pointers to const char does not matter
because that just means that the strings pointed to are immutable but
the pointers can point to different strings at different times/

You could write:

char const *(line)[3] = {0};

which creates an array of three pointers to const char which are
initialised with the null pointer.

Now when you want/need you can use assignment to store the address of a
string:

line[0] = "Seven white cliffs stand";
line[1] = "crumbling into the ocean.";
line[3] = "Sea gulls wheel above."

>>
>
>1) Do I understand it alrigt?

I don't know, did you?

>2) Is there a shorter way to set the strings other than line by line
>(my table is 3x3 of strings)?

Depends what you want to do. Unless the strings are literals you will
need to allocate storage. If you want 3 strings of 3 chars each you will
actually need an array of 3 arrays of fours char because strings need a
space for the nul terminator. You could write:

char data[3][4] = {"abc", "bcd", "cde"};

Which stores copies of the quoted strings into an array of 3.
To change the contents you would have to either work char by char or use
strncpy()

E.g. You could write:

data[1][0] = 'B';
to change the first character of the second string from 'b' to B.

or you could write:

strncpy(data[2], "xyz", 3);

to replace 'cde' by 'xyz'

-- 
Francis Glassborow      ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


Relevant Pages

  • Re: K&R2 Secition 5.9 - major blunders
    ... Would changing 'point to a' to 'point into a' twenty element array be ... > arrays of pointers is to store character strings of diverse ... comparison between what was really happening (arrays of pointers to ... pointer to a string(this probably would confuse beginners)" and ...
    (comp.lang.c)
  • Re: Newbee needs once more again help with passing arrays out of a function
    ... what llong is etc. So in trying to write something useful ... if you want to pass back an array of integers and an array of ... int argc, char *argv) ... of char pointers we allocated before. ...
    (comp.lang.c)
  • Re: Function Problem
    ... > but fnReplace takes pointers to strings of length 16. ... > void fnReplace (char *RplStr) ...
    (microsoft.public.windowsce.embedded.vc)
  • 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: Queue question wih dyn mem alloc
    ... I wrote two functions to pusha character array onto an array and ... int Len=0; ... int push(char **list, char *str, int curlen){ ... arrays to carry an array of strings. ...
    (comp.lang.c)