0

Code 1:

#include<stdio.h>
int main(void)
{
    int* arr[5];
    for(int i=0; i<5; i++) {
        arr[i] = (int*)malloc(sizeof(int));
        *arr[i] = i;
    }
    printf("%d",arr[3]);
    return 0;
}

Output:

13509232

Code 2:

#include<stdio.h>
int main(void)
{
    int* arr[5];
    for(int i=0; i<5; i++) {
        arr[i] = (int*)malloc(sizeof(int));
        *arr[i] = i;
    }
    printf("%d",arr[3][0]);
    return 0;
}

Output:

3

I've declared an array of pointers. Instead of going for *arr[3] to dereference the value stored, I can simply write arr[3][0] to dereference it. It works for all dimensions of arrays. Just by adding an extra zero to it's dimension, dereferences it's value stored at the location.

I couldn't get it, because at the very first place, arr[3] is 1D array and arr[3][0] is 2D array. After all, arr[3] is an address, and arr[3][0] is a value stored at the address arr[3]. Can I use in this way to get rid of * operator? Kindly explain in simple terms.

Thanks in advance.

EDIT 1: Let's take the simplest case, single variable.

#include<stdio.h>
int main(void)
{
    int k=5;
    int *ptr=&k;
    printf("%d",ptr[0]);
    return 0;
}

Output:

5
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Subash
  • 43
  • 7
  • 4
    Don't cast the return value of `malloc`. It hides errors if you forget to include stdlib.h (which, incidentally, you did). – sepp2k Jun 16 '18 at 12:47
  • [Do I cast the result of malloc?](https://stackoverflow.com/q/605845/995714) – phuclv Jun 16 '18 at 13:44
  • regarding: `printf("%d",arr[3]);` the value the code is trying to print is an ADDRESS. not an integer. So the correct format specifier is '%p' not '%d' – user3629249 Jun 17 '18 at 16:28
  • when calling any of the heap allocation functions: `malloc` `calloc` `realloc`, alway check (!= NULL) the returned value to assure the operation was successful – user3629249 Jun 17 '18 at 16:29
  • If the code is trying to print the value pointed to be the array entries, the correct syntax would be: `printf( "%d\n", *(arr[3]) );` This takes into account the value you want to access AND consideration of the precedence of the C operators – user3629249 Jun 17 '18 at 16:33

5 Answers5

3

ptr[x] can be written as *(ptr+x). So, ptr[0] translates to *(ptr+0) which is *ptr.

PhilMasteG
  • 3,095
  • 1
  • 20
  • 27
1

%d expects the value itself, not a pointer to it. arr[3] is a pointer, the 4th pointer in your pointer array. arr[3][0] is the actual number. You could also write *arr[3], and you actually do, in the loop.

Pointers behave similar to arrays, after you allocate memory to int *a, you can store a value to the pointed location as *a=123 or as a[0]=123. Also if you allocate memory for 2 integers, you can access the second item as a[1] (which is simpler), but also as *(a+1)

Bottom line: in terms of int-s, you have a 2D array, just not a tightly-packed n*m-style one, but one often reffered as jagged (https://en.wikipedia.org/wiki/Jagged_array).

tevemadar
  • 12,389
  • 3
  • 21
  • 49
0

Since the address calculated for both the cases will be same so it it fine to use they arr[3][0]. Let assume address of arr[3] to be 1000. Son address for arr[3][0] will be 1000+0*sizeof(int) i.e 1000

Cr6
  • 1
0

There is an easy interpretation for using [0] to deference(although I won't recommend it). See this small code:

int i;
int j[1];

What is difference between these 2? obviously one is array with size 1, but if you think about it, array of size 1 is somehow equal to defining that type once.

dereferencing with * is somehow equal to accessing with array index. But because the data looks like to an array with 1 element, you access it with [0].

Afshin
  • 8,839
  • 1
  • 18
  • 53
-1

Use this type of code. And try to change you're code to this way, Hope this might help you. Cheers.

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

int main()
{
    float *p, tot = 0;
    int i, n;

    printf("Number of observations: ");
    scanf("%d", &n);

    p = (float*)malloc(n*sizeof(float));

    if(p==NULL)
    {
        printf("Memory is not allocated");
        exit(1); 
    }

    for(i = 0; i < n; i++)
    {
        scanf("%f", p+i);
    }


    for(i = 0; i < n; i++)
    {
        tot += *(p+i);
    }

    printf("\nAverage = %.2f\n", tot/n);


    return 0;
}

Sample Input:

Number of observations: 4

12.12

34.14

43.1

45.87

Sample Output:

Average = 33.81