Thread: Pointers to pointers with arrays

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    2

    Pointers to pointers with arrays

    Brand new member who wants help because their program is due and they're jammed up? I'm sure it has never happened before ever! To be fair I have been trying to get help from the instructor and other programmers and no one has been responding. Any how...

    I'm relatively sure the problem is somewhere within the fillACat function and/or the dynamicCatArray. I'm pretty sure the problem is with the pointer/array * - but I have no idea how to fix it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    typedef struct {
    	double weight;
    	char name [30];
    	short int neutered;
    } CAT;
    
    void fillACat (CAT *dynamicCatArray[], char *names[], int i);
    
    CAT* makeACat () {
    	CAT* result;
    
    	result = malloc (sizeof (CAT));
    
    	return result;
    }
    
    main () {
    	char *names[6] = {"Fluffy", "Tigger", "Max", "Betty", "Cat-27" , "Jake"};
            int i;
    	CAT** dynamicCatArray;
    
    	dynamicCatArray = calloc (5, sizeof (CAT*));
    
    	srand (time(NULL));
    
    	for (i = 0; i < 5; i++) {
    		fillACat (dynamicCatArray, names, i);
    	}
    
    	free (dynamicCatArray);
    	system ("pause");
    }
    
    void fillACat (CAT *dynamicCatArray[], char *names[], int i) {
    	int catName, catWeight, catNeutered;
    
    	catName = rand () % 6;
    	catWeight = rand () % 30;
    	catNeutered = rand () % 2;
    
    	strcpy (dynamicCatArray[i]->name, names[catName]);
    	dynamicCatArray[i]->neutered = catNeutered;
    	dynamicCatArray[i]->weight = catWeight;
    }
    Any help that anyone can offer would be appreciated. I apologize in advance for any rules I might be breaking. Thanks
    Last edited by Nurumla; 07-17-2011 at 07:58 PM. Reason: Remove unnecessary code.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your dynamic array is an array of five pointers-to-cat, yet you are treating it as though it held five actual cats. It does not. Either you want what you have, in which case you need to make those pointers-to-cat point to an actual cat at some point along the way; or you really just wanted an array of cats, in which case you have one extra *.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    2
    Ah ha! I see. You are very helpful . Thank you.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    A couple of notes:

    1. The bare bones of a c program are:
    Code:
    int main (void){
    
         return (0);
    }
    Read the FAQ-main declaration for the discussion as to why.

    2. You never check the return of malloc and calloc to see if the allocation of memory actually worked. This is an extremely poor practice and is something you should work very hard to rid yourself of. If the allocation fails you will have a NULL pointer which will cause problems later in your code.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  2. Pointers to 2D arrays
    By bertazoid in forum C Programming
    Replies: 16
    Last Post: 02-26-2009, 04:46 PM
  3. pointers and arrays
    By yuri322 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2006, 01:22 PM
  4. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  5. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 01:48 PM