简体   繁体   中英

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 ). 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.

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:

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 :

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 . 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.

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.

If it is the case that scanNum is not being dereferenced correctly, how can I achieve this?

Thank you for the help

*Update

Thanks for the help.

Just to recap

My set_info function needs to be passed an address parameter. 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.

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.

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.

And the body of your 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.


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 :)

I would pass in the variable into your set_Info function, so that it knows where to save the data. 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

#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("%d", scanNum);

And as liho said, you need to return scanNum from set_info so you can get at it outside of the function.

int scanNum = set_Info();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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