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

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -