Reference to a Structure

This is a discussion on Reference to a Structure within the C Programming forums, part of the General Programming Boards category; Hello, how do I reference a array to a structure, in a way when I change the value of that ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 04: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, 09:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21