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.

Wayne
  • 3
  • 1

1 Answers1

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.

Myst
  • 18,516
  • 2
  • 45
  • 67
  • Sorry, but I don’t understand the meaning of "you were writing to the memory that held the variables in main". – Wayne Oct 30 '19 at 02:27
  • @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). – Myst Oct 30 '19 at 02:36
  • Sorry, but where is the difference between code one and code two? – Wayne Oct 30 '19 at 03:30
  • 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` – Myst Oct 30 '19 at 09:53
  • I got downvoted because...? – Myst Oct 30 '19 at 19:54
  • I don't know, that's not what I did. – Wayne Oct 31 '19 at 07:05
  • I understand now, thank you. – Wayne Oct 31 '19 at 07:12
  • @Arthasum - if my answer helped at all, feel free to upvote or accept the answer. – Myst Nov 01 '19 at 23:08
  • upvoted...not sure why you got downvoted, since this is perfectly reasonable. (maybe it was the grammar police; some things just have the _wrong effect_ on those people...) – Z4-tier Nov 02 '19 at 02:33