passing a pointer through a function c -
i'm trying exit while loop within function don't think i'm writing pointer correctly, c new me , reading examples online confusing.
i appreciate feed on i'm writing incorrectly.
int logon(int *log); int main(void) { int log; log = 1; while(log = 1) { int logon(log); } while(log = 2) { int mainmenu(); } printf("\ngood bye"); } int logon(*par) { char p; printf("would log on (y/n): "); scanf("%s", &p); if(p="y") { par = 2; } else if(p="n"); { par = 3; } else { printf("\nerror entering command\n") } return 0; }
i've updated code , fixed lot of errors guys helped identify.
now reason goes through logon loop twice if hit key neither 'y' or 'n'
this current code looks like
include
int logon(int *log); int mainmenu(int *log); int main(void) { int log; log = 1; while(log == 1) { (void) logon(&log); } while(log == 2) { mainmenu(&log); } printf("\ngood bye"); } int logon(int *par) { char p; printf("would log on (y/n): "); scanf("%c", &p); if(p == 'y') { *par = 2; } else if(p == 'n') { *par = 3; } else { printf("\nerror entering command\n"); } return 1; } int mainmenu(int *par) { printf("\nmain menu\n"); *par = 3; return 0; }
and out putting when dont hit 'y' or 'n'
log on (y/n): e error entering command log on (y/n): error entering command log on (y/n):
while(log = 1) while(log = 2)
wrong. in c, =
operator assignment, not comparison. setting log equal 1 , 2 there. want ==
operator comparison:
while(log == 1)
next:
scanf("%s", &p);
wrong. %s
arrays of char
ending in 0 ('\0'). how c represents strings. single character, want
scanf("%c", &p);
next:
if(p="y")
you misusing assignment operator again. in addition, comparing p
, char
"y"
, char *
- char array. double quotes strings, single quotes/apostrophes single characters. looking for
if(p == 'y')
next:
else if(p="n");
same errors above, plus, semicolon - ;
doesn't belong there. suffice say, should cause compilation error on else
statement below. want:
else if(p == 'n')
next:
par = 2; par = 3;
you assigning integers pointer. mean
*par = 2; *par = 3;
next:
int logon(log);
should be
(void) logon(&log);
next:
int mainmenu();
i don't know mainmenu
is, int
keyword makes no sense there.
Comments
Post a Comment