Error "Operand of * must be a pointer" when creating pointer with malloc and using it in a FOR

2

I'm trying an example of the book "Understanding and using Pointers" that has this code:

int main()
{
    char *pc = (char*)malloc(6);

    for (int i = 0; i < 5; i++)
    {
        *pc[i] = 0;
    }

    return 0;
}

In theory, it should work, but I get the error:

  

Operand of * must be a pointer.

Can you tell me what's wrong? I am using Visual Studio 2015 Community.

    
asked by Sergio Calderon 13.10.2016 в 17:48
source

1 answer

3

The error is that to refer to the pc variable inside the loop, you no longer have to use the pointer symbol.

pc[i] = 0;

That way you initialize the memory.

If you want to copy the character 0, add single quotes:

pc[i] = '0';
    
answered by 13.10.2016 / 18:05
source