By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 1,031 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

Struct in C

P: 3
Please help me. I am new to C and working on a zilog micro. I need to read all elements of a structure I have set up and write theses bytes to EEPROM.
I thought the way was to increment a pointer to the structure, but seem not to have much success doing this. Please advise how to go about this.

My code is:

struct Calib // Structure to hold calibration data
{
int DcOffset[3]; // Array [x1,x10,x100]
signed int CorrFctr[2][3][3]; // Array [V,I] [Red,White,Blue] [x1,x10,x100]
} CalData;

struct Calib *st_ptr;

for (i = 0; i <= 80; i++)
{
printf("Value at this address is: %d \n", st_ptr);
*st_ptr++;
}

Also, Please advise if one can assign constants in a struct. For example, can I add the element below, to the structure:

unsigned int CalVal[2][3] = {{230, 230, 230}, {25, 5, 1}};
Nov 1 '06 #1
Share this Question
Share on Google+
2 Replies


P: 18
What do you exaclty want to know. Can you post the hypthetical output you expect.

You code will print the pointer(addresses) of adjacent structures. thus if we say (suppose) the junk address contained by defn:

Expand|Select|Wrap|Line Numbers
  1. struct Calib *st_ptr;
is 4022 and size of int(or unsigned int) on your machine is 4, then delta=(3+3*3*2)*4 = 84, your program will print
4022
4106
4190
......
......
with a gap of sizeof( struct Calib ) = 84 everytime ..81 times. Why do you want to print this?
Nov 2 '06 #2

P: 3
Thank you for the answer - I actually reposted this question under the heading "Reading contents of a structure" as I could not find my original posting at first.

To answer your question, I am wanting to read out all the bytes of a structure and to write these to EEPROM i.e map the memory occupied by the contents of the structure to EEPROM. This is part of a uP development I am doing.

So what I am trying to figure out (in my newness to C), is if there is an elegant way to access the contents of the structure one byte at a time, using a pointer which I increment. I picture this a bit like starting the pointer at the first element in an Array[x] and reading a byte each time the ptr is incremented. only problem is I do not know how to do it with a structure containing multiple members.

I need something along the lines of :

void TestRoutine (void)
{
struct Calib // Structure to hold calibration data
{
signed int DcOffset[3];
signed int CorrFctr[2][3][3];
} CalData;

unsigned int i;
struct Calib *st_ptr;

st_ptr = &CalData;

for (i=0; i <= SizeOfStructure, i++) do
{
printf("Value at this address is: %d \n", *st_ptr); // write contents at address pointed to by pointer
st_ptr++; // point to next byte in structure and repeat
}



I know something like

for (i = 0; i <= 3; i++)
printf("Value at this address is: %d \n",st_ptr->DcOffset[i]);

will work, but seems to me there should be a way to read each byte from the structure Calib without having to reference its members such as DcOffset ?

Thanks.
Nov 3 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.