Thread: Pointer to a typedef struct

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    4

    Pointer to a typedef struct

    Hi,

    Please help me with this C syntax. I get a incompatible type warning with this:

    typedef struct{
    unsigned char a;
    unsigned char b;
    unsigned char c;
    } TEST;

    void main(void)
    {
    unsigned char *pointer;
    TEST Test;

    pointer = &Test;
    }

    What I tried to do there is to get the address of "Test".
    Thanks!
    James

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    TEST Test;
    TEST* pointer = &Test;
    Types must match, or you must use a cast (bad practice unless you know what you're doing).

    Also, http://cboard.cprogramming.com/dos-p...ead-first.html
    And of course, http://cpwiki.sourceforge.net/void_main
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    Thank you for your quick reply. I run into another incompatible type:

    Code:
    #define Element_A_Offset 0
    #define Element_B_Offset 1
    #define Element_C_Offset 2
    #define Element_D_Offset 3
    
    typedef struct{
      unsigned char a;
      unsigned char b;
      unsigned char c;
      unsigned char d;
    } TEST;
    
    TEST Array[2];
    
    //----------------------
    // Get X element of an Y array index
    //----------------------
    unsigned char Get_Element(unsigned char Array_Index, unsigned char Element_X_Offset)
    {
      TEST *pointer;
      unsigned char IndexOffset, Val;
      
      pointer = &Array[0];                                  // get address of array
      IndexOffset = Array_Index << 2;                       // 4 byte each
      Val = *(pointer + IndexOffset + Element_X_Offset);    // get element X of Array_Index
      *(pointer + IndexOffset + Element_X_Offset) = 0xAA;   // set element X of Array_Index
      
      return Val;
    }
    //------------
    
    void main (void)
    {
      unsigned char Value;
      
      Value = Get_Element(1, Element_B_Offset);
    }
    Thank you very much in advance!
    James

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    pointer is a pointer to a TEST. Val is an unsigned character.
    You still haven't addressed the void main problem.
    Perhaps you should describe what you are trying to do. This code will most likely fail in a most horribly manner.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    Hi,

    The function "Get_Element" is supposed to return the value of an element of a specific index of the array.

    For example, the function call:

    Value = Get_Element(1, Element_B_Offset);

    The returned value "Value" should be the value of element b of the second structure in the array "Array".

    Hope I didn't confuse you more...
    James

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I think our question is, why are you using techniques (char * as a generic type, using addresses to get at parts of a struct) that are considered bad form (and have been for a long time)?

    After all, if you want the value of element b of the second structure in the array "Array", why not just type
    Code:
    Array[1].b
    ?

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    I am writing code for a microcontroller and I want it to execute as fast as possible. Using "Array[1].b" would work perfectly, but it would involve some mulitply operations when the code compiles. Multiply/divide operation in a microcontroller takes many cycles.

    I wanted to speed it up by just use shifting, add and, subtract operations, which usually take only few cycles to complete.

    James

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are sure the compiler won't optimize this for you?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer problem or so...
    By TL62 in forum C Programming
    Replies: 19
    Last Post: 01-12-2008, 11:45 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM