简体   繁体   English

通过函数c传递指针

[英]passing a pointer through a function c

I'm trying to exit a while loop within a function but I don't think i'm writing the pointer correctly, c is new to me and reading examples online is confusing. 我试图退出一个函数中的while循环,但我认为我没有正确编写指针,c对我来说是新的,在线阅读示例令人困惑。

I would appreciate feed back on what 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 you like to 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 my code and fixed a lot of the errors you guys helped my identify. 我已经更新了代码,并修复了许多人帮助我识别的错误。

now for some reason it goes through my logon loop twice if i hit a key neither 'y' or 'n' 现在由于某种原因,如果我没有按下“ y”或“ n”键,它将经历两次登录循环

this is what my 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 you like to 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 this is what it is out putting when i dont hit 'y' or 'n' 这就是当我不打'y'或'n'时输出的东西

 would you like to log on (y/n): e

 Error entering command
 would you like to log on (y/n): 
 Error entering command
 would you like to log on (y/n): 

while(log == 1) it's == not = . while(log == 1)== not =

When in a while condition you write log = 1 it simply assigns 1 to log . 在一段时间内写入log = 1它只是将1分配给log Then while gets 1 from the variable log , which is implicitly converted to true , which makes while loop infinitely. 然后while从变量log获取1 ,该变量被隐式转换为true ,这使得while无限循环。

The parameters of your logOn function must be defined as so: logOn函数的参数必须定义为:

int logOn(int *par){//function body}

Also the syntax of your while condition must be as follows: 而且while条件的语法必须如下:

while(log == 1)

Note the double == sign. 请注意双==符号。 This is necessary as a single = sign will result in an infinite loop. 这是必要的,因为单个=符号将导致无限循环。

Furthermore when you pass in the address of a variable into a function as you have, then a * operator is required in front of it within the function body in order to deference the value at that address. 此外,当您将变量的地址按原样传递给函数时,函数主体内在其前面需要*运算符,以便引用该地址处的值。

eg 例如

int logOn(int *par)
{
    printf("%p", par); //address par 
    printf("%d",*par); //value stored at address par
}

Hope this helps. 希望这可以帮助。

while(log = 1)
while(log = 2)

Wrong. 错误。 In c, the = operator is for assignment, not comparison. 在c中, =运算符用于赋值,而不是比较。 You are setting log equal to 1 and 2 there. 您正在此处设置日志等于1和2。 You want the == operator for comparison: 您需要==运算符进行比较:

while(log == 1)

Next: 下一个:

scanf("%s", &p);

Wrong. 错误。 %s is for arrays of char ending in zero ('\\0'). %s用于以零('\\ 0')结尾的char数组。 This is how c represents strings. 这就是c表示字符串的方式。 For a single character, you want 对于单个字符,您想要

scanf("%c", &p);

Next: 下一个:

if(p="y")

You are misusing the assignment operator again. 您再次滥用赋值运算符。 In addition, you are comparing a p , a char to "y" , a char * - char array. 另外,您正在将一个p ,一个char与一个"y" ,一个char * -char数组进行比较。 Double quotes are for strings, single quotes/apostrophes are for single characters. 双引号用于字符串,单引号/撇号用于单个字符。 You are looking for 你在找

if(p == 'y')

Next: 下一个:

else if(p="n");

Same errors as above, plus, the semicolon - ; 与上述相同的错误,加上分号- ; doesn't belong there. 不属于那里。 Suffice it to say, that should cause a compilation error on your else statement below. 可以这样说,这将在下面的else语句上导致编译错误。 You want: 你要:

else if(p == 'n')

Next: 下一个:

par = 2;
par = 3;

You are assigning integers to a pointer. 您正在将整数分配给指针。 You probably mean 你可能是说

*par = 2;
*par = 3;

Next: 下一个:

int logOn(log);

should be 应该

(void) logOn(&log);

Next: 下一个:

int mainMenu();

I don't know what mainMenu is, but the int keyword makes no sense there. 我不知道mainMenu是什么,但是int关键字在那里毫无意义。

Some points in your program . 程序中的一些要点。

First, while(log = 1) , since your are checking if the log is equal to 1 or not, the syntax should be while(log == 1) { } 首先, while(log = 1) ,因为您正在检查log是否等于1,所以语法应为while(log == 1) {}

Secondly, while calling the logOn function, pass the address of log variable ( int logOn(&log) ;) 其次,在调用logOn函数时,传递日志变量的地址( int logOn(&log) ;)

Third, The signature of the int logOn(*par) function should be int logOn(int *par) . 第三, int logOn(*par)函数的签名应为int logOn(int *par)

Fourth, "y" or "n" represents a string literal, if you want to use only a single character, use 'y' or 'n', that is with single quote. 第四, "y""n"表示字符串文字,如果您只想使用单个字符,请使用“ y”或“ n”,即带单引号。

Fifth, To change the value of a pointer use the de-referencing operator * . 第五,要更改指针的值,请使用解引用运算符* Your line in the code should be 您在代码中的行应为

*par = 2;  (//Content of par equal to 2)

*par = 3;

Happy programming. 编程愉快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM