Dynamic Mutli dimensional Array question.

This is a discussion on Dynamic Mutli dimensional Array question. within the C Programming forums, part of the General Programming Boards category; Hi guys, im trying to make a Dynamic Mutli dimensional Array, There are lots of tutorials for making these (using ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    24

    Dynamic Mutli dimensional Array question.

    Hi guys, im trying to make a Dynamic Mutli dimensional Array,

    There are lots of tutorials for making these (using a pointer array of poniters) but not for adding and retriving from them really.

    and im having trouble retriving and adding iformation to the array,

    Ill post my code for adding, creating and resizing the array, and getting information

    Code:
    
    #define oops(s) { perror((s)); exit(EXIT_FAILURE); }
    #define MALLOC(s,t) if(((s) = malloc(t)) == NULL) { oops("error: malloc() "); }
    
    double **dynarray;
    int dynArraySize = 0;
    int currentRow = -1;
    const int COLUMS = 2;
    const int INCREMENT = 10;
    
    void createDynarray(double **arrayPtr)
    {
    	int i;
    
    	MALLOC(arrayPtr, sizeof(double *) * (INCREMENT));
    	for (i = 0; i < INCREMENT; i++)
    	{
    	    MALLOC(arrayPtr[i], sizeof(double) * COLUMS);
    	}
    	dynArraySize = 10;
    }
    void resizeDynarray(double **arrayPtr)
    {
    	double **tmp;
    	int i;
    	  if (currentRow >= dynArraySize)
    	  {
    	    if(tmp = realloc(arrayPtr, sizeof(double *) * (dynArraySize + (INCREMENT))) == NULL)
    	    {
    	      oops("realloc() error! ");
    	    }
    	    for(i = dynArraySize; i < (dynArraySize + INCREMENT); i++)
    	    {
    	      MALLOC(tmp[i], sizeof(double) * COLUMS);
    	    }
    	    dynArraySize += INCREMENT;
    	    arrayPtr = tmp;
    	  }
    }
    
    void addDoubleToArray(double **arrayPtr,double *data)
    {
    	int i;
    	double **tmpPtr;
    
    	currentRow++;
    	if(currentRow < dynArraySize)
    	{
                       arrayPtr += currentRow;
    	   arrayPtr = data;
    	}
    	else
    	{
    		resizeDynarray(arrayPtr);
    		addDoubleToArray(arrayPtr,data);
    	}
    }
    double *getDoubleFromArray(double **arrayPtr,int index)
    {
    	double *retVal;
    	if(index <= dynArraySize)
    	{
    		arrayPtr += index;
    		retVal = arrayPtr;
    	}
    	else
    	{
    	 oops("Array out of bounds error! ");
    	}
    	return retVal;
    }

    I get a program crash when ever i try to retrive something from the array (when i call getDoubleFromArray)

    its probly sometihing simple but im stumped? anyone see the problem? (i havent really dealt to much with pointers to pointers so i could be doing something very wrong i guess heh)

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well it could be the fact that you're not even trying to pay attention to your data types.
    Code:
    double *getDoubleFromArray(double **arrayPtr,int index)
    {
    	double *retVal;
    	if(index <= dynArraySize)
    	{
    		arrayPtr += index;
    		retVal = arrayPtr;
    One of these things is not like the other...


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

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    24
    Ahh good point,

    i think is where i confused with pointers to pointers,

    If i do retVal = *arrayPtr;

    That means retval equals the pointer that arrayPtr was pointing to right?

    so that means if ArrayPtr points to Pointer which points to 10,
    retVal would = the Pointer which points to 10 (so the memory address of 10) right?

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    No. Don't mix up your operators your * would refer to the value of the variable your & is the address of

    If you do
    Code:
    retVal = *arrayPtr;
    We would assume retVal is plain data which is being assigned with the value of pointer arrayPtr in otherwords, if arrayPtr points to memory that stores a 10. Then retVal would equal 10.

    Code:
    #include <stdio.h>
    
    int main() {
      int foo, baz = 10;
      int *bar;
    
      bar = &baz;  // Points to the address of baz
    
      foo = *bar;  // Equals the value that bar is pointing to
    
      printf("%d", foo);
    }
    Last edited by SlyMaelstrom; 02-22-2006 at 05:25 PM.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    24
    hmmmm okay

    So arrayPtr is a pointer that points to and array of pointers;
    So if wanted to change one of the pointers in the array,
    i though i could just do arrayPtr[index] = NewPointer;
    but that crashed,

    So to do it another way maybe (arrayPtr + index(do i need a sizeof here?)) = Newpointer

    but that didnt work either?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well you're almost right again. retVal is in this case a pointer. Thus, they're right when they say:
    Quote Originally Posted by fatdunky
    If i do retVal = *arrayPtr;

    That means retval equals the pointer that arrayPtr was pointing to right?

    so that means if ArrayPtr points to Pointer which points to 10,
    retVal would = the Pointer which points to 10 (so the memory address of 10) right?
    Yes. retval is a pointer. Thus, if arrayPtr points at a pointer, dereferencing that gives you a pointer. You then assign that pointer directly to retval. Just like doing this:
    Code:
    int *ptr1, *ptr2;
    int x = 5;
    ptr2 = &x;
    
    ptr1 = ptr2;
    Here you are assigning the value of ptr2 to ptr1. Thus, ptr1 effectively points to x.


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

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    24
    hehe i think i figured out what my problem was *blush* i was correctly creating the array and it was null, if you look at the create array i was just creating a tempory array

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic structure with array and array count
    By Nazgulled in forum C Programming
    Replies: 14
    Last Post: 06-08-2007, 10:10 PM
  2. Replies: 4
    Last Post: 11-02-2006, 10:41 AM
  3. dynamic array
    By linuxdude in forum C Programming
    Replies: 5
    Last Post: 07-13-2004, 02:33 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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