help using extern int16

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
DaRe_2236336
Level 3
Level 3
First like given

I'm trying to get some values from this accelerometer, the I2C is working fine but im trying to pass the values to main so that i can see them in debug mode so i can get offset values. I'm trying to pass them from the function that gets the values in the i2c.c file to my main.c file using "extern int16 rawDATA[3];" but i am getting warnings that i am getting an error saying "undefined reference to 'rawDATA[3]'" and warnings saying that the variables i created to show the data are created but not used. I also tried just passing the data as individual pieces before i put it in an array in the function which gave me the same result.

pastedImage_0.png

tried "void accelgetAllData(void)"

and having no return while using "extern int16 rawX,rawY, rawZ;"

pastedImage_1.png

any help would be appreciated, sorry if this is in the wrong section.

0 Likes
1 Solution
KyTr_1955226
Level 6
Level 6
250 sign-ins 10 likes given 50 solutions authored

Shouldn't rawDATA[3] be defined outside of main?  I don't actually know if/how extern works when defined inside a function.  Someone with some more knowledge of the ins and outs of C could chime in on that.

A problem I see though is that int16 accelgetAllData(void) wants to return an int16 and you are returning an int16* since rawDATA is an array.  So either return a pointer to rawDATA[] or you could pass in the address of rawDATA and directly modify it in the function.

so

int16 * accelgetAllData(void) would match what you have written for the return,

but

void accelgetAllData(int16 *raw_data_array) might be what you actually want in this case.

With that 2nd one, you are passing in the address of the first element of an array into the function, so you could do:

raw_data_array[0] = rawX;

raw_data_array[1] = rawY;

raw_data_array[2] = rawZ;

And that would change the values in rawDATA when called as accelgetAllData(rawDATA);

I don't even think you'll need an extern.

extern is a promise to the linker that the symbol exists somewhere elsewhere in another .c file so you can call it.

I.E. you would define an extern variable in main.c:

uint16_t ExternalVar = 100;

but if you want to call it from adc.c you would have to declare it in adc.c or adc.h:

extern uint16_t ExternalVar;

View solution in original post

0 Likes
7 Replies