I have been working with C for the first time in a long time and one of the biggest problems for me has been working with strings, since they aren't expressed as well as they are in Python.
From what I know and understand, a char *
is just a pointer to a string(or rather, the first character in a string). A char[]
is very similar and can be used the same way.
My first question is a little side question, but while we use it to execute the same things, is there a difference in correctness or how the compiler views it?
Going ahead, I know that char *[]
is just an array, but each element is a pointer of type char *
. So through that each element when deferenced/accessed would just return a string. Which is why char *argv[]
just takes values from command line.
For a problem that I was working on I needed a a 2D array of strings and had been trying to run it is char *[][]
and making function calls for it.
I have a function type defined as void runoff_function(candidates *, int a, int b,char * array[a][b]);
That expects a 2D array of character pointers.
My main function has a variable defined and populated as char* list[n][argc];
Except when running a loop to initialize user inputs:
char* list[n][argc];
for(int i=0;i<n;i++)
{
printf("Voter %d\n",(i+1));
for(int j=1;j<argc;j++)
{
printf("Rank %d\t",j);
scanf("%s",list[i][j-1]);
}
I get a seg fault after my first input and I don't know why.