how do i create an array...

This is a discussion on how do i create an array... within the C Programming forums, part of the General Programming Boards category; how do i a function to create an array of n amount of structures dynamically and then to return a ...

  1. #1
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85

    how do i create an array...

    how do i a function to create an array of n amount of structures dynamically and then to return a pointer to array. So far I have ....

    Code:
    typedef struct{
    	char *name;
    	int day_of_birth;
    	int month_of_birth;
    	int year_of_birth;
    } data_t;
    
    
    data_t
    create_data_t_array(int n) {
    	int i;
    	data_t *ptr;
    	ptr = malloc(sizeof(*ptr));
    	for (i = 0; i < n; i++)
    	{
    	ptr[i] = malloc(sizeof(*ptr))	
    	}
    	return ptr;		 
    }
    but i dont think will work. pls help!

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    You nearly had the correct solution:

    1. First of all, your function shouldn't return a data_t but instead a pointer to data_t
    2. The way you're allocating your array inside the function is wrong. Just do it like you would do for integers:
    Code:
    int *array = malloc(100 * sizeof(*array));
    The code you want is the following:

    Code:
    data_t *create_data_t_array(int n) {
    	data_t *ptr;
    	ptr = malloc(n * sizeof(*ptr));
    	return ptr;
    }
    
    int main(void)
    {
      data_t *myArray = create_data_t_array(10);
      return 0;
    }

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Also, don't forget to free() what you *alloc()!
    Code:
    data_t *create_data_t_array(int n) {
    	data_t *ptr;
    	ptr = malloc(n * sizeof(*ptr));
    	return ptr;
    }
    
    void free_data_t_array(data_t *array, int n) {
        int x;
    
        for(x = 0; x < n; x ++) {
            free(array[n]);
        }
    
        free(array);
    }
    
    int main(void)
    {
      data_t *myArray = create_data_t_array(10);
      free_data_t_array(myArray, 10);
      return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85
    should I also have to initialize it first :
    Code:
    void
    init_array (data_t *pt_arrayr, int n) {
    	int i;
    	for (i = 0; i < n; i++)
    	{
    		ptr_array[i].name = {0};
    		ptr_array[i].day_of_birth = 0;
    		ptr_array[i].month_of_birth = 0;
    		ptr_array[i].year_of_birth = 0;
    	}
    }
    is this nessecary?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    ptr_array[i].name = {0};
    What are you trying to do? I assume you're trying to zero fill an array. Well, that only works at declaration time. You should be getting a compiler alert here about it.

    [edit]
    On looking back, name is just a pointer, and as such, the line should be:
    Code:
    ptr_array[ i ].name = NULL;
    [/edit]

    [edit2] Omitted structure member added. [edit2]

    In general, it's a good idea to initialize everything before you try to do something meaningful with it. Here, it depends on how you're keeping track of what's valid and what's invalid data.



    Quzah.
    Last edited by quzah; 04-10-2007 at 11:44 PM.
    Hope is the first step on the road to disappointment.

  6. #6
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85
    what would you recommend a as a good way to take input from keystroke. I have started writing a read_into_function but am having trouble formulating a loop system for it to iterate through each element, and then to the next structure in the array etc etc

  7. #7
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85
    I tried ptr_array[i] = NULL, I had to define NULL as 0 and then the compiler didn't like it :

    c:34: error: incompatible types in assignment ?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here are some FAQs on getting a number and a line, since I'm not sure what you're having trouble with.

    See the second edit, I had omitted the structure member on my copy/paste job.


    Quzah.
    Last edited by quzah; 04-10-2007 at 11:44 PM.
    Hope is the first step on the road to disappointment.

  9. #9
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85
    I want to pass information into members of the array of structures. I know the number of structure, n, and I have a pointer to the array. I want to step through each member of structure collecting the fields and then I want to move on to the next structure in the array.

    something like this, but im not sure how to walk though each member of each structure within the array. I though about using an if(ch=="\n"), then iterate, but how do iterate the pointer, like this : ptr_array++?

    Code:
    data_t 
    read_into_array(bday_t *ptr_array, int n) {
    		int next;
    		printf("Enter a name, date of birth, month of birth, and year of birth: \n");
    		
    		values_read = scanf("&#37;s %d %d %d", ptr_array->name, &(ptr_array->day_of_birth), &(ptr_array->month_of_birth, &(ptr_array->year_of_birth));

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You might consider something like his:
    Code:
    int getrecord( struct foo *bar )
    {
        if( bar != NULL )
        {
            ...prompt for each member's data for this structure...
            ...put it into bar as you do so...
            return 0; /* return OK, whatever you want */
        }
        return 1; /* return ERROR, whatever you want */
    }
    
    ...
    
    for( x = 0; x < num; x++ )
    {
        if( getnum( array[ x ] ) == 0 ) /* OK! */
        {
            ...do whatever...
        }
        else /* So sad. */
        {
            dieAHorribleDeath( );
        }
    }
    Or if you prefer using a pointer.
    Code:
    for( ptr = array; ptr < array + arraylen; ptr++ )
    {
         getnum( ptr ); /* Add error checking. */
    }

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. Create random array
    By Hunter_wow in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2007, 06:29 AM
  3. How to create multi-dimension string array
    By mihu in forum C Programming
    Replies: 2
    Last Post: 04-02-2006, 11:09 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

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