populating 2-D array

This is a discussion on populating 2-D array within the C Programming forums, part of the General Programming Boards category; #include <stdio.h> #include <stdlib.h> void getA(int MatrixA[3][3]); int main(void) { int MatrixA[3][3]; getA(MatrixA[3][3]); return 0; } void getA(int MatrixA[3][3]) { ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    4

    populating 2-D array

    #include <stdio.h>
    #include <stdlib.h>

    void getA(int MatrixA[3][3]);

    int main(void)
    {

    int MatrixA[3][3];

    getA(MatrixA[3][3]);

    return 0;
    }

    void getA(int MatrixA[3][3])
    {

    for (int y=0; y<3; y++)
    {

    for (int x=0; x<3; x++)
    {

    int num;
    printf("enter nums");
    scanf("%d", &num);
    MatrixA[y][x]= num;
    }
    }
    }

    when I compile this code, I get the warning
    "warning: passing argument 1 of 'getA' makes pointer from integer without a cast"

    what does that mean and how can I overcome it?

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    In C array indexes start at zero so MatrixA[3][3] element doesn't exist. In fact you have walked off the end of the array ie out-of-bounds. Normally you pass the array name, which is a pointer to its first element, as an argument to a function for manipulation.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Delete the [3][3] in your line of code that calls the function. Leave the rest as is.

    The function prototype says "I'm going to receive an int array address" (cause that's what arrays turn into when passed to a function).

    But the sending line of code is saying "I'm sending you the int at MatrixA[3][3]".

    So you're literally trying to change an integer (since it's an int array), into a pointer, without a cast.

    And no, don't cast it, please.

    As ItCbitC pointed out, not only is it wrong, but MatrixA[3][3] is not an element *in* the MatrixA array. Highest row and column is 2 (0,1,2).

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    thank you!

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by edster View Post
    thank you!
    You're very welcome.

    << but if you don't put code tags (use the # icon in the editing window) around your program next time, the grumps will be after you. >>

    It's amazing how many ways you can try to set this up, and still have it wrong. Tuck your program away for reference, because you're sure to want it later.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10: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