简体   繁体   English

解引用从另一个函数传递给main()的指针

[英]De-Referencing a pointer passed from another function to main()

I'm trying to use a separate function to input data using scanf() (outside of main ). 我正在尝试使用单独的函数通过scanf() (在main之外scanf()输入数据。 This new function is supposed to print a line and then receive input from the user. 该新功能应该先打印一条线,然后从用户那里接收输入。 However something appears to be going awry between the scanf in the function and the printf() function in the main that I am testing it with. 但是,在我用来测试的函数中的scanfmain函数中的printf()函数之间似乎出现了问题。

I believe that I am receiving a pointer from the function but certain compiler warning are making me wonder if my assumption about the pointer is even correct. 我相信我从函数中收到了一个指针,但是某些编译器警告使我怀疑我对指针的假设是否正确。

I am confused by the output of this code: 我对以下代码的输出感到困惑:

#include <stdio.h>
void set_Info(void);

int main()
{
int scanNum = 0;
set_Info();

printf("%d", &scanNum);
return 0;
}

void set_Info(void)          /* start of function definition */

{
int scanNum;

printf("Scan test, enter a number");
scanf("%d",&scanNum);
}

If I provide a number, say 2, the result of the printf statement in the main() is: 如果我提供一个数字,例如2,则main()中的printf语句的结果为:

2665560

Now, in so far as I am able to tell that output appears to me like a memory address so what i attempted to do to fix that is dereference the pointer in main like so : 现在,据我所知,输出对我来说就像是一个内存地址,所以我试图解决的问题是像这样取消引用main中的指针:

int scanNum = 0;
int scanNumHolder;

set_Info();

scanNumHolder = *scanNum;
printf("%d", &scanNumHolder);

I believe that this code makes scanNum variable to become assigned to the dereferenced value of scanNum . 我相信这段代码会使scanNum变量分配给scanNum的取消引用的值。 However I get the same output as above when I do this. 但是,当我执行此操作时,会得到与上述相同的输出。 Which leads me to believe one of two things. 这使我相信两件事之一。 Either that I am not correctly dereferencing scanNum , or that scanNum is not in fact a pointer at all in this situation. 在这种情况下,我要么没有正确地取消引用scanNum ,要么实际上scanNum根本不是指针。

The most common error I receive from the compiler is: 我从编译器收到的最常见错误是:

error: invalid type argument of unary ‘*’ (have ‘int’)

Which makes sense, I suppose, if I'm attempting to treat an int value as a pointer. 我想,如果我试图将一个int值当作一个指针,那是有道理的。

If it is the case that scanNum is not being dereferenced correctly, how can I achieve this? 如果出现scanNum未被正确取消引用的情况,我该如何实现?

Thank you for the help 感谢您的帮助

*Update *更新

Thanks for the help. 谢谢您的帮助。

Just to recap 回顾一下

My set_info function needs to be passed an address parameter. 我的set_info函数需要传递一个地址参数。 The reason an address parameter has to be used is because the local memory of a function is erased after the function call ends. 必须使用地址参数的原因是因为函数调用结束后会擦除函数的本地内存。 So in order to do work a variable declared in the main function, I pass the address of the variable in question so that when the function ends the changes are not lost. 因此,为了处理在主函数中声明的变量,我传递了有问题的变量的地址,以便在函数结束时不会丢失更改。

Inside the main function, when set_info is called with &scanNum as the argument, it passes a reference tp the variable so that it can be assigned the value generated by the scanf statement in the function. 在主函数内部,当使用&scanNum作为参数调用set_info时,它将给变量tp传递引用,以便可以为该变量分配由函数中scanf语句生成的值。

I realize that what I was doing wrong as correctly pointed out by the awesome people of SO, is that I am trying to call set_info like it returns a value but in fact changes the variable like I actually want. 我意识到,正如SO的出色人士正确指出的那样,我做错了什么,是我试图调用set_info就像它返回一个值,但实际上像我实际想要的那样更改了变量。

Thanks again for the help! 再次感谢您的帮助!

This function: 该功能:

void set_Info(void)
{
    int scanNum;
    scanf("%d", &scanNum);
}

reads the integral number from the standard input and stores it into scanNum variable, which is local variable with automatic storage duration that exists only within the scope of this function. 从标准输入中读取整数,并将其存储到scanNum变量中,该变量是具有自动存储持续时间的局部变量,仅在此函数的范围内存在。

And the body of your main : 和你的main

int scanNum = 0;
set_Info();

printf("%d", &scanNum);

defines a local variable called scanNum , then calls a set_Info() function which doesn't affect scanNum defined in main in any way and then it prints the address of scanNum variable. 定义被称为本地变量scanNum ,然后调用一个set_Info()不影响功能scanNum中所定义main以任何方式,然后将其打印的地址scanNum变量。


This is what you are trying to do: 这是您要执行的操作:

void set_Info(int* num)
{
    // read an integer and store it into int that num points to:
    scanf("%d", num);
}

int main()
{
    int scanNum = 0;
    // pass the address of scanNum to set_Info function so that
    // changes to scanNum are visible in the body of main as well:
    set_Info(&scanNum);
    printf("%d", scanNum);
    return 0;
}

I also recommend you spend more time reading some book with C basics before you'll continue programming :) 我还建议您在继续编程之前花更多时间阅读一些有关C基础的书:)

I would pass in the variable into your set_Info function, so that it knows where to save the data. 我会将变量传递到您的set_Info函数中,以便它知道将数据保存在何处。 This would then allow you to scan multiple values, and you would simple increment the pointer. 然后,这将允许您扫描多个值,并且可以简单地增加指针。 Be sure to pass the variable address into set_Info() using &variableName, since that function expects a pointer 确保使用&variableName将变量地址传递到set_Info()中,因为该函数需要一个指针

#include <stdio.h>
void set_Info(int *pScanNum);

int main()
{
    int scanNum = 0;
    set_Info(&scanNum);

    printf("%d", scanNum);
    return 0;
}


//Pass in the pointer to scanNum
void set_Info(int *pScanNum)
{
    printf("Scan test, enter a number");
    scanf("%d",pScanNum);
}

Get rid of your ampersand! 摆脱你的“&”号! Printf wants an integer not a pointer. Printf想要一个整数而不是一个指针。

printf("%d", scanNum);

And as liho said, you need to return scanNum from set_info so you can get at it outside of the function. 正如liho所说,您需要从set_info返回scanNum,以便可以在函数外部获取它。

int scanNum = set_Info();

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

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