Reputation: 43
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
Upvotes: 0
Views: 89
Reputation:
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
Upvotes: -1
Reputation: 9173
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]
.
Upvotes: 0
Reputation: 13205
%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).
Upvotes: 1
Reputation: 3185
ptr[x]
can be written as *(ptr+x)
. So, ptr[0]
translates to *(ptr+0)
which is *ptr
.
Upvotes: 3
Reputation: 1
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
Upvotes: 0