Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > String array() in C

Reply
Thread Tools

String array() in C

 
 
JackYee123@gmail.com
Guest
Posts: n/a
 
      05-07-2007
Hey,

I need a structure to store a string array in c, for example

Index Content
-------- -----------
0 word1
1 word2
2
3
4
5

 
Reply With Quote
 
 
 
 
JackYee123@gmail.com
Guest
Posts: n/a
 
      05-07-2007
Here is the compelte message

Hi,

I need a structure to store a string array in c, for example

Index Content
-------- -----------
0 word1
1 word2
2 word3
3 word4
4 word5
5 word6
--------------------

I was thinking to use char**, but I don't want to use double pointer,
if there an easy way to get around this?

Thanks.



 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      05-07-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ps.com...
> Here is the compelte message
>
> Hi,
>
> I need a structure to store a string array in c, for example
>
> Index Content
> -------- -----------
> 0 word1
> 1 word2
> 2 word3
> 3 word4
> 4 word5
> 5 word6
> --------------------
>
> I was thinking to use char**, but I don't want to use double pointer,
> if there an easy way to get around this?
>

No. You can declare an array of pointers, but really the array is just a
char ** dressed up with different syntax.
If don't know how many string you need at run time

char **list;
int N;

and malloc() is the way to go.
If you do

char *list[N];

is OK.

However you very rarely need raw tables of strings. Usually the string is
tied to something. So
struct mydata
{
char *word;
int value;
double value2;
};

where value and value2 are arbitrary things, eg counts, associated with each
"word", is more common.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      05-07-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Here is the compelte message
>
> Hi,
>
> I need a structure to store a string array in c, for example
>
> Index Content
> -------- -----------
> 0 word1
> 1 word2
> 2 word3
> 3 word4
> 4 word5
> 5 word6
> --------------------
>
> I was thinking to use char**, but I don't want to use double pointer,
> if there an easy way to get around this?


Is your number of strings fixed, or variable?




Brian

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      05-07-2007
(E-Mail Removed) wrote On 05/07/07 13:31,:
> Here is the compelte message
>
> Hi,
>
> I need a structure to store a string array in c, for example
>
> Index Content
> -------- -----------
> 0 word1
> 1 word2
> 2 word3
> 3 word4
> 4 word5
> 5 word6
> --------------------
>
> I was thinking to use char**, but I don't want to use double pointer,
> if there an easy way to get around this?


There are several approaches with different advantages
and disadvantages. You haven't specified your needs very
precisely, so I'll just sketch out a few methods. Note that
these are NOT equivalent!


/* Fixed-length array of fixed-length words (any
* short words are followed by extra '\0' bytes
* to a total size of six). Array elements are
* modifiable, but no word can grow beyond five
* payload characters.
*/
char words[][5+1] = { "word1", ..., "word6", };


/* Fixed-length array of pointers to words of
* arbitrary length. The pointers can be changed
* to point at different words, but the original
* word data cannot be changes.
*/
char *words[] = { "word1", ..., "word6", };


/* Fixed-length array of pointers to words of
* arbitrary length. Both the pointers and the
* words can be changed, but the original words
* cannot be lengthened in place.
*/
char word1[] = "word1";
...
char word6[] = "word6";
char *words[] = { word1, ..., word6, };


/* Dynamically allocated "array" of fixed-length
* words. Array elements are modifiable, but the
* words themselves cannot be lengthened. The
* typedef is for clarity, and can be eliminated.
*/
typedef char Word[5+1];
Word *words = malloc(N * sizeof *words);
if (words != NULL) {
strcpy (words[0], "word1");
...
strcpy (words[5], "word6");
}


/* Dynamically-allocated "array" of pointers to
* dynamically-allocated words. Everything is
* modifiable, replaceable, extensible, all-
* singing, all-dancing, and carbon-neutral.
*/
char **words = malloc(N * sizeof *words);
if (words != NULL) {
words[0] = malloc(sizeof "word1");
if (words[0] != NULL)
strcpy (words[0], "word1");
...
/* A different way to calculate the size: */
words[5] = malloc(strlen("word6") + 1);
if (words[5] != NULL)
strcpy (words[5], "word6");
}

--
(E-Mail Removed)
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      05-07-2007
(E-Mail Removed) wrote:
> Hey,
>
> I need a structure to store a string array in c, for example
>
> Index Content
> -------- -----------
> 0 word1
> 1 word2
> 2
> 3
> 4
> 5
>


