Hello,
Code:
#include<stdlib.h>
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);
}
printf("Displaying Infromation:\n");
for(i=0;i<n;++i)
printf("%s\t%d\t%.2f\n",(ptr+i)->c,(ptr+i)->a,(ptr+i)->b);
return 0;
}
In the above code i would like to use (.) Operator in scanf and printf statements but i am getting some errors, please help me how can i use (.) operator in scanf and printf statement.
scanf("%s %d %f", &(*ptr+i).c, &(*ptr+i).a, &(*ptr+i).b);
printf ("%s %d %f\n", (*ptr+i).c, (*ptr+i).a, (*ptr+i).b);
Error:
[Error] invalid operands to binary + (have 'struct name' and 'int')