Thread: pointers and structures

  1. #1
    Registered User
    Join Date
    Jan 2003

    pointers and structures

    if i have a structure like this

    Typedef struct CodeNo_Values
    DataType WeighingUnit;
    DataType FinalWeight;
    DataType FreeFall;
    DataType PreliminaryWeight;
    DataType UnderWeight;
    DataType OptimalPreliminaryOutput;
    } CodeNo;

    Typedef struct Timer_Values
    DataType TareTimer;
    DataType TareSteadyTimer;
    DataType PreFeedTimer;
    DataType DischargeTimer;
    DataType StabilityTimer;
    } Timer;

    Typedef struct Protocol_Values
    char Stx;
    char len
    void * Ptr;
    char chksum

    1.) No can i do this without doin malloc?

    Protocol * X;
    Timer *T;
    CodeNo *C;

    x->Stx = 0x45;
    x->Ptr = T; // or it could be C
    x->chksum = 0x56;


    2.) Now where is malloc needed?


  2. #2
    Registered User
    Join Date
    Jun 2003
    No you can't ( let's just say that what you wrote is right ).
    Malloc is needed to dynamiccaly allocate space for your pointer.
    You have to do:
    x = ( Protocol *) malloc ( sizeof( Protocol ) );
    ( Trying to be a good C Programmer )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector of arrays of pointers to structures
    By Marksman in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 03:44 AM
  2. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 05:31 PM
  3. structures with pointers to structures
    By kzar in forum C Programming
    Replies: 3
    Last Post: 11-20-2004, 08:32 AM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Freeing pointers in structures
    By jim50498 in forum C Programming
    Replies: 4
    Last Post: 03-08-2002, 11:53 AM