[Solved] Pointers ++

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am stuck. It has been more than a year since I have done anything more than the most trivial coding with C and this has me flummoxed. I think that I have tried every possible combination and nothing seems to work. Here is the situation.

 

I have a struct prototyped in a file structs.h as:

 

typedef struct {
	uint8_t dummy;      //to occupy EEPROM zero address
	uint8_t version;	//calblock version number
	int32_t scale[6][3]; //[scale_index][axis_index]
	int16_t off[6][3];
} calblock_t;

I have it defined in a module as a local variable in a function:

 

calblock_t sramcal;

I want it as a local so that it has no persistence. This instance only serves as a mirror to write to the eeprom.  It does exactly what one would expect it to do in this function. After loading it from the eeprom using eeprom_read_block(), I can access its members just fine. 

 

Now, I would like to pass this struct to another function which will format it and display it in a menu system. This second function has a prototype of

 

void CalList(uint8_t, char *, calblock_t * ); 

And the definition of the function looks like 

 

void CalList(uint8_t SensIdx, char *MyPtr, calblock_t * MyCal )
    {
    ...
    }

The compiler does not complain about this or the line where it is called:

 

CalList(0,MyPtr,&sramcal);

 

But, as soon as I try to use MyCal in the second function, I get a variety of errors, including

 

uint8_t MyVersion = *MyCal.version;

request for member 'version' in something not a structure or union

MyVersion = MyCal.version;

request for member 'version' in something not a structure or union

MyVersion = &MyCal.version;

request for member 'version' in something not a structure or union

Now, it becomes more illuminating if I try to create a new calblock_t in the second function and equate it to the first one via the passed pointer:

 

calblock_t ACalBlock;
ACalBlock = MyCal;

incompatible types when assigning to type 'calblock_t {aka struct <anonymous>}' from type 'calblock_t * {aka struct <anonymous> *}'

It appears that the second function is unable to access that local struct in the first function. Is there a problem trying to pass a local object via pointer to another function? If so, how do I do it without creating an object with infinite persistence? My reason for this is that this is an addition to an existing program and SRAM space is quite short (in other parts) and I don't want to fill up ram space with something that is used so intermittently. I guess that I could create an independent local copy in the second function, and load it directly from eeprom, but that sort of defeats things, no?

 

By the way, I have an actual copy of K&R open, right here, and I can find nothing that refers to limitations on pointers. There may be, and I just have not been able to find it. In any case, I am STUCK!

 

Thank you for your help!

 

Jim

 

 

This topic has a solution.

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

Last Edited: Sat. Mar 20, 2021 - 05:09 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Since you are using a pointer to the structure do;

 

uint8_t MyVersion = MyCal->version;

Happy Trails,

Mike

JaxCoder.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why didn't I think of that?

 

Thank you VERY much! Works like a charm.

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:

uint8_t MyVersion = *MyCal.version;

 

It's that pesky operator precedence thing again.

You could do

MyVersion = (*MyCal).version

to force the dereference before doing the dot, but this is a bit cumbersome and is why the arrow operator exists, with a pointer to a struct much easier as

MyVersion = MyCal->version