Thread: Problem about struct

  1. #1
    Unregistered
    Guest

    Question Problem about struct

    Dear All,

    I wanna ask that how can I assign a NULL value to a struct?

    thank a lots!

    tkw

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    i have no clue what ya mean...
    you technically can't assign structs to NULL without them being a pointer or acessing a member variable.

    but if your saying you want the values of the structs members to start with NULL then its simple.

    Code:
    #include <memory.h> // header for memset()
    
    typedef struct 
    {
        int i;
        float f;
        char c;
    }mystruct;
    
    int main(void)
    {
        mystruct str;
        memset(&str,NULL,sizeof(mystruct));
        printf("i:%d\nf:%f\nc:%c\n",str.i,str.f,str.c);
        return 0;
    }
    Last edited by no-one; 09-19-2001 at 11:12 AM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well you can initialise all members of a structure to zeros (of the correct type) like this

    mystruct str = { 0 };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct File and array problem Please help
    By theprogrammer in forum C Programming
    Replies: 17
    Last Post: 04-02-2009, 08:05 AM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  4. Replies: 22
    Last Post: 12-23-2008, 01:53 PM
  5. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM