Reputation: 109
I'm currently learning c language, and I bumped into this code. ptr is already a pointer type of variable, so what is the effect of the &
operator on it, cause I know that usually the operator uses to get the address of non-pointer variable.
struct name {
int a; float b; char c[30];
};
int main()
{
struct name *ptr;
int i,n;
printf("Enter n: ");
scanf("%d",&n);
ptr = (struct name*)malloc(n*sizeof(struct name));
/* Above statement allocates the memory for n structures with pointer ptr pointing to base address */
for(i=0; i<n; ++i) {
printf("Enter string, integer and floating number respectively:\n");
scanf("%s%d%f", &(ptr+i)->c, &(ptr+i)->a, &(ptr+i)->b);
}
}
Upvotes: 1
Views: 281
Reputation: 320361
Firstly, operator &
in C and C++ languages is used to get address of any variable. More precisely, it can be used to get address of [almost] any lvalue (there are some differences between C and C++ in that regard). Pointer variables are ordinary variables. There's nothing special about them, which means that there's nothing unusual in seeing operator &
applied to pointers.
Secondly, in the code you provided, there actually isn't a single instance of &
being applied to a pointer. There are four applications of &
in your code
&n
&(ptr+i)->a
&(ptr+i)->b
&(ptr+i)->c
In the first two cases it is applied to int
objects. In the thirds case it is applied to float
object. In that last case it is applied to a char [30]
object. No pointers.
Upvotes: 1
Reputation: 7766
The code &(ptr + i)->c
gives you the address of the struct element c
that belongs to the ith struct in your list. Let's break it down a bit.
(ptr + i)
is pointer arithmetic. It adds i to the address stored at ptr
.
(ptr + i)->c
accesses the struct element c through the pointer (ptr + i)
.
&(ptr + i)->c
takes the address of the struct element c through the pointer (ptr + i)
.
Also, I know this isn't quite doing what you thought it was doing, since you thought the address-of operator applied to the pointer, but just an FYI: you can indeed take the address of a pointer. Such a construct is a pointer to a pointer, and is useful when you want to change the pointer (not just the value stored at the address it points to) in a function. e.g.
int a = 5; /* regular variable */
int* pa = &a; /* pointer to a */
int** ppa = &pa; /* pointer to pointer (which points to a) */
Upvotes: 2
Reputation: 15872
struct name* ptr;
This creates a pointer to a name
.
struct name** p = &ptr;
This creates a pointer to a pointer to a name
by taking the address of the pointer you already created.
In your case, you are passing in pointers to scanf
, and have a dynamic array of name
, so
&(ptr + i)->c
finds the ith element of the array, and returns the address of its c
member to scanf
(same with a
and b
).
Passing in the address of the pointer allows for that pointer to be changed (e.g. reallocated). In C++, it would be virtually identical to passing by reference.
Upvotes: 0
Reputation: 4939
&(ptr + i)->c
this takes the address of the variable c stored at the ith element of ptr. So your first intuition was correct.
ptr+i
is simply the pointer arithmatic to find the ith element fo the array.
(ptr+i)->c
accesses the field c
from that struct, and the &
takes the address of that variable.
Upvotes: 2