How to determine number of lines in char **

P

paktsardines

Dear all,

As of yesterday I have this function:

char ** lines = read_file("filename.txt");


Now, I want to print the contents of lines. A first attempt:

int i=0;
while (lines) {
printf("%s\n",lines);
i++;
}

Seg faults after printing all lines because it tries printing beyond
the end of the array.

How can I determine how many variable-length lines are in the array?

The only way I can think of is to modify readfile so it can be called
as:

int numlines=read_file("filename.txt", lines);


Is there another/better way?

Thanks for any tips,

Pakt.
 
I

Ian Collins

Dear all,

As of yesterday I have this function:

char ** lines = read_file("filename.txt");


Now, I want to print the contents of lines. A first attempt:

int i=0;
while (lines) {
printf("%s\n",lines);
i++;
}

Seg faults after printing all lines because it tries printing beyond
the end of the array.

How can I determine how many variable-length lines are in the array?

Stick a null pointer at the end of the array of lines.
 
P

paktsardines

Dear all,
As of yesterday I have this function:
char ** lines = read_file("filename.txt");
Now, I want to print the contents of lines. A first attempt:
int i=0;
while (lines) {
printf("%s\n",lines);
i++;
}

Seg faults after printing all lines because it tries printing beyond
the end of the array.
How can I determine how many variable-length lines are in the array?

Stick a null pointer at the end of the array of lines.


Brilliant! Thanks again!
 
C

CBFalconer

Ian said:
(e-mail address removed) wrote:
.... snip ...

Stick a null pointer at the end of the array of lines.

This requires that an empty line be defined as something other than
a NULL pointer. It may be an empty string.
 
I

Ian Collins

CBFalconer said:
This requires that an empty line be defined as something other than
a NULL pointer. It may be an empty string.
Which in this context would be a pointer to a '\0'.
 
K

Keith Thompson

CBFalconer said:
This requires that an empty line be defined as something other than
a NULL pointer. It may be an empty string.

Of course. Why would you even consider representing an empty line as
a null pointer?
 
B

Ben Bacarisse

CBFalconer said:
Because it requires no new storage space.

Neither, in effect, does an empty string. Of course, that would
unnecessarily complicate the usage of the array of strings, but that also
happens when it contains a mixture of strings and nulls.
 
C

CBFalconer

Ben said:
Neither, in effect, does an empty string. Of course, that would
unnecessarily complicate the usage of the array of strings, but
that also happens when it contains a mixture of strings and nulls.

Yes it does, and it may be relatively major. When assigned via
malloc, the space needs to be suitably aligned for all types. This
often means assignment in blocks of 8 or 16 bytes. Thus that one
byte terminator really eats up 16, plus the overhead involved in
keeping track of the storage, etc.
 
B

Ben Bacarisse

CBFalconer said:
Yes it does, and it may be relatively major. When assigned via
malloc, the space needs to be suitably aligned for all types. This
often means assignment in blocks of 8 or 16 bytes. Thus that one
byte terminator really eats up 16, plus the overhead involved in
keeping track of the storage, etc.

That is not what I had in mind. It is possible to indicate empty
lines by using a pointer to the same, statically allocated, string.

I am not advocating doing this in anything but the oddest of
situations (space being very tight and the usage of the pointers being
such that a shared empty string is more convenient than repeatedly
checking for NULL).

The trouble with using NULL, as you know, is that is complicates every
use of very line. At least with a shared empty string all the lines
*are* strings.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top