Problem with arrays inside a structure

This is a discussion on Problem with arrays inside a structure within the C Programming forums, part of the General Programming Boards category; Hi, Here is my program. I've several queries related to it. Code: #include<stdio.h> struct _test { int iNum[5]; }; typedef ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Location
    Mysore, India
    Posts
    14

    Question Problem with arrays inside a structure

    Hi,

    Here is my program. I've several queries related to it.

    Code:
    #include<stdio.h>
    struct _test
    {
    	int iNum[5];
    };
    typedef struct _test test;
    int main(int argc, char** argv)
    {
    	test newTest;
    	newTest.iNum[0] = 1;
    	newTest.iNum[1] = 2;
    	newTest.iNum[2] = 3;
    	newTest.iNum[3] = 4;
    	newTest.iNum[4] = 5;
    	printf("%d\n",newTest.iNum[0]);
    	printf("%d\n",newTest.iNum[1]);
    	printf("%d\n",newTest.iNum[2]);
    	printf("%d\n",newTest.iNum[3]);
    	printf("%d\n",newTest.iNum[4]);
    }
    1. Can I have an array without the size being specified inside a structure? For eg: Can I have it like this?
    Code:
    struct _test
    {
    	int iNum[];
    };
    2. In the above program, I've initialized each element of the array individually. Is there any way to do it in one go? When I initialize it like
    Code:
    newTest.iNum = {1,2,3,4,5};
    I'm getting an error.

    Plz help..

    Thanks,
    Babu

  2. #2
    Registered User
    Join Date
    Jun 2007
    Posts
    6
    1) Yes you can, but you have to take care of the memory allocation for it. For example you can do:
    Code:
    struct _test
    {
    	int *iNum;
    };
    
    // Somewhere else, where n is the size you like.
    struct _test a;
    a.iNum = malloc(sizeof(int)*n);
    2) I'm not sure about it, but GCC seems to support a way to do it:
    Code:
    struct _test a =
    {
    	.iNum = {0,1,2,3,4},
    };
    Last edited by XayC; 07-12-2007 at 09:41 AM.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Location
    Mysore, India
    Posts
    14
    I'm using Visual C++ IDE and I'm unable to initialize it in a single statement. I'm getting a syntax error instead. Can somebody tell me how to do it?
    Last edited by babu; 07-12-2007 at 10:15 AM. Reason: To add more info

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >2. In the above program, I've initialized each element of the array individually. Is there any way to do it in one go?
    Code:
    	test newTest = {{1,2,3,4,5}};

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    6
    If you want to initialize it you can do it this way (works with MS Visual C++ too):
    Code:
    int main(int argc, char** argv)
    {
        test newTest = {{0,1,2,3,4}};
    
    	printf("%d\n",newTest.iNum[0]);
    	printf("%d\n",newTest.iNum[1]);
    	printf("%d\n",newTest.iNum[2]);
    	printf("%d\n",newTest.iNum[3]);
    	printf("%d\n",newTest.iNum[4]);
    }
    This works because you initialize it when allocated (declared), but you cant initilize it in this way after.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem about Creating structure
    By albert3721 in forum C Programming
    Replies: 3
    Last Post: 06-05-2007, 08:33 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Zeroing out member arrays in a Structure
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 03-26-2004, 03:50 AM
  4. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM
  5. Structure problem
    By mattz in forum C Programming
    Replies: 10
    Last Post: 11-30-2001, 01:19 PM

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