c - I am getting error in this code as "invalid indirection" -
i trying dynamically allocate contiguous block of memory, store integer value , display it.
#include<stdio.h> #include<conio.h> void main() { int i; int *ptr; ptr=(void *)malloc(sizeof(int)*5); //allocation of memory for(i=0;i<5;i++) { scanf("%d",&ptr[i]); } for(i=0;i<5;i++) { printf("%d",*ptr[i]); //error found here`` } } }
ptr[i] means value @ address (ptr+i) *ptr[i] meaningless.you should remove *
your corrected code should :
#include<stdio.h> #include<stdlib.h> #include<conio.h> int main() { int i; int *ptr; ptr=malloc(sizeof(int)*5); //allocation of memory for(i=0;i<5;i++) { scanf("%d",&ptr[i]); } for(i=0;i<5;i++) { printf("%d",ptr[i]); //loose * } return 0; } //loose }
Comments
Post a Comment