Thread: Storage classes

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    41

    Storage classes

    Code:
    void mycreatedynamicstorage(int **MyArray)
    {
    
    
    by malloc -allocate some 2D storage for the variable above.
    
    
    }
    
    void populatestorage(int **MyArray)
    {
    
    Simply populate the sample.
    
    
    }
    
    int main(void)
    {
    
    int **MyArray;
    mycreatedynamicstorage(MyArray);
    populatestorage(MyArray);
    
    
    }

    My question comesdown to storage class. I have following set-up. The create routine works as it should and creates an array,

    but when it is sent to the next function it seems that I get a segmentaion fault and it would appear the array has gone

    missing. Should I use a storage class for this. Now these routines are tested and work if I dont not explicity call the arrays

    in the function and place the int **MyArray out of the main and into global. Please help if you can, I would send the code, but

    I amworking on Linux without a connection so have had to type in the salient points. Thanks

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    in order for mycreatedynamicstorage() to work, main() has to pass it a pointer to pointer to pointers (whew!) -- that's three stars, not two. That allows mycreatedynamicstorage() to change the pointer declared in main().
    Code:
    const int MaxX = 20;
    const int MaxY = 20;
    void mycreatedynamicstorage(int ***MyArray)
    {
    	int i;
    	int ** ay = malloc(MaxY * sizeof(int *));
    	for(i = 0; i < MaxY; i++)
    		ay[i] = malloc(MaxX * sizeof(int));
    	*MyArray = ay;
    }
    or better yet, pass the x and y values
    Code:
    void mycreatedynamicstorage(int ***MyArray, int x, int y)
    {
    	int i;
    	int ** ay = malloc(y * sizeof(int *));
    	for(int i = 0; i < y; i++)
    		ay[i] = malloc(x * sizeof(int));
    	*MyArray = ay;
    
    }
    Last edited by Ancient Dragon; 12-02-2005 at 09:21 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yet more cross-posting - sheesh
    Lot of reading of the forum rules not being done.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  3. need help with storage classes
    By datainjector in forum C Programming
    Replies: 1
    Last Post: 07-03-2002, 04:27 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM