Thread: How to use Pointer inside a Structure

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    11

    How to use Pointer inside a Structure

    I hav made a structure in my C program:

    Code:
    typedef struct species
    { int Trophic;
      int Niche[10000];
    } species;
    So each species has a Trophic value and a Niche array.
    The Niche array can at maximum carry 9999 elements (0 to 9999). But in reality, MOST of the species take JUST 2 or 3 elements in their Niche array. Just 1 or 2 species use a large number of Niche elements. But still I am having to assign a size of 10000 to every species' Niche array. That must be taking a lot of memory.

    Now the number of Niche elements that a specie occupies in its niche array (i.e how many elements of the niche array are occupied) is decided as the program runs. But once decided, it remains constant.

    So I am trying to use Dynamic Memory Allocation for the Niche array so that the species that take a few Niche elements are assigned a small size to their Niche array and the species that take a larger number of Niche elements get larger size to their Niche array. But I am not able to use a pointer inside structure.

    I am very new to pointers etc. But still have studied & practiced a bit of them but not able to use it in structures.

    Can someone please guide me.

    Regards
    Alice

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, you put a pointer inside a structure and use it.

    Code:
    typedef struct species {
        int Trophic;
        int n_of_Niches;
        int *Niches;
    } species;
    You'll have to allocate memory for each "array" when you make a new species thing.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Or malloc for one huge Niche array that guesses at the amount required for all species. The structure can then point to specific index in that array where that species' elements start. If you need to increase that array, use realloc.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by nonoob View Post
    Or malloc for one huge Niche array that guesses at the amount required for all species. The structure can then point to specific index in that array where that species' elements start. If you need to increase that array, use realloc.
    I don't think that's such a good idea. realloc might give you a pointer to a whole new chunk of memory instead of simply extending what you have. Then you have to go through every instance of a species and update the Niche element to point to the right place in the new array. Not trivial unless you bother keeping track of the absolute offset in the old jumbo array or remember the old and new base address and do some arithmetic. And that's assuming you have a nice collection of species where you can find all instances and update them in one fell swoop.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    11

    Re: How to use Pointer inside a Structure

    Thanx a ton tabstop, nonoob, anduril462

    I have developed a short code (using calloc). Can u please check it carefully if I have made any mistakes ,like memory leaks etc.. (although I have checked it by printing the outputs, but am not shure about memory leaks)

    Code:
    typedef struct species
    {
        int * niche;
    } species;
     
    void main()
    {
      const int TotalSpecies = 3;
      species s[TotalSpecies];
      int i,j,nichesize;
    
      for( i = 0; i < TotalSpecies; i++)
       {
    	  printf("enter the niche size of specie %d	\n",i);
    	  scanf("%d",&nichesize);
    
    	  s[i].niche = calloc(nichesize,sizeof(int));
    
    	  printf("enter the niche value(s) of specie %d\n",i);
    
      	  for(j=0;j<nichesize;j++)
    	   	  scanf("%d",&s[i].niche[j]);
       }
    
    
       for( i = 0; i < TotalSpecies;i++)
    		 free(s[i].niche);
    
    }
    Thanx a lot u all
    Regards
    Alice

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Looks OK - apart from void main is wrong
    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.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Quote Originally Posted by anduril462 View Post
    I don't think that's such a good idea. realloc might give you a pointer to a whole new chunk of memory instead of simply extending what you have. Then you have to go through every instance of a species and update the Niche element to point to the right place in the new array. Not trivial unless you bother keeping track of the absolute offset in the old jumbo array or remember the old and new base address and do some arithmetic. And that's assuming you have a nice collection of species where you can find all instances and update them in one fell swoop.
    You're right. Then the "pointer" would be better off being an integer index. An offset relative to the beginning of the array, no matter which absolute memory address it's based at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to pass pointer to a structure in a message queue
    By smitsaboo in forum C Programming
    Replies: 4
    Last Post: 08-15-2009, 11:33 PM
  2. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  3. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM