Wednesday, 2 October 2013

adress of a pointer variable

adress of a pointer variable

I'm currently learning c language, and I bumped into this code. ptr is
already a pointer type of variable, so what is the affect of the '&'
operator on it, cause I know that usualy the operator uses to get the
adress of non-pointer variable.
Tnx, Dean.
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);
}
}

No comments:

Post a Comment