char *string

  • Thread starter =?ISO-8859-1?Q?Andreas_M=FCller?=
  • Start date
?

=?ISO-8859-1?Q?Andreas_M=FCller?=

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=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
 
J

John Fullman

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 = new char[length + 1];
strcpy(string, inputBuffer);
++i;
}

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

Jim Langston

Andreas Müller said:
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=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 = new char[40];
read(STDIN_FILENO, string, 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.
 
G

Greg

Andreas said:
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=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
 

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

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top