Go Down

Topic: Structure pointer problem (Read 1 time) previous topic - next topic

Marius

I am using a pointer to a structure to write into eeprom.
Code: [Select]
struct CFG {
  byte configEmpty;
  byte motorSpeed;
  byte pulleyRatio;
  byte maxSpeed;
  byte minSpeed;
  byte pulsesPerMeter;
  byte downRampDistance;
  byte upRampDistance;
  byte jobSize;
  byte jobRollCount;
  byte jobMeterCount;
};
struct CFG config;
struct CFG *cfg_ptr;


Then i use the pinter like this and get an error that a lvalue is required as the lefthand operand. Please help.
Code: [Select]

void readConfig(void)
{
  for(byte i=0; i < sizeof(config);i++)
  {
    cfg_ptr = &config;
    byte value = EEPROM.read(i);
    (cfg_ptr++) = value;
    LCD.print(value);
  }
}


Rather people think you to be a fool than you open your mouth and confirm it.

EVP

Shouldn't this be this?

Code: [Select]

//your code
struct CFG config;
struct CFG *cfg_ptr;

//new code
CFG config;   //CFG is the type so you don't need to put stuct in front of it
CFG *cfg_ptr;

and this

cfg_ptr = &config;

could be this

cfg_ptr = config;


after that it isn't obvious to me what your using the pointer for as your reading a byte into value from the eprom and then printing it to an LCD. you do assigning something here

Code: [Select]
(cfg_ptr++) = value;

but without testing it you are assigning the address of the pointer to value (a byte) and then incrementing the pointer by the size of CFG (the whole lot) This wont work i don't think.  Unless i'm missing something.

wildbill

Several problems. Your immediate compilation issue is that you're not de-referencing the pointer, but also, as mentioned above, the ++ is not doing what you expect. It doesn't matter though, because you're setting the pointer back to the initial position in every iteration of your for loop. Try this:
Code: [Select]

void readConfig(void)
{
  byte *cfg_ptr= (byte*)&config;

  for(byte i=0; i < sizeof(config);i++)
  {
    byte value = EEPROM.read(i);
    *cfg_ptr++ = value;
    LCD.print(value);
  }
}

Marius

Wildbill
The pointer is of type CFG - of the structure. Will it still point to the structure if I dereference it to a byte.
What I am trying to achieve is to read the eeprom sequentially into the structure. No matter the size of the members (increment the pointer by one byte at a time). This way I can later write longs and floats and pack them byte by byte into the structure. When you then reference the structure, you will read the correct values.
Rather people think you to be a fool than you open your mouth and confirm it.

wildbill


Wildbill
The pointer is of type CFG - of the structure. Will it still point to the structure if I dereference it to a byte.
What I am trying to achieve is to read the eeprom sequentially into the structure. No matter the size of the members (increment the pointer by one byte at a time). This way I can later write longs and floats and pack them byte by byte into the structure. When you then reference the structure, you will read the correct values.


Yup, that code does exactly what you want.

EVP

I makes no difference really but instead of printing to LCD from value, i would print from the structure. i.e. copy from the eprom to the structure then print from the structure using the pointer and then finally increment it. This way you will be sure that what is in the stuct is the same as the eprom.

Marius

That is exactly what will be done. The print is just so I can see what is going on.
Rather people think you to be a fool than you open your mouth and confirm it.

Marius

Thanks for the help guys. It is working fine. I am still slightly confused by the pointer issue but I am sure I will get over it soon. :smiley-red:
Rather people think you to be a fool than you open your mouth and confirm it.

wildbill

#8
Apr 23, 2011, 10:34 pm Last Edit: Apr 23, 2011, 11:20 pm by wildbill Reason: 1

Thanks for the help guys. It is working fine. I am still slightly confused by the pointer issue but I am sure I will get over it soon. :smiley-red:


A pointer is just an address of a location in memory, so whether it's CFG* or byte*, it has the same value. The only difference is that if it's CFG*, the compiler will allow you to directly reference the elements of the struct, which is just a syntactic convenience. Byte* lets you forget about the struct and treat it as the set of contiguous bytes it really is. ptr++ moves to the next element pointed to though, so if it's a byte pointer, it'll increment by one. If it's defined as a pointer to a struct, it'll increment by the size of the struct.

edit: clarity

Marius

Wildbill,
Thanks man, I did discover what you described. I think my understanding of pointers to structures was not up to scratch. Anyway, the issue is solved and I understand better now. Thanks a lot for the help, it was very constructive and appreciated.
Rather people think you to be a fool than you open your mouth and confirm it.

Go Up