Pages: [1]   Go Down
Author Topic: Structure pointer problem  (Read 908 times)
0 Members and 1 Guest are viewing this topic.
Centurion RSA
Offline Offline
Full Member
***
Karma: 0
Posts: 166
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am using a pointer to a structure to write into eeprom.
Code:
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:
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);
  }
}

Logged

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

Yorkshire England
Offline Offline
Sr. Member
****
Karma: 2
Posts: 267
Arduino good init
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Shouldn't this be this?

Code:
//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:
(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.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
  }
}
Logged

Centurion RSA
Offline Offline
Full Member
***
Karma: 0
Posts: 166
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Yorkshire England
Offline Offline
Sr. Member
****
Karma: 2
Posts: 267
Arduino good init
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Centurion RSA
Offline Offline
Full Member
***
Karma: 0
Posts: 166
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That is exactly what will be done. The print is just so I can see what is going on.
Logged

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

Centurion RSA
Offline Offline
Full Member
***
Karma: 0
Posts: 166
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: April 23, 2011, 04:20:46 pm by wildbill » Logged

Centurion RSA
Offline Offline
Full Member
***
Karma: 0
Posts: 166
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

Pages: [1]   Go Up
Jump to: