473,411 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

String array() in C

Hey,

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

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

May 7 '07 #1
12 43474
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.

May 7 '07 #2

<Ja********@gmail.comwrote in message
news:11*********************@h2g2000hsg.googlegrou 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

May 7 '07 #3
Ja********@gmail.com 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

May 7 '07 #4
Ja********@gmail.com 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");
}

--
Er*********@sun.com
May 7 '07 #5
Ja********@gmail.com 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. */

May 7 '07 #6
Malcolm McLean wrote:
>
<Ja********@gmail.comwrote in message
news:11*********************@h2g2000hsg.googlegrou 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.

May 7 '07 #7
Martin Ambuhl <ma*****@earthlink.netwrites:
Ja********@gmail.com 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.
--
May 7 '07 #8
Richard <rg****@gmail.comwrote:
Martin Ambuhl <ma*****@earthlink.netwrites:
Ja********@gmail.com 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 ___ jt@toerring.de
\__________________________ http://toerring.de
May 7 '07 #9
On May 8, 7:31 am, Martin Ambuhl <mamb...@earthlink.netwrote:
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.

May 7 '07 #10
In article <5a*************@mid.uni-berlin.de>,
Jens Thoms Toerring <jt@toerring.dewrote:
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?
It appears that the OP inadvertently sent the article before he finished
it. He followed up with a complete version that included six strings.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
May 7 '07 #11
Richard wrote:
Martin Ambuhl <ma*****@earthlink.netwrites:
>Ja********@gmail.com 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.
Perhaps you could explain what aesthetic criterion leads you to such an
idiosyncratic claim.
>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.
array_of_strings is an array of pointer, all of which can be modified.
Perhaps you could explain what strange language standard leads you to
such an idiosyncratic claim.

>
>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.
If you knew your ass from a whole in the ground, you would know
a) the initialization is complete and
b) contains all the information in the original poster's message. To
pretend that one "knows" what the other strings are is a misleading
claim of mind-reading; to pretend that the initialization requires 6
explict initialisers is misleading and untrue.

Perhaps you could explain what leads you to such an absurd and
idiosyncratic claim.

May 7 '07 #12
Old Wolf wrote:
On May 8, 7:31 am, Martin Ambuhl <mamb...@earthlink.netwrote:
>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.
I think you're on to something here Wolf. :-)

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
May 8 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
22
by: spike | last post by:
How do i reset a string? I just want to empty it som that it does not contain any characters Say it contains "hello world" at the time... I want it to contain "". Nothing that is.. Thanx
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
14
by: Bob | last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in...
8
by: Jeff Johnson | last post by:
Hi, I've begun converting an ASP site over to .NET and I'm a novice at both the new platform as well as C#. I have a COM+ object that returns a string array when it is called. The size of...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
11
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer ...
14
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.