Help with 2-D Arrays and Malloc

This is a discussion on Help with 2-D Arrays and Malloc within the C Programming forums, part of the General Programming Boards category; Hi guys, I seriously do not understand how to use malloc to create a two-dimensional array. I understand the concept ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    37

    Help with 2-D Arrays and Malloc

    Hi guys,

    I seriously do not understand how to use malloc to create a two-dimensional array. I understand the concept of malloc and how you can use a pointer to basically create a 1-D array using it. The problem is, I do not understand this pointers to pointers stuff, I don't understand the syntax or how to get it to function properly. I've been searching for help online but nothing has really addressed my specific issue. Can someone be kind enough to either tell me a good online source of help for this or give me a quick run-down (if they are good enough to simplify this problem quickly)? Thanks

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Okay short updated, I found a site that was very helpful and I get the jist of it.

    Now for the next problem: I'm trying to create the dynamic array using malloc in the main function, and then call another function to actually fill the array and print the values. I'm getting an error message when doing it and it won't compile. Can anyone help? Thanks

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    
    { int **createMatrix(int **array,int rows, int columns);
        int **array;
      int rows=2;
      int columns=2;
      int i;
      array=(int**)malloc(rows*sizeof(int*));
    
      for (i=0;i<rows;i++)
      array[i]=(int*)malloc(columns*sizeof(int));
      createMatrix(array,rows,columns);
    
    }
    int **createMatrix(int **array,int rows, int columns)
    {   int i,j;
        for (i=0;i<rows;i++)
        printf("\n");
        {
            for(j=0;j<columns;j++)
            array[i][j]=2;
            printf("%d ",array[i][j]);
        }
    }

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    457
    Code:
    int **createMatrix(int **array,int rows, int columns);
    This line should not be in main, it should be above it. You do not define functions inside other functions.

    main() should return an integer and be declare as

    Code:
    int main(void)

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Okay, thanks for that.

    The bad news is, the program still won't run. I think it might have to do with how I sent the parameter to the function. Is that the proper way of assigning the argument to the double pointer parameter?

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    457
    What error message do you get?

    Note that while it doesn't matter for this particular program, you need to free the memory you malloc. In anything more complex not freeing would result in memory leaks.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    It's a runtime error, the program compiles but then before printing anything it gives a pop-up saying 'whatever.C has stopped working', similar to the message you would get if you forgot the & in a scanf statement.

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    457
    Code:
    int **createMatrix(int **array,int rows, int columns)
    {   int i,j;
        for (i=0;i<rows;i++)
        printf("\n");
        {
            for(j=0;j<columns;j++)
            array[i][j]=2;
            printf("%d ",array[i][j]);
        }
    }
    Your problem comes from not using braces where you should:

    in the first loop over i, since you have no braces, you are just printing a newline i times. Then you are looping over the array column index, but i is already passed rows, so array[i] is no longer valid.

    So, put the brace that is under the printf("\n"); just above it and try again.

    Let this be a lesson to you: Always (no matter what, even if you don't need them) put braces in around all blocks of code that could conceivably need them. It helps break up the code visually, and you will never have something silly like this occur.

    In the future: when you get a segmentation fault when you are using arrays that are dynamically allocated, chances are you ran off the end of one of the dimensions.

    Also: since your createMatrix is not returning anything, why does your declaration of it say it will be returning int **?

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    Thanks, the problem is solved!

    The double pointer return was a leftover from something else I was doing, I never bothered to change it.

    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2010, 03:12 AM
  2. Structures, arrays, pointers, malloc.
    By omnificient in forum C Programming
    Replies: 9
    Last Post: 02-29-2008, 12:05 PM
  3. Confused about malloc and arrays
    By mc61 in forum C Programming
    Replies: 5
    Last Post: 01-15-2008, 06:22 PM
  4. Large arrays, malloc, and strcmp
    By k2712 in forum C Programming
    Replies: 1
    Last Post: 09-24-2007, 09:22 PM
  5. malloc for structs and arrays
    By rkooij in forum C Programming
    Replies: 15
    Last Post: 05-04-2006, 08:38 AM

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