Home > Back-end >  Difference in ptr,&ptr,&ptr[0]
Difference in ptr,&ptr,&ptr[0]

Time:02-12

Below is the test code:

#include <stdio.h>

int funtion(int *ptr)
{
    int temp;
    temp = *ptr;
    printf("ptr = %p %p %p %d\n", ptr, &ptr, &ptr[0], *ptr);
    printf("*ptr = %d\n", *ptr);
    return temp;
}

int main()
{
    int i = 10;
    int *tp = &i;
    printf("tp = %p %p %p %d\n", tp, &tp, &tp[0], *tp);
    (void)funtion(tp);
    return 0;
}
answer:
tp = 0x7ffc1117392c 0x7ffc11173930 0x7ffc1117392c 10
ptr= 0x7ffc1117392c 0x7ffc111738f8 0x7ffc1117392c 10
*ptr =10

**question:

  1. what are the difference between tp &tp &tp[0]
  2. Which passing parameter we should use.
  3. is this difference only for passing parameter?**

CodePudding user response:

You are passing an int pointer to function. This is passed by value. Of course, the value they point to is the same.

int funtion(int *ptr)
{
    int temp  = *ptr;
    printf("ptr = %p %p %p %d\n", ptr, &ptr, &ptr[0], *ptr);
    printf("*ptr = %d\n", *ptr);
    return temp;
}

temp is initialized with the value pointed to by ptr.

The first thing you print is ptr: the memory address you passed to function by value.

The second thing you print is &ptr. This is the address of that function argument. While this may be the same between function calls due to implementation-specific handling of the stack, it may also be different.

The third thing you print is &ptr[0]. This is the address is ptr[0], which is equivalent to writing *(ptr 0) or just *ptr. Getting the address of this in turn is equivalent to just writing ptr.

The fourth thing you print is *ptr. Again, as with initializing temp this is just the int value that ptr points to.

CodePudding user response:

1. what are the difference between tp &tp &tp[0]

Its their type.
The type of tp is int *.
The type of &tp is int **.
The type of &tp[0] is int * because &tp[0] is equivalent to tp1) :

&tp[0]  ->  &(tp[0])  ->  &(*(tp)   (0))  ->  &(*tp)  ->  tp

Note : You do not need to use [0] with pointer to access the value of a scalar type, which the pointer is pointing at.

2. Which passing parameter we should use.

Its not clear, what exactly you want to ask.
You are passing value of tp to function() function which is nothing but &i. The ptr parameter of function() function will hold the &i when it is called. If you want to make changes to the value of i within the function() function then you can do it because ptr hold address of i and *ptr will give value at that address which is nothing but the value of variable i. If you just want to access the value of i, you can simply pass the value of variable i.

3. is this difference only for passing parameter?

Which difference? I believe, you mean - why there is difference in &tp and &ptr?
ptr is local variable of function() function and tp is local variable of main() function. Though, they both hold the address of variable i their own addresses are different because they are different variables.


1). C Standards#6.5.2.1

The definition of the subscript operator [] is that E1[E2] is identical to (*((E1) (E2)))..

  • Related