Re: initializing table of strings
From: Francis Glassborow (francis_at_robinton.demon.co.uk)
Date: 05/01/04
- Next message: Francis Glassborow: "Re: temporary file in C++"
- Previous message: Kevin Chen: "Re: Runtime errors but no compiletime errors"
- Next in thread: Rafi Kfir: "Re: initializing table of strings"
- Reply: Rafi Kfir: "Re: initializing table of strings"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Francis Glassborow: "Re: temporary file in C++"
- Previous message: Kevin Chen: "Re: Runtime errors but no compiletime errors"
- Next in thread: Rafi Kfir: "Re: initializing table of strings"
- Reply: Rafi Kfir: "Re: initializing table of strings"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|