Help!!! Urgent

This is a discussion on Help!!! Urgent within the C Programming forums, part of the General Programming Boards category; How can I pass a 2D array to a func without specifing the size of the array!!! Say, I my ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    29

    Unhappy Help!!! Urgent

    How can I pass a 2D array to a func without specifing the size of the array!!!

    Say, I my size of array is not fixed and hence I cannot specify array[][4] in the declaration...so what is the solution?

    #include <stdio.h>
    void printArray(int array[ ][4]);

    main() {
    int array[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
    printArray(array);
    return 0;
    }

    void printArray(int array[][4], int m, int n) {
    int i, j;

    for(i=0 ; i<3 ; i++) {
    for(j=0 ; j<4 ; j++) {
    printf("%2d ", array[i][j]);
    }
    printf("\n");
    }
    }
    C.B.Ashesh,
    Hyderabad, India

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >Help!!! Urgent

    >How can I pass a 2D array to a func without specifing the size of the array!!!
    http://www.eskimo.com/~scs/C-faq/q6.19.html

    Investigate [code] [/code] tags.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Also,
    >>main()
    This is wrong, main() only returns integer, so the right way to write this is int main() and then return a value at the end of the function.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    29
    How do I print the values in the function after passing the array?

    What is the statement for ??????? in the prog below?

    #include <stdio.h>

    void f2(int *aryp, int m, int n);

    int main() {
    int array[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
    printArray(array);
    f2(&array[0][0], 3, 4);
    return 0;
    }

    void f2(aryp, nrows, ncolumns)
    int *aryp;
    int nrows, ncolumns;
    {
    int i,j;
    for(i=1;i<nrows;i++){
    printf("\n");
    for(j=1;j<=ncolumns;j++) {
    printf("%d\b ",??????????);
    }
    }
    printf("\n");
    }
    C.B.Ashesh,
    Hyderabad, India

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >>How do I print the values in the function after passing the array?
    >>What is the statement for ??????? in the prog below?

    >http://www.eskimo.com/~scs/C-faq/q6.19.html

    The part that says "array[i][j] is accessed as aryp[i * ncolumns + j]" was a hint. (But then again, so were [code] [/code] tags.) And your loops should be as follows.
    Code:
    for(i=0;i<nrows;i++){
    for(j=0;j<ncolumns;j++) {
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68
    if you don't know the size of the 2D array, try making one dynamically,

    Code:
        int ** array;
    
        array=(int**)malloc(sizeof(int*)*ROW); //ROW= no of rows in the array
    
        for (i=0;i<ROW;i++)
               array[i]=(int*)malloc(sizeof(int)*COLUMN);
     //COLUMN= no of columns in the array

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. display character size...(quite urgent..)
    By karthi in forum C Programming
    Replies: 10
    Last Post: 07-11-2007, 10:42 PM
  2. beginner plzz help urgent
    By sara101 in forum C Programming
    Replies: 11
    Last Post: 01-14-2007, 10:38 PM
  3. Linked List Need Help Urgent
    By ykchua in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 03:57 PM

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