0

Under what circumstances, the pointer in the C language function will change after the function is executed, and under what circumstances will not change.

I am learning pointers now, but I am having some trouble now.

The first code

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

void test(int* p);

int main()
{
    int a[] = {2, 4, 6, 8, 0};
    int* p = a;

    printf("before p = %p, *p = %d\n", p, *p);
    test(p);
    printf("after p = %p, *p = %d\n", p, *p);

    system("pause");
    return 0;
}

void test(int* p)
{
    p++;
    printf("In test p = %p, *p = %d\n", p, *p);
}

The second code

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

void swap (int* a, int* b);
int main()
{
    int a, b;

    scanf("%d %d", &a, &b);
    swap(&a, &b);
    printf("%d %d\n", a, b);

    system("pause");
    return 0;
}

void swap (int* a, int* b)
{
    int t;
    t = *a;
    *a = *b;
    *b = t;
}

I want to know why p has not changed in the first program after the execution of the function, and the second program a, b has changed.

1 Answer 1

0

Variables in C have a value (content) and they have a memory address (location) - the address may be optimized away by the compiler when not in use.

C passes arguments to functions by value (always), so the arguments (variables) in the function occupy a different memory location (even if they are initialized with the same value).

The value of the pointer can be manipulated (p++), effecting its data (changing the location to which p is pointing).

Pointers can also be dereferenced (*p), effecting the data in the location they point to.

In the function test(int* p), the argument p is placed in the scope of the function. It can't accessed by other functions and it isn't available outside of the function (unless it's location is known by someone else, then they can access it).

When you edit the content of the p variable, it only effects the value of p within the function - it changes the location to which p is pointing.

However, when you dereference the value of p, you read / write to the location to which p is pointing (the location in the memory that holds the variable in main).

This is why the p++ in test doesn't effect the value in main.

On the other hand, in the swap function, you were writing to the memory that held the variables in main, which has side effects in main.

Sign up to request clarification or add additional context in comments.

9 Comments

Sorry, but I don’t understand the meaning of "you were writing to the memory that held the variables in main".
@Arthasum - In C (and generally in computers), everything is either a 0 or a 1 in some bit located somewhere. The variables in main are zeros and ones somewhere in the memory (allocated on the stack). Pointers allow you to access these bits in the computers memory - assuming you know their address. In main, you retrieved a variable's memory address using &a. Later, in swap, you accessed that variable's memory using *a (dereferencing the pointer).
Sorry, but where is the difference between code one and code two?
The difference is in the dereferencing operator *... it looks something like this (to clarify I use the same variable name and operation): *p = *p + 1 vs p = p + 1
I got downvoted because...?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.