Hello. I have a problem with an array of strings. I am scanning a string in a loop. I'm then assigning that string to an array of strings. The problem is, it just assigns a pointer. So when I try to look at the array later, It only displays the last modified string. So I need a way to assign the characters from a string to the array.
ALSO, when I try to read in DIRECTLY to my array of strings, I get a "segmentation fault" error. You can see it is commented out on my code, but feel free to uncomment it and try.
This could have something to do with my declaration, or the way I use my pointers. Pointers are confusing to me. Anyway, thanks for your help
Source:
Code:
#include <stdio.h>
#define MAXLETTERS 10
#define MAXARRAYS 5
int main(void)
{
char array[MAXLETTERS];
char *list[MAXARRAYS];
int i;
for (i = 0; i < MAXARRAYS; i++)
{
scanf("%s",array);
//scanf("%s",list[i]); For some reason this doesn't work
list[i] = array; //This makes all arrays in the list point to the final one i typed in
printf("\narray: %s",array); //Prints the array we input
printf("\nlist %d: %s\n",i,list[i]); //Shows this array has been correctly copied
}
for (i = 0; i < MAXARRAYS; i++)
{
printf("%s\n",list[i]);
}
return 0;
}