Struct in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Toe007
    New Member
    • Nov 2006
    • 3

    Struct in C

    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}};
  • Jai Vrat Singh
    New Member
    • Oct 2006
    • 18

    #2
    What do you exaclty want to know. Can you post the hypthetical output you expect.

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

    Code:
    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?

    Comment

    • Toe007
      New Member
      • Nov 2006
      • 3

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

      Comment

      Working...