Thread: How to initialize variable in struct?

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    81

    Unhappy How to initialize variable in struct?

    Hi,

    I am programming an OSP project, but I do not know how to initialize a variable in struct. Here is the code:

    Code:
    cpu.h
    
    typedef struct pcb_node PCB;
    
    extern struct pcb_node {
        int    pcb_id;         /* PCB id                                        */
        int    size;           /* process size in bytes; assigned by SIMCORE    */
        int    creation_time;  /* assigned by SIMCORE                           */
        int    last_dispatch;  /* last time the process was dispatched          */
        int    last_cpuburst;  /* length of the previous cpu burst              */
        int    accumulated_cpu;/* accumulated CPU time                          */
        PAGE_TBL *page_tbl;    /* page table associated with the PCB            */
        STATUS status;         /* status of process                             */
        EVENT  *event;         /* event upon which process may be suspended     */
        int    priority;       /* user-defined priority; used for scheduling    */
        PCB    *next;          /* next pcb in whatever queue                    */
        PCB    *prev;          /* previous pcb in whatever queue                */
        int    *hook;          /* can hook up anything here                     */
    };
    
    -------------------------------------------------------------------
    
    cpu.c
    
    PCB *pcb;
    pcb->priority=0; //Error here
    How can fix this error?

    Thanks

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    23
    Code:
    PCB *pcb;                                  <--- This is wrong 
    pcb->priority=0; //Error here
    by declaring pcb as a *pcb, you only created a 4 byte pointer to a PCB struct, and since you did not assign the pointer to point to a valid struct object, you'll probably get error especialy in run time.

    If you wish to use the pointer, you'll need to assign it to point to somewhere, the following will work:
    Code:
    PCB MyPCB;
    PCB *pcb;
    
    pcb = &MyPCB;
    pcb->priority = 0;
    You can also initialise the entire MyPCB to 0 with memset:
    Code:
    PCB MyPCB;
    memset((void *)&MyPCB, 0,  sizeof(MyPCB));  <-- Makesure that MyPCB is not a pointer
    Last edited by Mercury_Linx; 04-19-2003 at 04:19 AM.
    Merc

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You can also initialise the entire MyPCB to 0 with memset:
    Code:
    PCB MyPCB;
    memset((void *)&MyPCB, 0,  sizeof(MyPCB));
    Initializing the entire structure to zero could also be done as follows.
    Code:
    PCB MyPCB = {0};
    This would be better if you wanted to initialize the pointers to NULL (see Q5.16 and Q7.31).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. Help please im stuck
    By ItsMeHere in forum C Programming
    Replies: 7
    Last Post: 06-15-2006, 04:07 AM
  4. Structures help
    By Goosie in forum C++ Programming
    Replies: 29
    Last Post: 06-24-2005, 02:15 AM
  5. Replies: 4
    Last Post: 12-12-2002, 02:32 PM