Hey guys.
Im experiencing problems trying to parse a char array as a pointer to a function i have defined in my code. This code is being written purely for self learning purposes though the wall i have hit is driving me nuts. Ive checked out the tutorials and i cant seem to find an answer to this unless ive overlooked it. Im using the gcc (c99) compiler.
heres the code i have so far:
Code:
#include <stdio.h>
#include <string.h>
void read_string(char *);
int main(int argc, char *argv[])
{
char forename[30], surname[30], banner_id[9], programme[50];
char a = 'a';
int stage = 0;
printf("Please type your forename:\n");
//scanf("%30s", &forename);
read_string( &forename);
printf("Please type your surname:\n");
scanf("%30s", &surname);
printf("Please type your banner ID:\n");
scanf("%9s", &banner_id);
printf("Please type your programme of study:\n");
scanf("%50s", &programme);
printf("Please type your year of study:\n");
scanf("%d", &stage);
printf("\n%s %s %s\n%s\n%d\n\n", forename, surname, banner_id, programme, stage);
return 0;
}
void read_string(char *l_string)
{
/* char character;
int index;
for (index = 0; index < (sizeof(l_string) * sizeof(char)); index++)
{
scanf("%c", &character);
if (character == EOF)
{
return;
}
else
{
l_string[index] = character;
}
}
return;
*/}
Originally i had all my inputs as scanf, then i encountered a problem whereby if i entered spaces it messed everything up, i dont see this as a problem because while exploring other options i encountered something i find even more mind boggling.
I get the compiler error of:
Originally Posted by
C99 Compiler
gcc -o q2/main q2/main.c
q2/main.c: In function ‘main’:
q2/main.c:34: warning: passing argument 1 of ‘read_string’ from incompatible pointer type
So i have tried the following corrections to no avail: (prototype first then implementation)
void read_string(char *);
void read_string(char *l_string)
void read_string(char *[]);
void read_string(char *l_string[])
void read_string(char *);
void read_string(char *l_string[])
void read_string(char[] *);
void read_string(char *l_string[])
none of which seem to work. So question is, how do i pass in my char array as a pointer and not get these errors?
the test call for the function is the line: read_string( &forename);
(of course the function is commented out, i was trying to reduce the possibilities of what was causing this problem)
Thanks in advance for any help.