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

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 3089
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Yang Song | last post by:
HI, I am a little confused about char * and char. How would I be able to return a char* created in a function? Is new() the only way? How would I be able to return a point and a value at the same...
24
by: Norm | last post by:
Could someone explain for me what is happening here? char *string; string = new char; string = "This is the string"; cout << string << std::endl; The output contains (This the string) even...
9
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my...
22
by: Simon | last post by:
Hi, I have written a function to trim char *, but I have been told that my way could be dangerous and that I should use memmove(...) instead. but I am not sure why my code could be 'dangerous'...
4
by: Radde | last post by:
Hi, class String { public: String(char* str); void operator char*();
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
8
by: andrew.fabbro | last post by:
In a different newsgroup, I was told that a function I'd written that looked like this: void myfunc (char * somestring_ptr) should instead be void myfunc (const char * somestring_ptr) ...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
20
by: liujiaping | last post by:
I'm confused about the program below: int main(int argc, char* argv) { char str1 = "abc"; char str2 = "abc"; const char str3 = "abc"; const char str4 = "abc"; const char* str5 = "abc";
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.