473,222 Members | 1,809 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,222 software developers and data experts.

Arrays of Strings, malloc

Hi,
I am trying to read in a list of data from a file. Each line has a string in
its first column. This is what i want to read. I could start by saying
char[1000][] to read in 1000 lines to the array( i think!!).

But I want to use malloc. Each string is at most 50 characters long, and
there may be zero to thousands of lines. How do I actually start the array?
I have seen char **array etc. At first I tried char *array[50] but I think
that gives 50 pointers to chars :( .How do I use malloc to set space for the
array once I know how many lines there are? is it
array=malloc(lines*sizeof(char *)) ??

Finally, I want to print each of the strings using printf. How do I access
say the 30th line?

I have sucessfully done this a few times with doubles, ints etc, but now
with strings it seems a 'two dimesional' problem and I'm confused with the
pointer aspect of it.

Can anyone start me off on a simple solution?

Thanks
Ian

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.556 / Virus Database: 348 - Release Date: 26/12/2003
Nov 14 '05 #1
10 8974
Ian Todd wrote:
How do I use malloc to set space for the
array once I know how many lines there are? is it
array=malloc(lines*sizeof(char *)) ??
Yes.

I don't know how you know beforehand how many lines there are, though.
Do you read the file twice? If so, I'd suggest to instead start by
malloc()ing e.g. an array of 50 strings, and then as you read in the
file, realloc() it to twice the previous size whenever you have used up
the currently allocated space. When you reach the end of the file,
realloc() it down to just the necessary size, to free the unused
space you allocated.
Finally, I want to print each of the strings using printf. How do I access
say the 30th line?
array[29]. Assuming you count lines from 1 when you say 30th.
I have sucessfully done this a few times with doubles, ints etc, but now
with strings it seems a 'two dimesional' problem and I'm confused with the
pointer aspect of it.


It's just the same. Think of it as a one-dimensional array of 'char*'s,
since a string is a char*.

But a char* string needs malloced space itself, so that part is a bit
more complicated than int or double: W hen you read in a string, you'll
have to malloc space for it (with malloc(length of string + 1)), then
read or copy the string into that space. Later, when you are finished,
you must free() each string before you free() the array of strings.

If you don't know how long the strings can be, you can use the realloc
trick as above: malloc a size which is often enough, read in the string
a piece of a time, realloc as needed, and realloc the string down to its
actual size when done. Or you might just have a single input buffer
which you treat that way, and copy the strings from it into newly
allocated strings when you have read them in.

--
Hallvard
Nov 14 '05 #2
Ian Todd wrote:

Hi,
I am trying to read in a list of data from a file.
Each line has a string in
its first column. This is what i want to read. I could start by saying
char[1000][] to read in 1000 lines to the array( i think!!).

But I want to use malloc.
Each string is at most 50 characters long, and
there may be zero to thousands of lines.
How do I actually start the array?
I have seen char **array etc. At first I tried char *array[50]
but I think that gives 50 pointers to chars :(
.How do I use malloc to set space for the
array once I know how many lines there are? is it
array=malloc(lines*sizeof(char *)) ??

Finally, I want to print each of the strings using printf.
How do I access say the 30th line?

I have sucessfully done this a few times with doubles,
ints etc, but now
with strings it seems a 'two dimesional' problem
and I'm confused with the pointer aspect of it.

Can anyone start me off on a simple solution?


/* BEGIN hello.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LINES 1000
#define LENGTH 50

int main(void)
{
char (*line)[LENGTH], (*pointer)[50];

line = malloc(LINES * sizeof *line);
if (!line) {
fputs("malloc failure\n", stderr);
exit(EXIT_FAILURE);
}
strcpy(line[30], "hello, ");
strcpy(line[31], "world");
strcpy(line[32], "");
for (pointer = line + 30; **pointer; ++pointer) {
fputs(*pointer, stdout);
}
putchar('\n');
free(line);
return 0;
}

/* END hello.c */

--
pete
Nov 14 '05 #3
pete wrote:

Ian Todd wrote:

Hi,
I am trying to read in a list of data from a file.
Each line has a string in
its first column. This is what i want to read. I could start by saying
char[1000][] to read in 1000 lines to the array( i think!!).

But I want to use malloc.
Each string is at most 50 characters long, and
there may be zero to thousands of lines.
How do I actually start the array?
I have seen char **array etc. At first I tried char *array[50]
but I think that gives 50 pointers to chars :(
.How do I use malloc to set space for the
array once I know how many lines there are? is it
array=malloc(lines*sizeof(char *)) ??

Finally, I want to print each of the strings using printf.
How do I access say the 30th line?

I have sucessfully done this a few times with doubles,
ints etc, but now
with strings it seems a 'two dimesional' problem
and I'm confused with the pointer aspect of it.

Can anyone start me off on a simple solution?


/* BEGIN hello.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LINES 1000
#define LENGTH 50

int main(void)
{
char (*line)[LENGTH], (*pointer)[50];


(*pointer)[LENGTH]

--
pete
Nov 14 '05 #4
I do know how many lines there are by scanning the lines with fgets first.
So lines=34 say. I know then how much space I need for 34 lines, each with
50 characters. How do I start the array off? Is it
char * array[50]?

then array=malloc(lines*sizeof(char *))

Thanks,
Ian
"Ian Todd" <ha********@REMOVEyahooME.com> wrote in message
news:q5******************@newsfep1-win.server.ntli.net...
Hi,
I am trying to read in a list of data from a file. Each line has a string in its first column. This is what i want to read. I could start by saying
char[1000][] to read in 1000 lines to the array( i think!!).

But I want to use malloc. Each string is at most 50 characters long, and
there may be zero to thousands of lines. How do I actually start the array? I have seen char **array etc. At first I tried char *array[50] but I think
that gives 50 pointers to chars :( .How do I use malloc to set space for the array once I know how many lines there are? is it
array=malloc(lines*sizeof(char *)) ??

Finally, I want to print each of the strings using printf. How do I access
say the 30th line?

I have sucessfully done this a few times with doubles, ints etc, but now
with strings it seems a 'two dimesional' problem and I'm confused with the
pointer aspect of it.

Can anyone start me off on a simple solution?

Thanks
Ian

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.556 / Virus Database: 348 - Release Date: 26/12/2003

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.556 / Virus Database: 348 - Release Date: 26/12/2003
Nov 14 '05 #5
Thanks a lot for your help. I have a basic idea of whats going on. Here is
my test program. The principle has been integrated into my main program and
is working.
#include<stdio.h>
#include<stdlib.h>

int main(void)
{
char (*string)[50];
int i,lines;

lines=5;
string=malloc(lines* sizeof(*string));
for(i=0;i<lines;i++)scanf("%s",string[i]);
for(i=0;i<lines;i++)printf("%s\n",string[i]);
free(string);
return 0;
}

Thanks,
Ian

"Ian Todd" <ha********@REMOVEyahooME.com> wrote in message
news:q5******************@newsfep1-win.server.ntli.net...
Hi,
I am trying to read in a list of data from a file. Each line has a string in its first column. This is what i want to read. I could start by saying
char[1000][] to read in 1000 lines to the array( i think!!).

But I want to use malloc. Each string is at most 50 characters long, and
there may be zero to thousands of lines. How do I actually start the array? I have seen char **array etc. At first I tried char *array[50] but I think
that gives 50 pointers to chars :( .How do I use malloc to set space for the array once I know how many lines there are? is it
array=malloc(lines*sizeof(char *)) ??

Finally, I want to print each of the strings using printf. How do I access
say the 30th line?

I have sucessfully done this a few times with doubles, ints etc, but now
with strings it seems a 'two dimesional' problem and I'm confused with the
pointer aspect of it.

Can anyone start me off on a simple solution?

Thanks
Ian

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.556 / Virus Database: 348 - Release Date: 26/12/2003

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.556 / Virus Database: 348 - Release Date: 26/12/2003


Nov 14 '05 #6
Ian Todd wrote:

I am trying to read in a list of data from a file. Each line has
a string in its first column. This is what i want to read. I could
start by saying char[1000][] to read in 1000 lines to the array( i
think!!).

But I want to use malloc. Each string is at most 50 characters
long, and there may be zero to thousands of lines. How do I
actually start the array? I have seen char **array etc. At first
I tried char *array[50] but I think that gives 50 pointers to
chars :( .How do I use malloc to set space for the array once I
know how many lines there are? is it
array=malloc(lines*sizeof(char *)) ??

Finally, I want to print each of the strings using printf. How do
I access say the 30th line?


Examine the freverse.c application example included with
ggets.zip, and I think your questions will be answered. You can
find ggets.zip at:

<http://cbfalconer.home.att.net/download/>

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #7


Ian Todd wrote:
Hi,
I am trying to read in a list of data from a file. Each line has a string in
its first column. This is what i want to read. I could start by saying
char[1000][] to read in 1000 lines to the array( i think!!).

But I want to use malloc. Each string is at most 50 characters long, and
there may be zero to thousands of lines. How do I actually start the array?
I have seen char **array etc. At first I tried char *array[50] but I think
that gives 50 pointers to chars :( .How do I use malloc to set space for the
array once I know how many lines there are? is it
array=malloc(lines*sizeof(char *)) ??

Finally, I want to print each of the strings using printf. How do I access
say the 30th line?

I have sucessfully done this a few times with doubles, ints etc, but now
with strings it seems a 'two dimesional' problem and I'm confused with the
pointer aspect of it.

Can anyone start me off on a simple solution?


You can use a char ** variable. Use functions realloc and
malloc to add the strings. Use a integer variable to keep
track on the number of strings you have allocated.

A good solution is to create a data structure for the array
of strings and the count. Write functions to managed this data
type.

A simple example:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define FNAME "test.txt" /* File name goes here */

typedef struct StrArray
{
char **data;
size_t count;
} StrArray;

char *AddStrArray(StrArray *p, const char *s);
void PrintStrArray(StrArray *p);
void FreeStrArray(StrArray *p);

int main(void)
{
StrArray myfile = {NULL,0};
char *s,buf[156];
FILE *fp;

fp = fopen( FNAME , "r");
if(fp)
{
while(fgets(buf,sizeof buf, fp))
{
if((s = strrchr(buf,'\n')) != NULL)
*s = '\0';
if(!AddStrArray(&myfile,buf))
{
puts("Allocation failure");
break;
}
}
fclose(fp);
}
else perror("Unable to open file");
if(myfile.count >= 30)
printf("Line 30 is \"%s\"\n",myfile.data[29]);
/* PrintStrArray(&myfile); */
FreeStrArray(&myfile);
return 0;
}

char *AddStrArray(StrArray *p, const char *s)
{
char **tmp;

if((tmp = realloc(p->data,(sizeof *tmp)*(p->count+1)))==NULL)
return NULL;
p->data = tmp;
if((p->data[p->count] = malloc(strlen(s)+1)) == NULL)
return NULL;
return strcpy(p->data[p->count++],s);
}

void PrintStrArray(StrArray *p)
{
size_t i;

for(i = 0; i < p->count; i++)
puts(p->data[i]);
return;
}

void FreeStrArray(StrArray *p)
{
size_t i;

for(i = 0; i < p->count; i++)
free(p->data[i]);
free(p->data);
p->data = NULL;
p->count = 0;
return;
}

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #8
On Mon, 29 Dec 2003 13:35:50 -0000, "Ian Todd"
<ha********@REMOVEyahooME.com> wrote:
Hi,
I am trying to read in a list of data from a file. Each line has a string in
its first column. This is what i want to read. I could start by saying
char[1000][] to read in 1000 lines to the array( i think!!).

But I want to use malloc. Each string is at most 50 characters long, and
there may be zero to thousands of lines. How do I actually start the array?
I have seen char **array etc. At first I tried char *array[50] but I think
that gives 50 pointers to chars :( .How do I use malloc to set space for the
array once I know how many lines there are? is it
array=malloc(lines*sizeof(char *)) ??

Finally, I want to print each of the strings using printf. How do I access
say the 30th line?

I have sucessfully done this a few times with doubles, ints etc, but now
with strings it seems a 'two dimesional' problem and I'm confused with the
pointer aspect of it.

Can anyone start me off on a simple solution?


"A string is a contiguous sequence of characters terminated by and
including the first null character" (n869, section 7.1.1, paragraph
1). While not always technically precise, we tend to refer to this
sequence as an array. You imply that you want to store the data in an
array of strings. So it is a 2-d situation as you surmise.

There are two popular solutions, an "array of pointers" and a "pointer
to an array." Even though they are significantly different, the two
share a common syntax in referring to the individual strings and to
the characters that make up the strings. While this common syntax
simplifies the language it also causes some pervasive confusion.

If T is an object type, then T* is type pointer to T. If we define
T* p;
then p is a pointer to T and when initialized with a non-NULL value
points to exactly one object of type T. However, we frequently allow
p to point to the first of many objects of type T, with the objects in
a contiguous sequence. We then treat p as if it were an array of T
and use p[i] to refer to the i-th object in the array.

For the "array of pointers" approach:

Define a pointer to pointer to char (char** pp;).
Allocate space for some number of pointers to char
(pp = malloc(N * sizeof *pp);). Note that pp now points to the first
on N pointers to char. Even though these pointers are currently
uninitialized, we can still talk of pp as an array of N pointers or
pointing to such an array.
Read the next string from the file into a buffer.
Determine the length of the string.
Allocate enough space to hold the string
(pp[i] = malloc(length);). Note that pp[i] now points to the first of
*length* char. Even though these char are currently uninitialized, we
can still talk of pp[i] as an array of *length* char or pointing to
such an array.
Copy the string from the buffer to the memory pointed to by pp[i]
or, almost equivalently, copy the string from the buffer to the array
pp[i].
Loop back and read the next string.

Things to note:

The size of each string is independent of the other strings.
If viewed in the common left justified tabular form, the right ends of
the strings would not line up. This is often referred to as a jagged
array.

The value in pp is an address.

The value at that address is another address. This value is
the first of N such addresses. You refer to any particular value with
the expression pp[i].

Each pp[i] is the address of a char. This char is the first
char in a contiguous sequence of char terminated by a null character.
Therefore, this sequence is a string. We can say pp[i] points to the
string. Furthermore, we can refer to any particularly character in
the i-th string with the expression pp[i][j].

If you ever determine that the number of strings exceeds N,
you can use realloc to cause pp to point to a larger area capable of
holding more pointers. This is why pp is sometimes referred to as a
dynamic 2-d array.

For the "pointer to an array" approach:

Determine the maximum length of any string (you said 50).
Define a pointer to an array of *maximum* char
(char (*pa)[50];). Note that this is truly a pointer to an array, not
a pointer to the first of a sequence that we are treating as an array.
When we say a char * points to an array or string, we are using verbal
shorthand to avoid the cumbersome expression in the previous sentence
and we are being a little imprecise. When we say pa points to an
array, we are being very precise.
Allocate space for some number of such arrays
(pa = malloc(N * sizeof *pa);). Note that pa now points to the first
of N objects. Even though these objects are uninitialized, we can
still talk of pa as an array of N object or as pointing to such an
array. The fact is that each object is an array of 50 char. So we
talk of pa as an array of N arrays of 50 char. We can use pa[i] to
refer to the i-th object so pa[i] is the i-th array of 50 char.
Read the next string from the file into a buffer.
Copy the string from the buffer to the array pa[i].
Loop back and read the next string.

Things to note.

Each string is housed in an array of 50 char.

The value in pa is an address.

The object at that address is an array of 50 char. This
object is the first of N such objects. You refer to any particular
array as pa[i]. You can refer to a particular character in this array
with pa[i][j].

If you ever determine that the number of strings exceeds N,
you can use realloc to cause pa to point to a larger area capable of
holding more arrays. This is why pa is sometimes referred to as a
dynamic 2-d array. Unlike pp above which is dynamic in both
dimensions, pa is dynamic only in the first dimension. The second
dimension is fixed (at 50 in this example).
<<Remove the del for email>>
Nov 14 '05 #9
Barry Schwarz wrote:
"A string is a contiguous sequence of characters terminated by and
including the first null character" (n869, section 7.1.1, paragraph
1). While not always technically precise, we tend to refer to this
sequence as an array. The size of each string is independent of the other strings.


One of the distinctions between strings and arrays,
is that strings have lengths, objects have sizes.

--
pete
Nov 14 '05 #10
Ian Todd wrote:

Thanks a lot for your help. I have a basic idea of whats going on. Here is
my test program. The principle has been integrated into my main program and
is working.
#include<stdio.h>
#include<stdlib.h>

int main(void)
{
char (*string)[50];
int i,lines;

lines=5;
string=malloc(lines* sizeof(*string));
You neglected to ensure that the return value of malloc
was not NULL, before using it. That's bad.
for(i=0;i<lines;i++)scanf("%s",string[i]);
for(i=0;i<lines;i++)printf("%s\n",string[i]);
free(string);
return 0;
}


--
pete
Nov 14 '05 #11

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

Similar topics

4
by: Robert | last post by:
Hi, How can i resize an array of strings to add more? This is an example i just downloaded from a website: char *cpArray; int i; for (i = 0; i < 10; i++) cpArray = (char *)malloc(20 *...
2
by: hokieghal99 | last post by:
I wish to place all files and directories that are within a user defined path (on a Linux x86 PC) into some type of array and then examine those items for the existence of certain charaters such as...
9
by: Colin Doig | last post by:
Hi, I need to pass an array of strings to a function, but I can't get it to work. This is what I wrote : #include <stdio.h> void a_function(char *blah) { printf("%s %s %s", blah, blah,...
6
by: Ruben | last post by:
I'm trying to pass an array of string to a function without knowing how many strings I have beforehand. I've defined one functions as char * insert(char table,int cols, char values); out of...
5
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I...
1
by: ketema | last post by:
Hello, I was wondering if someone could help me with a function I am trying to write. The purpose of the function is to read in text from a file in the following format: FIRSTNAME LASTNAME...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
6
by: bwaichu | last post by:
Is my understanding of the allocation of these correct? I used fixed sized allocations for the example below, so I realize there is some optimization that can be done to those. I would like to...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.