So I'm sure this question is answered many times already but I am having trouble seeing how to fix my situation. I took a snippet of my program that contains my warning-generating code:
#include <stdio.h>
#include <stdlib.h>
inputData(int size, char *storage[size])
{
int iterator = -1;
do {
iterator++;
*storage[iterator] = getchar();
} while (*storage[iterator] != '\n' && iterator < size);
}
main()
{
char name[30];
inputData(30, name);
}
The warning message:
$ gcc text.c text.c: In function ‘main’: text.c:18:5: warning: passing argument 2 of ‘inputData’ from incompatible pointer type [enabled by default] inputData(30, name); ^ text.c:4:1: note: expected ‘char **’ but argument is of type ‘char *’ inputData(int size, char *storage[size])
EDIT:
Ok, so I managed to play around with some syntax and fixed my problem. I still wouldn;t mind hearing from somebody more knowledgeable than I why precisely this was needed. Here is what I changed:
#include <stdio.h>
#include <stdlib.h>
inputData(int size, char *storage) // changed to point to the string itself
{
int iterator = -1;
do {
iterator++;
storage[iterator] = getchar(); // changed from pointer to string
} while (storage[iterator] != '\n'); // same as above
}
main()
{
char name[30];
inputData(30, name);
printf("%s", name); // added for verification
}
inputData()
, code should have 3 reasons to stop: 1)getchar()
returned'\n'
2)getchar()
returnedEOF
3) No more room. – chux - Reinstate Monica