First of all, buf points no where. So when you send it to fgets, it will try to modify some random memory location --> bad. So you need to initialize it:
... because after all, you're only getting LIM elements. And btw, fgets will automatically close off your string with a NULL so that line after the fgets is redundant and useless.
The reason you are getting the same thing 10 times is because all the pointers in star[] point to the same thing: buf. What you need to do is allocate a new buffer everytime or start off with a fixed-length buffer.
Method #1: Fixed-length buffer:
Code:
void getstrings(char star[MAX][LIM])
{
int count;
for (count = 0; count < MAX; count++)
{
fgets(star[count], LIM, stdin);
}
for (count = 0; count < MAX; count++)
{
puts(star[count]);
}
}
That require modifying your client code.
Method #2: Allocating a new buffer everytime:
Code:
void getstrings(char * star[MAX])
{
int count;
char buf[LIM];
for (count = 0; count < MAX; count++)
{
fgets(buf, LIM, stdin);
star[count] = (char *)malloc(LIM);
strcpy(star[count], buf);
}
for (count = 0; count < MAX; count++)
{
puts(star[count]);
}
}
// ... somewhere else in your code ...
{
getstrings(array);
// do something with it
// now free all the memory
for (count = 0; count < MAX; count++)
{
free(star[count]);
}
}