简体   繁体   中英

Pointers in C for a rookie

I am just starting to learn programming for a unit I am doing in my engineering course and I have come across pointers. I just wanted some reassurance that I actually understand the concept correctly in terms of using a pointer as an argument in a function. If I understand it correctly, you pass a pointer to an address of a variable you would like to be altered by a separate function called, even though it is a local variable within the scope of the calling function. Does that make sense? I have an example from my text book which I re-wrote. The only thing is they gave it in two incomplete parts and I put it together, filled in the blanks and added the final printf statement in the main function. I'll paste it here:

#include <stdio.h>
#include <stdlib.h>

#define READ_OK 0
#define READ_ERROR 1

int read_num(int lo, int hi, int *num);

int main(int argc, char *argv[])
{
    int lo = 0, hi = 0, *num, val;
    printf("Please enter a lower bound and an upper bound for your range,respectively\nLower: ");
    scanf("%d", &lo);
    printf("Upper: ");
    scanf("%d", &hi);
    num = &val;
    if(read_num(lo,hi, &val) != READ_OK)
    {
        printf("Read error, program abort\n");
        exit(EXIT_FAILURE);
    }   
    else
    {
        printf("You entered %d, press any key to continue: \n", val);
        getchar();
    }
    return 0;
}

int read_num(int lo, int hi, int *num)
{
    int next;
    printf("Enter a number between %d and %d: ", lo, hi);
    while(scanf("%d", &next)==1)
    {
        if (lo<=next && next<=hi)
        {
            *num = next;
            return READ_OK;
        }
        printf("%d is not between %d and %d\nTry again: ", next, lo, hi);
    }
    return READ_ERROR;
}

So is my understanding correct? "val" gets modified in read_num() by passing it's address in the form of pointer "*num", in which the the value for "next" is then written? PS: is this syntax correct? PPS: What would this process specifically be called?

Thanks a bunch for any help :)

The *num is not necessary inside the main() function. As you are passing the address of the val inside the read_num() , so any changes from the read_num() will also affect the value inside main() as you are working with the address.

In your program you have basically use two different pointers- one is inside main which is num, and another inside read_num() which is also num, for more understanding see the scope of a variable in c. As the val is inside main so you don't need to use pointer here, because you have the access of changing the value from the main as it is local to it. You will need pointer when you will be changing the value of val outside from the main, or from outside of the scope of the variable.

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