views:

75

answers:

4

I'm trying to create a program which takes in a set number of strings (the user is asked to put in the number of strings they will enter), once it has these strings, they are placed in an array, using dynamic memory.

The ouput would be something like this:

# of Strings: 3
Cat
Dog
Elephant 

Cat
Dog
Elephant

Heres a snippet of my code, after I have the number of strings.

sptr=malloc(sizeof(char*)*nStrings);

for(i=0;i<nStrings;i++)
{
    scanf("%s",string);
    length=strlen(string);
    sptr[i]=malloc(sizeof(char)*length);
    sptr[i]=string;
}

Where sptr is the array I'll access to output the strings. So it's an array of pointers which then point to individual strings (or other arrays of characters, if you'd prefer to think of it that way).

Lets say there are two strings. I allocate memory for two pointers, Then in the first pointer, i scan in a string, i find the length of the string, i allocate memory the size of the string and i equal the pointer to the string. This all works dandy, and if I were to put a printf() right after that last line, it will work. The problem i face is, if lets say there are 3 strings, each time through sptr[i] is assigned correctly, but then outside of that block, all of the indicies of sptr are = to the last string i put in, and I have no idea why.

If you could help me out I'd appreciate it. Thanks.

A: 

strlen doesn't account for the zero termination, you need to add one. But mainly you need to copy the string into the memory you allocate.

ergosys
A: 

You need to allocate 1 character extra for the null terminator:

sptr[i]=malloc(sizeof(char)*(length+1));

Also, you need to copy the string into the newly allocated memory:

strcpy(sptr[i], string);
Thomas
A: 

There are 2 problems in your code: you don't allocate enough memory. Should be length + 1 because of the ending \0 in a string. Secondly you should use strcpy to copy the string to your allocated memory. Lookup the strdup function that handles both.

Maurits Rijk
+2  A: 
sptr=malloc(sizeof(char*)*nStrings);

for(i=0;i<nStrings;i++)
{
    scanf("%s",string);
    sptr[i]=strdup(string);
}

I assume the variable string has enough memory to keep the read strings.

The error occured because you set the pointer to point to the string variable.

George B.
Is it possible you could expand on your answer? I did manage to get it working, however I do not know what strdup is (or the library required). I used strcpy instead. The problem being, I do not understand WHY that resulted in an error. Isn't sptr[i] treated as a string? Thanks for the answer though, it did help.
Blackbinary
strdup is defined in string.hFunction prototype:char *strdup(const char *s);The statementsptr[i]=strdup(string);is equivalent tosptr[i]=malloc(sizeof(char) * (strlen(string)+1));strcpy(sptr[i],string);The reason why what you did did not work is that by assigning the variable string to sptr[i] you did assign the pointer that points to string to the sptr array cell.So what you did is make each cell of sptr point to string.But because the content of string did change after each iteration, after exiting the loop all variables had the same content, which is the last string you read.
George B.