469,641 Members | 1,187 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,641 developers. It's quick & easy.

char *string

hi @all,

I have a char* string[10] array and i want to fill it up in a while
loop:
while(i<3){
length = read(STDIN_FILENO, inputBuffer, sizeof(inputBuffer);
...
}

after this the array should look like this:
string[0]=the first entry
string[1]=the 2nd entry
string[2]=the third entry

string[i]=inputBuffer works for the 1st entry but not for the 2nd,
is this because of the null-terminated strings?

Can anybody help me?
thanks
Andreas
Sep 17 '05 #1
3 2790
It's hard to say when not all of the code in question is present, but
it sounds to me like you are using the same buffer space for each
string.

For each of the 3 strings, you must move it off of the buffer using new
char[len+1] and strcpy()...

char inputBuffer[255];

while(i<3){
length = read(STDIN_FILENO, inputBuffer, sizeof(inputBuffer);
string[i] = new char[length + 1];
strcpy(string[i], inputBuffer);
++i;
}

Don't forget to delete when you're all done.

Sep 17 '05 #2
"Andreas Müller" <ma**************************@amueller.org> wrote in
message news:dg**********@newsserv.zdv.uni-tuebingen.de...
hi @all,

I have a char* string[10] array and i want to fill it up in a while
loop:
while(i<3){
length = read(STDIN_FILENO, inputBuffer, sizeof(inputBuffer);
...
}

after this the array should look like this:
string[0]=the first entry
string[1]=the 2nd entry
string[2]=the third entry

string[i]=inputBuffer works for the 1st entry but not for the 2nd,
is this because of the null-terminated strings?

Can anybody help me?
thanks
Andreas
Please post all your code.
char* string[10]; gives you an array of 10 character pointers. That's all the memory that's
allocated, the memory to hold 10 character pointers.
while(i<3){
length = read(STDIN_FILENO, inputBuffer, sizeof(inputBuffer);


Where have you allocated memory for the inputBuffer? If you want your input
buffer to be your string array allocate memory for it.

int i = 0;
while (i < 3)
{
string[i] = new char[40];
read(STDIN_FILENO, string[i], 40); // I don't know read's parms, just
copying yours.
++i;
};

I wouldn't necessarily do it that way, I'm just following the same type of
structure your original code gave.

Sep 17 '05 #3
Andreas Müller wrote:
hi @all,

I have a char* string[10] array and i want to fill it up in a while
loop:
while(i<3){
length = read(STDIN_FILENO, inputBuffer, sizeof(inputBuffer);
...
}

after this the array should look like this:
string[0]=the first entry
string[1]=the 2nd entry
string[2]=the third entry

string[i]=inputBuffer works for the 1st entry but not for the 2nd,
is this because of the null-terminated strings?

Can anybody help me?
thanks
Andreas


I think using C++ instead of C here would probably provide the most
help. One of the key advantages of using C++ streams and buffer classes
is that they take care of much of the memory management of I/O
operations.

The benefit is not just greater convenience. While it is true that the
C++ implementation is not only easier to write (because there is less
code that needs to be written), the real advantage lies in the greater
security and reliabilty provided by a standard implementation instead
of the majority of one-off, do-it-yourself I/O implementations that are
often susceptible to buffer overflows, off-by-one errors and the like.

In this case, the data structures appear to be easily convertible: a
std::vector for the string array, std::string for the individual
strings, and an std::fstream from which to read them. Of course putting
them all together is the fun part, so I won't spoil it with sample
code.

Greg

Sep 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Yang Song | last post: by
24 posts views Thread by Norm | last post: by
9 posts views Thread by Christopher Benson-Manica | last post: by
22 posts views Thread by Simon | last post: by
4 posts views Thread by Radde | last post: by
2 posts views Thread by Peter Nilsson | last post: by
5 posts views Thread by jab3 | last post: by
8 posts views Thread by andrew.fabbro | last post: by
18 posts views Thread by Pedro Pinto | last post: by
20 posts views Thread by liujiaping | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.