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

int main()
{
    int* retPtr = (int*)malloc(sizeof(int)*2);
    
    retPtr[0] = 0;
    retPtr[1] = 1;
    
    printf("%d\t%d",retPtr[0],retPtr[1]);
    
    int* retPtr1 = (int*)malloc(sizeof(int));
        
    *retPtr1 = 1;
    
    printf("\n%d",*retPtr1);
    
    return 0;
}

Can anyone tell me why unary operator (*) is needed for a retPtr1 to access a value and not needed for retPtr to access a value for array.

wohlstad
  • 12,661
  • 10
  • 26
  • 39
  • 2
    You don't need to use the `*`, you can also use `retPtr1[0]` – David Ranieri Aug 08 '22 at 19:26
  • 1
    Welcome to Stack Overflow! [don't cast malloc](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Barmar Aug 08 '22 at 19:27
  • 1
    `ptr[i]` is equivalent to `*(ptr+i)`. In the case of the first element, `i == 0` so `ptr[0] == *ptr` – Barmar Aug 08 '22 at 19:28
  • 2
    *"and not needed for retPtr"* - it is needed... sort of, you're just using basic language syntax sugar that alleviates you of the typing. Your `retPtr[1] = ...` is synonymous to `*(retPtr+1) = ...`. This is covered in even the worst of the basic C language references and how-to's. Check the chapter(s) on pointers. – WhozCraig Aug 08 '22 at 19:30

2 Answers2

1

Can anyone tell me why unary operator (*) is needed for a retPtr1 to access a value and not needed for retPtr to access a value for array.

I can't tell you why, because it's simply not true. You can use * to access any kind of a pointer (or array), and you can use [] to access any kind of pointer (or array).

Here is a modification of your program demonstrating both:

int* retPtr = malloc(sizeof(int)*2);

retPtr[0] = 0;
retPtr[1] = 1;

printf("%d\t%d\n", retPtr[0], retPtr[1]);
printf("%d\t%d\n", *retPtr, *(retPtr + 1));

int* retPtr1 = malloc(sizeof(int));
    
*retPtr1 = 2;

printf("%d\n", *retPtr1);
printf("%d\n", retPtr1[0]);

Both pairs of printf calls access exactly the same values. The modified program prints

0   1
0   1
2
2
Steve Summit
  • 45,437
  • 7
  • 70
  • 103
0

The array access operator is nothing else than an add and a dereference. For example the following lines are similar:

printf("%d\n", my_arr[0]);
printf("%d\n", *my_arr);

So are these:

printf("%d\n", my_arr[1]);
printf("%d\n", *(my_arr + 1));

The last line uses pointer arithmetics. That means, my_arr is a pointer to a memory address and if we add something to it, it will point to an other memory address. Imagine your memory is a huge byte array. A memory address is an index to it. And when using the * operator, you are taking the value at this index. But whenever you are using the array access operator, you are first adding to the index/address and than taking the value.

Let's analyze it step by step.

int* my_arr = malloc(sizeof(int) * 5);
my_arr[0] = 0;
my_arr[2] = 5;

my_arr could now be for example 0xff560.

int* my_arr2 = my_arr + 2;

Now my_arr2 is 0xff568 and my_arr is unchanged. 8 is added to the pointer, because in this example one integer is 4 bytes large.

printf("%d %d\n", *my_arr, *my_arr2);

This will write "0 5". And the last two lines are just the complocated way to write:

printf("%d %d\n", my_arr[0], my_arr[2]);

Btw, you can output pointer values with "%p" in printf.

Tenobaal
  • 637
  • 1
  • 16
  • When you say that the following lines are similar, you should say equivalent, because they are. Also, you should not say you _can_ use the `%p` specifier with `print` for pointers, but rather that you _**should**_. – Chris Aug 09 '22 at 01:37