Thread: default values in struct

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    270

    Question default values in struct

    1) I have this:

    Code:
    struct data 
    {
    	int numOfObjects;
    	int positions[40];
    };
    typedef struct data info;
    I have a variable defined as: info stuff

    How can i set the positions in the array positions to all 0 as default?

    2) If I have an array of those structs (infoArray[]), how can I access the data?
    is it:
    infoArray[i].numOfObjects
    OR
    infoArray[i].stuff.numOfObjects
    ?
    Last edited by taurus; 10-04-2009 at 04:54 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by taurus View Post
    1) I have this:

    Code:
    struct data 
    {
    	int numOfObjects;
    	int positions[40];
    };
    typedef struct data info;
    I have a variable defined as: info stuff

    How can i set the positions in the array positions to all 0 as default?

    2) If I have an array of those structs (infoArray[]), how can I access the data?
    is it:
    infoArray[i].numOfObjects
    OR
    infoArray[i].stuff.numOfObjects
    ?
    Global variables are automatically set to 0 or it's string equivalent. For dynamic arrays, calloc will do the same, malloc will not. Otherwise, you need to use a loop with assignment. Check on your compiler and see what your local variables are set to, initially.

    There is no "infoArray", in your code, just one instance of a struct named stuff. Info is another name, or pseudonyml, for a struct of type data.

    If you did have an array of these structs called "infoArray", then you'd access the data by using infoArray[i].numOfObjects. You'd use the "struct data" or "info" to define the array type, when you first declared the array, only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Help please im stuck
    By ItsMeHere in forum C Programming
    Replies: 7
    Last Post: 06-15-2006, 04:07 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  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. Adding Default values to controls in Dialog boxes
    By juhigarg in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2001, 12:44 AM