我在这段代码中收到错误 "invalid indirection"

I am getting error in this code as "invalid indirection"

我正在尝试动态分配一个连续的内存块,存储一些整数值并显示它。

#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 is found here``                     
              }
}    }

ptr[i] 表示地址 (ptr+i) 处的值所以 *ptr[i] 是 meaningless.You 应该删除 *

您更正后的代码应该是:

 #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 the *
          }
  return 0;
}     //loose extra }