Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - C++ integer pointer assignment problem
C++ integer pointer assignment problem
Pointer variable p begins to store the value to the address it points to without allocating storage space, which is of course wrong.

int * p =(int *)malloc(256 * sizeof(int)); //Let him point to a dynamically allocated memory block.

or

int a[256]= { 0 };

int i,* p;

p = a;

for(I = 0; I & lt256; i++)

{

*(p+I)= I;

}

It's okay, let p point to an array a.

Only one int can be stored in dynamically allocated memory.

p =(int *)malloc(sizeof(int));

change into

p =(int *)malloc(sizeof(int)* 256);