0

I'm new to C and I had a couple questions about getting struct values into a function and the correct way to declare the function.

in my common.h I've defined

extern struct ddrautocal;
int get_eeprom_vals(uchar); // is this the correct declare the function?

in calibration.c I define my struct and change some set some values (not shown)

#include <common.h>
struct ddrautocal {
    u32 wdtr;
    u32 clkp;
    u32 rdcc;
};

in proc.c

#include <common.h>
int get_eeprom_vals(struct ddrautocal *cal){
// I'd like to access cal.wdtr and cal.clkp
}

I'm a complete derp, I know but I'm trying to get better. I've been trying to get this working all day and would like to know if I am declaring the function correctly in common.h and what is the correct way to access the ddrautocal struct in my get_eeprom_vals function located in proc.c Any help would be greatly appreciated. Thanks!

Lews Therin
  • 10,907
  • 4
  • 48
  • 72
txcotrader
  • 585
  • 1
  • 6
  • 21

2 Answers2

1

int get_eeprom_vals(uchar); signature is wrong.. It should be:

int get_eeprom_vals(struct ddrautocal *cal); So the signature of the forward declaration and function definition matches.

To access the members, you need: cal->wdtr, cal->clkp or (*cal).wdtr (*cal).clkp

-> uses the pointer indirection operator. * is the combination of the indirection operator and the period - the dot - to access the field.

I also think you need #include "common.h" instead of #include <common.h> as common.h isn't a system header.

Lews Therin
  • 10,907
  • 4
  • 48
  • 72
  • Excellent response. I'm now able to compile successfully. In my proc.c, when accessing members like cal->wdtr I am getting a compilation error of "error: dereferencing pointer to incomplete type". Changes made in calibration.c should be global because of "extern struct ddrautocal;" in common.h correct? – txcotrader Oct 30 '12 at 22:29
  • I think you need to include calibration.c in proc.c. I haven't done in C in a loong time, but I believe that's why it is complaining. It can't find the definition of the struct. – Lews Therin Oct 30 '12 at 22:31
0

First thing to know is that we have to make sure that the declaration of a function and its definition match each other. In your case, the definition has the signature:

int get_eeprom_vals(struct ddrautocal *cal)

(A signature of a function includes its name and the argument list).

You need to replace the declaration with:

int get_eeprom_vals(struct ddrautocal *cal);

Also if you want to use period dot to derefer to a structure's members, you can declare your function as follows, and then you can refer to its members as what you showd

int get_eeprom_vals(struct ddrautocal cal);
{
     // you can use cal.wdtr etc
}
Guangchun
  • 341
  • 1
  • 4
  • Guangchun, thank you, very terse response. when running int get_eeprom_vals(struct ddrautocal *cal); { printf("WRTR %x\n CLKP %x\n", cal.wdtr, cal.clkp); } – txcotrader Oct 30 '12 at 22:46
  • int get_eeprom_vals(struct ddrautocal *cal){ printf("WRTR %x\n CLKP %x\n", cal.wdtr, cal.clkp); } Gives me an error of: error: request for member 'wdtr' in something not a structure or union error: request for member 'clkp' in something not a structure or union does this mean i declared my extern struct ddrautocal; incorrectly? – txcotrader Oct 30 '12 at 22:50