returning a pointer of a struct of a struct array...

This is a discussion on returning a pointer of a struct of a struct array... within the C Programming forums, part of the General Programming Boards category; Greetings, I am a bit new at programming and am working on a project for my own amusement. The problem ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    2

    Question returning a pointer of a struct of a struct array...

    Greetings,

    I am a bit new at programming and am working on a project for my own amusement. The problem is I have created a structure and I want to make an array of the structure, but do it in a fuction in such a way that I can create the elements at declaration then be able to use that information in main.

    So I decided to try to use a pointer to the struct and return that to a declared pointer in main - I wasn't sure how else to do this.
    Anyway - here's what I have. Any help would be appreciated!

    Code:
    #include <stdio.h>
    
    typedef struct nountypes
    {
    	char n[15];
    }nouns;
     
    nouns* defNouns();
    
    int main(void)
    { 
    	nouns *nWords;
    
    	*nWords = defNouns();	
    }
    
    nouns* defNouns()
    {
    	nouns *nWordsptr;
    	nouns nWords[] = {"sun", "ship", "darknes"};
    
    	*nWordsptr = nWords[0];
    
    	return *nWordsptr;
    }
    Trying to compile this I get 2 'incompatible type' errors.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You shouldn't return the address of a non-static variable that is local to the function you're in (which is what you're trying to do).

    You have these choices
    - make the nWords array static
    - make the memory allocation for nWords dynamic (use malloc to create it)
    - make the nWords array a global variable instead, and forget the defNouns function altogther.

    Here's a static example.
    Code:
     #include <stdio.h>
     
     typedef struct  nountypes
     {
       char  n[15];
     } nouns;
     
     nouns *defNouns(void);
     
     int main(void)
     {
       nouns *nWords;
       int   i;
     
       nWords = defNouns();
     
       for (i = 0; nWords[i].n[0] != '\0'; i++)
       {
         puts (nWords[i].n);
       }
       
       return 0;
     }
     
     nouns *defNouns(void)
     {
       static nouns  nWords[] = {{{"sun"}}, {{"ship"}}, {{"darknes"}}, {{""}}};
       return(nWords);
     }
    And here's a global version
    Code:
     #include <stdio.h>
     
     typedef struct  nountypes
     {
       char  n[15];
     } nouns;
     
     #define NWORDS_COUNT 3
     nouns  nWords[NWORDS_COUNT] = {{{"sun"}}, {{"ship"}}, {{"darknes"}}};
     
     int main(void)
     {
       int   i;
     
       for (i = 0; i < NWORDS_COUNT; i++)
       {
     	puts (nWords[i].n);
       }
       
       return 0;
     }
    If you're up for a malloc version, ask and I'll post one (or someone will probably beat me to it )

    I've also shown you two different ways of determining the number of elements in the array. The first code used a zero length string in the structure of the last element of the array. The second code used a #define to hard code the number of elements.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct (with pointer) to byte array
    By rabencor in forum C Programming
    Replies: 1
    Last Post: 02-08-2009, 04:27 AM
  2. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 02:17 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  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

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