Hi,

I read in data from a file something like this in my main function

text text
texttext texxttet
texet

char **array;

ifp = fopen (filename.txt , "r");

then I allocated some memory using malloc

for(i = 0; i < 3; i++)
   {
      array[i] = malloc(50 * sizeof(char));
   }

then I have this

for(i = 0; i < 3 ; i++)
   {
      fgets(arrayString[i], 50 , ifp);
   }

The contents of the file will print fine in the int main but when Im passing it in other functions I can only print the first line of the input file. For example this would print fine in int main()

for (i = 0; i < 3; i++)
   {
      printf("%s", arrayString[i]);
   }

but when I pass this array in second function like this

PrintString(*array);

with this prototype

void PrintString(char *pArray)

When printing it in above function with code below I only get to print first line from the input file and not rest of them

for (i = 0; i < 3; i++)
   {
      printf("%d -  %5s", i, pArray);
   }

this code will show
text text
text text
text text

What am I doing wrong?

>>PrintString(*array);
That is not an array of strings like you originally declared because it only has one asterisk. All that takes is one character array. If you want it to print all the strings then you need it like this: printString(char **array); or this printString( char* array[]);

commented: thanks for the help +1

Make the prototype: void PrintString(char **pArray) add an extra *

Call the function as: PrintString(*array); remove the *


Display each subscript of the pointer: printf("%d - %5s", i, pArray[i] ); add the individual pointer subscript

commented: thanks +1

Thanks for the help. It worked!

Now Im trying to pass the read string into another function needs to check if its palindrome and count how many vowels are in it.

would I pass it in like this for string 3

PalindromeCheck(array[2])

And how should I check if string is palindrome. I just need some hints
thanks

And how should I check if string is palindrome. I just need some hints
thanks

Searching...

Thank you.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.