Home > Enterprise >  Pointer variable can't be given value, instead, normal variable can be
Pointer variable can't be given value, instead, normal variable can be

Time:02-26

I faced a wired problem, it is a big and commercial project so I can't put the original code here. but the logic is like this:

struct sample_t
{
  int inta;
  int * p_intb;
};
sample_t sample ={0};
sample.inta = 0; 
sample.p_intb = NULL; // crashed, why?

run into crash due to the pointer operation, but for inta there was no problem. however, if I use memset(&sample.p_intb, 0, 4) to replace above equal, it worked.

What is the reason of this? How to solve this problem?

This is a screenshot of the code

CodePudding user response:

this problem was caused by pointer start address(must be times of 4) I am not sure about the exact reason, but should be related to the chip (ecu).

  • Related