0

My program reads words from a file, and stores them in a dynamically allocated array as a number of words.

My problem is while in the while loop when I print the array, it seems to pointing to the correct word. After the code falls through the while loop, when I print all the indicies, 'ice' is the last word, and I am trying to find out why.

FILE *fileptr=fopen("file.txt","r");

char** DataArray;
int num_of_words=0;

char str[10];

while(fscanf(fileptr,"%s",&str)!=-1)
{
    num_of_words++;
}

DataArray=(char**)malloc(num_of_words*sizeof(char*));

rewind(fileptr);

int i=0;

while(fscanf(fileptr,"%s",&str)!=-1)
{
    printf("%s",str);
    int len=strlen(str);
    printf("\t%d",len);

    DataArray[i]=(char*)malloc(len*sizeof(char));
    DataArray[i]=str;
    printf("\t%s\n",DataArray[i]);
    i++;
}

printf("\n");
//printf("%s\n",*(DataArray+2));
printf("%s\n",DataArray[0]);
printf("%s\n",DataArray[1]);
printf("%s\n",DataArray[2]);


fclose(fileptr);

Output:

apple  5   apple
mango  5   mango
ice  3   ice

ice
ice
ice
octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
Nilayan
  • 15
  • 2

2 Answers2

1

It's not enough to assign a pointer. Especially when str takes on a new value each time through the loop.

DataArray[i]=(char*)malloc(len*sizeof(char));
DataArray[i]=str;
printf("\t%s\n",DataArray[i]);
i++;

You have to use strcpy the way you have written this program, because you have already allocated space using malloc. You can also use strdup, but that creates the dynamic storage for you. If it were up to me, I'd write it the way you have written it, using malloc first, and then

strcpy(DataArray[i], str);

octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
0

DataArray[i] = malloc(len); In this expression, malloc allocates len bytes and returns a pointer to those bytes. That pointer is assigned to DataArray[i].

DataArray[i] = str; This expression assigns a pointer to str's first byte to DataArray[i], which isn't what you expect it to do. Remember that pointer that you're getting from malloc? Where has it gone?

I suggest using the strcpy function, since it's designed to copy a string from one array to another:

DataArray[i] = malloc(len + 1); // remember to allow space for a '\0' terminator!
strcpy(DataArray[i], str);

ps. Don't cast the return value of malloc, and don't multiply by sizeof (char). There's no point to either of these, except to make your code more difficult to read. This is a commonly asked question, which is answered by most books. Which book are you reading?

autistic
  • 1
  • 3
  • 35
  • 80
  • Why was this downvoted? What's wrong with it? We're just teaching basic C, which gets confusing when you're first learning it, especially thinking you can assign a pointer, and the string gets copied. This is probably another drive-by down vote. – octopusgrabbus Mar 10 '13 at 20:32
  • I was wondering the same thing. Thanks for asking for me. Is there anything I can do to improve my answer, mysterious down-voter? – autistic Mar 10 '13 at 20:33
  • yes thank u, both of them seem unnecessary ; will keep them in mind. Book : Problem solving & Program Design in C, Hanly, Jeri R and Elliot.B Koffman – Nilayan Mar 10 '13 at 20:42
  • @Nilayan Very well. What is `strlen("What")`? How many characters does "What" require? You can find the answer to both of these questions, and your original post, in chapter 8. How many characters does `malloc(strlen("What"));` allocate? How many characters are needed to store "What" as a string? – autistic Mar 10 '13 at 20:53