search

Home  >  Q&A  >  body text

c++ - Can't understand a sentence about c dynamic memory allocation?

char *data;
int leng=0;

data = (char *)malloc(nFileLen);

for (int i = 0; (aa = fgetc(fp)) != EOF;i++)
{
    data[i] = aa;
        
    leng++;
}

I don’t understand how to operate like this after dynamically allocating memory. Can you explain data[i]=xxx I don’t understand this sentence
and when I use it, I use *data only outputs one character
, but using the loop data[i] can output a lot. I’m wondering if it’s not *data[i]

代言代言2703 days ago672

reply all(1)I'll reply

  • 为情所困

    为情所困2017-06-20 10:08:25

    data为char *类型指针,可以以数组下标形式访问,指针+偏移,data[i]等价于*(data + i)

    reply
    0
  • Cancelreply