Home > database >  clarification for pointers in c language
clarification for pointers in c language

Time:04-05

this is a snippet from my code. I'm wondering if the pointers I'm using and the way I'm using them is valid. The code works but I need more clarification on how they work.

Here's the code:

void swapValues(int *x, int *y) {
    int max;
    max = *x;
    *x = *y;
    *y = max;
}

The int variable max can be equal to *x? I understand how the *y = max; works but I'm confused in the first one. Can y'all please elaborate it more so that I can grasp how it works.

CodePudding user response:

Assuming you mean

max = *x;

with *x instead of *p, then it works basically the same as the assignment

*y = max;

First the pointer x is dereferenced to get the value x is pointing to. Then that value is assigned to the (rather badly named) max variable.

  • Related