char array_of_strings[6][6] = {"word1", "word2"}; /* in your restricted
chase. The strings are modifiable. */

char *array_of_strings[] = {"word1", "word2"}; /* More generally, but
the strings must be copied elsewhere if you want to use modified forms
of them, although the pointers can be modified. */

char *array_of_strings[6] = {"word1", "word2"}; /* If you must have six
strings, and string literals are not a problem. */

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      05-07-2007
Malcolm McLean wrote:
>
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ps.com...

[...]
>> I was thinking to use char**, but I don't want to use double pointer,
>> if there an easy way to get around this?
>>

> No. You can declare an array of pointers, but really the array is just a
> char ** dressed up with different syntax.


This is wrong. Malcolm has been around long enough to know that a
pointer is not an array, and a pointer to a pointer is not the same as
an array of pointers. And he has been around long enough to know that
lying to seekers after knowledge is not welcome here.

 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      05-07-2007
Martin Ambuhl <(E-Mail Removed)> writes:

> (E-Mail Removed) wrote:
>> Hey,
>>
>> I need a structure to store a string array in c, for example
>>
>> Index Content
>> -------- -----------
>> 0 word1
>> 1 word2
>> 2
>> 3
>> 4
>> 5
>>

>
> char array_of_strings[6][6] = {"word1", "word2"}; /* in your
> restricted chase. The strings are modifiable. */


That is disgusting code.

>
> char *array_of_strings[] = {"word1", "word2"}; /* More generally, but
> the strings must be copied elsewhere if you want to use modified forms
> of them, although the pointers can be modified. */


Which pointers? No "pointers" can be modified.

>
> char *array_of_strings[6] = {"word1", "word2"}; /* If you must have
> six strings, and string literals are not a problem. */
>


Misleading. If you are going to use "six" then specify all the strings.


--
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      05-07-2007
Richard <(E-Mail Removed)> wrote:
> Martin Ambuhl <(E-Mail Removed)> writes:
> > (E-Mail Removed) wrote:
> >> I need a structure to store a string array in c, for example
> >>
> >> Index Content
> >> -------- -----------
> >> 0 word1
> >> 1 word2
> >> 2
> >> 3
> >> 4
> >> 5
> >>

> >
> > char array_of_strings[6][6] = {"word1", "word2"}; /* in your
> > restricted chase. The strings are modifiable. */


> That is disgusting code.


Why? Perhaps an array of 6 strings, each long enough to hold 6 chars
is exactly what the OP needs. The problem is that underspecified that
this could be just the correct solution. But since that's not clear,
Mr. Ambuhl continued with:

> > char *array_of_strings[] = {"word1", "word2"}; /* More generally, but
> > the strings must be copied elsewhere if you want to use modified forms
> > of them, although the pointers can be modified. */


> Which pointers? No "pointers" can be modified.


What are you talking about?

> > char *array_of_strings[] = {"word1", "word2"};


defines an array of two pointers to char arrays and those pointers
can be modified, i.e. made to point to other strings (or char
arrays to be precise).

> > char *array_of_strings[6] = {"word1", "word2"}; /* If you must have
> > six strings, and string literals are not a problem. */


> Misleading. If you are going to use "six" then specify all the strings.


Which would you use, going by what the OP wrote? Invent some?

Regards, Jens
--
\ Jens Thoms Toerring ___ (E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      05-07-2007
On May 8, 7:31 am, Martin Ambuhl <(E-Mail Removed)> wrote:
> Malcolm McLean wrote:
> > [rubbish confusing arrays and pointers]

>
> This is wrong. Malcolm has been around long enough to know that a
> pointer is not an array, and a pointer to a pointer is not the same as
> an array of pointers. And he has been around long enough to know that
> lying to seekers after knowledge is not welcome here.


Are you sure he is lying? Perhaps he is just mistaken. With the
the number of wrong posts he makes each day (even more than
I do!), it would be quite a tour-de-force of trollage.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
'System.String[]' from its string representation 'String[] Array' =?Utf-8?B?UmFqZXNoIHNvbmk=?= ASP .Net 0 05-04-2006 04:29 PM
Is "String s = "abc";" equal to "String s = new String("abc");"? Bruce Sam Java 15 11-19-2004 06:03 PM
String[] files = {"a.doc, b.doc"}; VERSUS String[] files = new String[] {"a.doc, b.doc"}; Matt Java 3 09-17-2004 10:28 PM
String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 3 12-05-2003 04:20 PM
Re: String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 0 12-04-2003 04:40 PM



Advertisments