Thread: Reference to a Structure

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    4

    Reference to a Structure

    Hello,

    how do I reference a array to a structure, in a way when I change the value of that array I also affect the value of the structure, and when I affect the value of the structure I also affect the value of the array ?

    I tryed to do something with pointer to the structure, since a pointer to a structure is very similar to array. I have done something like this :

    Code:
    typedef structu
    {
       unsigned a1:1;
       unsigned a2:1;
       unsigned a3:1;
       .
       .
       .
       unsigned a8:1;
    }Bits;
    
    int main()
    {
       Coils objCoil;
       Coils *ptrCoil;
       ptrCoil = & objCoil;
    
       for(int i = 0; i < 8; i++)
       {
           ptrCoil[i] = 1;
       }
    
    }
    Thanks and best regards,
    BoSCHoW.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    hope you mean by Coils your Bits struct - and no, you cannot access struct members by index - only by name

    objCoil->a1 =1;
    objCoil->a2 =1;
    etc
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    4
    It is possible, lets say that I have a struct:
    Code:
    typedef struct
    {
       unsigned a1:1;
       .
       .
       .
       .
       .
       unsigned a2000:1;
    }Coils;
    and i have a function like this:
    Code:
    void transferBytes(unsigned char *source, unsigned char *dest, unsigned short quantity)
    {
          for(int i = 0; i < quantity; i++)
          dest[i] = source[i]; 
    }
    In the main function I just have to call the main function and write in the correct parameters:
    Code:
    int main()
    {
          Coils objCoils;
          Coils *ptrCoils;
          ptrCoils = &objCoils;
          unsigned char dest[260];
          unsigned short quantity = 20;
    
          transferBytes((unsigned char*)&ptrCoils, (unsigned char*)&dest, quantity);
    }
    Thanks and best
    regards,
    BoSCHoW.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    It is possible, lets say that I have a struct:
    And how do you predict what byte is finished in what member of the struct?

    If I will need to access that struct members in an array like manner - I would provide some macro for this...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. non-const reference and const reference
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 12-15-2007, 05:03 AM
  3. A general quaetion on portability
    By kris.c in forum C Programming
    Replies: 7
    Last Post: 07-20-2006, 03:48 AM
  4. problem with the library
    By kris.c in forum C Programming
    Replies: 21
    Last Post: 07-10-2006, 08:29 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM