pointers and structures

This is a discussion on pointers and structures within the C Programming forums, part of the General Programming Boards category; if i have a structure like this Typedef struct CodeNo_Values { DataType WeighingUnit; DataType FinalWeight; DataType FreeFall; DataType PreliminaryWeight; DataType ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    17

    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
    }Protocol;



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

    function_foo()
    {
    Protocol * X;
    Timer *T;
    CodeNo *C;

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

    }

    2.) Now where is malloc needed?


    pal

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    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 ) );
    Loading.....
    ( 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

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