Thread: Quick struct question

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    58

    Quick struct question

    I have this say:
    Code:
       
         struct temp1 {
                     char   *p;
                     short len;
          };
    Later on i have this : struct temp1 ARRAY[] = { some stuff};

    So instead of the above i want to do this:
    introduce another struct and want to do this:
    Code:
         struct temp2 {
               struct temp1 ARRAY[];
           };
    
    struct temp2  *ptr;
    
    How do i assing ARRAY to some values is it like this:
    ptr->ARRAY[]={ some stuff}???
    Thanks

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Here is an example to do it:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    struct temp1 {
                     char   *p;
                     short len;
          };
    struct temp2 {
               struct temp1 arr[3];
           }*ptr;
    int main(void)
    {
    	ptr=malloc(50);
    	(ptr->arr[0]).p="HELLO";
    	(ptr->arr[0]).len=56;
    	printf("%s\n%d",(ptr->arr[0]).p,(ptr->arr[0]).len);
             return 0;
    }
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    58
    Thats great but my problem is i want to assign something to the array at global scope.??

    Sorry should of said that!

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    I dont think if you can do that coz you need to have space allocated for ptr using malloc first which I dont think can be done globally. Without malloc ptr will be uninitialized pointer which will point in any arbritrary address causing segmentation fault.
    Someone else can throw some light on it.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Also, arrays have a fixed size so you can't declare an array like this:
    Code:
    int arr[];
    You can however initialize it with something in which case the size is set implicitly:
    Code:
    int arr[] = {1, 2, 3};

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 05:38 PM
  2. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  3. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  4. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM