I'm afraid I'm having some trouble understanding basic concepts of C, and would really like some help. My main issue is with passing an array of strings to a function. Here is an example program:
#include<stdio.h>
#include<string.h>
void examplefunction(char *poemstring)
{
printf(&poemstring[0]);
printf(&poemstring[1]);
printf(&poemstring[2]);
printf(&poemstring[3]);
}
int main()
{
char poemstring[4][8];
strcpy(poemstring[0],"oh\n");
strcpy(poemstring[1],"noetry\n");
strcpy(poemstring[2],"bad\n");
strcpy(poemstring[3],"poetry\n");
examplefunction(poemstring);
getch();
return 0;
}
which, when runs, produces the output:
oh
h
As I understand it, when I declare poemstring, I declare is as a array of size four of arrays of size eight that hold characters. I then use strcpy to copy the four lines of the poem to the elements of poemstring. This leaves poemstring as an array of size four, with each element an array of characters holding a line of the poem. I then pass this array of strings to the function example function.
As I understand it, although I passed poemstring, the compiler decides that it's only going to pass a pointer to poemstring, which is why I tell examplefunction to expect a pointer to be passed to it by using the asterisk. When I use the printf functions, however, I have to use the ampersand to tell it to look at where the pointer is pointing instead of looking at the pointer itself, and then I tell it to print each of the individual lines by using the square brackets.
I can't see how this is going wrong. Have I made a silly error in my code, am I misunderstanding something about C completely, or am I wrong in some other way?