I am trying to parse an empty array of string to a function where that function fills the array with some string data. Then use the array of strings outside that function.
Below is an example code of what I'm trying to do.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define READLENGTH 155
#define READLISTLENGTH 100
void fileReader(char *readBuffer[]) {
for (int i=0;i<100;i++) {
char buffer[50];
snprintf(buffer, 20, "hello world %d",i);
readBuffer[i] = &buffer;
}
}
int main()
{
char *readBuffer[READLISTLENGTH];
fileReader(readBuffer);
for(int i = 0; i < READLISTLENGTH; i++) {
printf("%s \n",readBuffer[i]);
}
}
The expected output is,
hello world 0
hello world 1
hello world 2
:
:
hello world 99
But what I get is,
hello world 99
hello world 99
hello world 99
:
:
hello world 99