简体   繁体   中英

Pointers in c programming

I am new to the pointer concepts.. i dont understand the following program.. please tell about the logic of this program..

function (int *p,int *q){
  return(*p = (*p + *q) - (*q = *p));
}

int main(){
  int y = 15, z = 25;
  function(&y, &z);

  printf("%d\t%d", z, y);
}

This program invokes undefined behavior. It both modifies and uses the value of *q without a sequence point between.

C99 standard, Section 6.5, paragraph 2:

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.

*q is read in the sub-expression (*p + *q) , and this is not done to determine the value to store in the sub-expression (*q = *p) .

The intent appears to be to swap the two values pointed to, but a completely reasonable alternate interpretation also exists: evaluate (*q = *p) first, then (*p + *q) (which would equal *p + *p thanks to the assignment). This would result in *p being assigned *p + *p - *p , or just *p . As a result, z would be assigned the value of y (15), while y would remain 15.

I must emphasize that because this is undefined, your compiler can do pretty much whatever it wants, not just limited to this interpretation or swapping the two.

Pointer means you pass the address of the variables instead of the value.

You can then dereference(get the value pointed to by this address) and perform operations using that value.

If you pass a variable by value, that is without using &, a new copy will be created inside the function. Any changes done, will not be visible outside the function. If you pass pointers, then manipulations done to the content of the pointer will be visible outside the function.

On last note, pointers are complex to understand for new programmers. You better read tutorials and spend some time thinking about how things are working.

This is example of function where you pass by reference that is you pass the location of actual variable instead of making a copy of it and passing.so in you function p is basically pointing to y which is 15 and q is pointing to z=25 .It should be easy from there on to find the answer. *p= (15+25)-(15) *q=15.So y=25 z=15 .

First of all you have to know about the pointer...

Pointer is a variable that contain memory address of another variable...

In above program...you have declared 2 variables y and z... and you have created a function namely function(&y,&z).... means ...it sends the reference (memory address) of y and z to the funcion...function(*p,*q)

so *p contain 15 and *q contain 25...

*p=(*p+*q) means y= 15+25 which is 40....

Variable, like y and z in your program, have ranges. They exist only in a limited logical space. For instance, y and z only "exists" in main ; that's why they're called "local variable". Would you write :

int x,y;

void main
{
  // Stuff 
}

x and y would be global variables, that any function in this file could access. Since global variable make things harder to debug, when you need to access variable declared in a function, you can use pointer (it's only one of the uses of pointers, useful in your example).

When you give parameters to a function, by default, those parameters are copies, not the original. So, function(int p, int q) creates two new variables : you can do whatever you want to q and p, the x and z would not change.

When you use pointers, you create a special variable, containing the adress of the variable. You can get the adress of any variable with the "&" sign. This is why, to call function(int* p, int* q) you need to write function(&y, &z).

To access the content of a pointer, you need to use the * sign in front of the pointer. Beware ! If you don't do this, you're not modifying the variable the pointer is pointing to, but the adress of the pointer (in your case, that would not be useful ; but in array manipulation, for instance, it can be very useful).

So, your function gets the adresses of two variables, and then do some computing. This function does two things :

  • It computes something and give the result ;
  • During the computing, it gives to q the value of p, so z's value becomes y's value in the main.

Do some printf in your main, checking the value of your variables, before and after the function. Do it with a function using pointers and another one that does not use them, and it'll be much clearer to you.

And add a "int" in front of your function, since it returns something.

wnoise's answer is correct with but if you want to understand why you have such results below you can find reasoning.

this is not a pointer challenge but rather operator precedence.

Just treat *p and *q as You would use y & z

and with this code: return(*p = (*p + *q) - (*q = *p)); remember that result of assigment is assigned value thus with *p = y = 15 and *q = z = 25

you have:

return(*p = (15 + 25) - (*q = *p));

return(*p = 40 - (*q = 15)); //*q -> z becomes 15

return(*p = 40 - 15);

return(*p = 25); // *p -> y becomes 25

return(25);

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