2

I am trying to write a function that reads a text file and puts every words of this text into an array with dynamic allocation so that this array is not over-sized.

I managed to write a working function that creates an array with every words (i.e arr[0] = "bla", arr[1] = "bla", ...) but I can't return this array, so i can use it in my main().

char* toArray(char *filename) {
  int words = countWords(filename); // this function returns the number of words in the file
  char *arr[words]; 
  int cur_size = 0;
  int i;

  FILE *file;
  file = fopen(filename, "r");

  for(int i = 0; i < words; i++) {
    fscanf(file, "%*s%n", &cur_size);
    arr[i] = malloc( (cur_size+1) * sizeof(char));
  }
  fclose(file);

  file = fopen(filename, "r");
  for (i = 0; i < words; i++) {
    fscanf(file, "%s", arr[i]);
    printf("%s\n", arr[i]);
  }
  fclose(file);

  return arr; // not sure if this is how i should return it
}


int main(int argc, char const *argv[]) {
  char *arr = toArray("file.txt");
  printf("%s", arr[0]); 

  return 0;
}

I expected

 printf("%s", arr[0]) 

to print the first word of my file.

but instead i get this error : format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=] printf("%s\n", arr[0]);

I guess i made a mistake in my function ?

4
  • Even if you allocate the elements in the array arr dynamically, the array arr itself is not allocated dynamically. Returning it will return a pointer to a local variable, a local variable which will cease to exist immediately once the function returns. What's more, you have an array of pointers, which will never be equal to the type char *, but with dynamic allocation could be of the typechar **. Commented Sep 22, 2019 at 9:46
  • So, if i allocate dynamically my arr itself like this char *arr = malloc(sizeof(char) * numberOfChar)) it should be better ? Commented Sep 22, 2019 at 10:05
  • I allocated my memory to my arr like this now : char *arr = malloc(numberOfChar*sizeof(char)); but now i got issues with my elements allocation : arr[i] = malloc( (cur_size + 1) * sizeof(char)) assignement makes integer from pointer without a cast i don't know why this happens Commented Sep 22, 2019 at 10:21
  • What you seem to want is an array of strings. A "string" can be seen as achar *. And array can be seen as a pointer to its (first) element. So if you have an array of char * you need a pointer to char * which turns out to be a char **. Now for how to allocate it, you're part-ways there with the code you show in your question. Commented Sep 22, 2019 at 10:33

3 Answers 3

1

Your array is local, we need to declare it dynamically. Please try below sample code.

char **arr = NULL;
arr = (char**)malloc(words*sizeof(char*))
/* then use it */
return arr;
Sign up to request clarification or add additional context in comments.

2 Comments

I tried it but the return type of arr seems to be wrong. i got "return from incompatible pointer type return arr " should i return *arr instead ? since the array is composed of strings which are *char
you need to change function return type to char**
0

Thanks you all for your answers ! I managed to make it work.

I changed my function return type to char** I allocated memory dynamically to my array like this: arr = (char **)malloc(words*sizeof(char));

and then it worked in my main when I delcared char **arr = toArray("file.txt");

Comments

-1

Your code:

 char *arr = toArray("file.txt");

In this case, arr is a string, and not an array of strings! Therefore, arr[0] is just a single char, and not a string. And a single char is basically treated like an int.

3 Comments

This is only part of the problem. OP is attempting to return an array of strings, and fixing their main routine to handle one string will not accomplish that.
Entering text as answers is for answers, not hints.
thanks for your answer but i don't get where i make it wrong in my function. Is my array type definition the problem ? Or is it just the way i'm allocating memory ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.