Thread: Passing 2-D array to a Function

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    69

    Passing 2-D array to a Function

    Hello,

    Code:
    #include <stdio.h>
    int first (int(*q), int, int);
    int second (int(*q)[4], int, int);
    int main(){
    int s[3][4]={1, 2, 3, 4, 
                 5, 6, 7, 8, 
                 9, 10, 11, 12};
    first (s, 3, 4);
    second (s, 3, 4);
    return 0;
    }
    int first (int *q, int x, int y)
    { 
    int i, j;
    for (i=0; i<x; i++)
    {
    for (j=0; j<y; j++)
    printf ("%d ", *(q+i*y+j));
    printf ("\n");
    }
    printf ("\n");
    return (*(q+i*y+j));
    }
    int second (int (*q)[4], int x, int y) 
    {
    int i, j, *p;
    for (i=0; i<x; i++)
    {
    p=q+i;
    for (j=0; j<y; j++)
    printf ("%d ", *(p+j));
    printf ("\n");
    }
    return (*(p+j)); 
    }
    when i compile this code why it gives me bellow shown two warnings -

    Warning W8075 array23.c 8: Suspicious pointer conversion in function main
    Warning W8075 array23.c 29: Suspicious pointer conversing in function second


    would you please explain about the printf ("%d ", *(q+i*y+j)); statement.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You really need to indent your code properly, e.g.,
    Code:
    #include <stdio.h>
    
    int first(int(*q), int, int);
    int second(int(*q)[4], int, int);
    
    int main()
    {
        int s[3][4] = {1, 2, 3, 4,
                       5, 6, 7, 8,
                       9, 10, 11, 12};
        first(s, 3, 4);
        second(s, 3, 4);
        return 0;
    }
    
    int first(int *q, int x, int y)
    {
        int i, j;
        for (i = 0; i < x; i++)
        {
            for (j = 0; j < y; j++)
                printf("%d ", *(q+i*y+j));
            printf("\n");
        }
        printf("\n");
        return(*(q+i*y+j));
    }
    
    int second(int (*q)[4], int x, int y)
    {
        int i, j, *p;
        for (i = 0; i < x; i++)
        {
            p = q + i;
            for (j = 0; j < y; j++)
                printf("%d ", *(p + j));
            printf("\n");
        }
        return(*(p+j));
    }
    Anyway, the warnings that you are getting likely have to do with the fact that s is an array of 3 arrays of 4 ints, so it is converted to a pointer to an array of 4 ints. However, in main, you pass it as an argument to a function that expects a pointer to an int:
    Code:
    first(s, 3, 4);
    Then in second, you do pointer arithmetic with the pointer parameter that is a pointer to an array of 4 ints, but then assign the result to a pointer to an int instead:
    Code:
    p = q + i;
    Quote Originally Posted by Shankar k
    would you please explain about the printf ("%d ", *(q+i*y+j)); statement.
    The author decided to reinterpret the array of arrays as a single contiguous array of elements. The idea is basically to compute the index such that you get the equivalent of:
    Code:
    printf("%d ", s[i][j]);
    where s is the array from main. Frankly, this code has overuse of pointer notation where array notation is appropriate.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    warnings are caused by your code trying to convert from one type of pointer to another without indicating to the compiler that this is really what you want

    This is what I'd do to remove the compilers doubt
    Code:
    #include <stdio.h>
    int first (int(*q), int, int);
    int second (int(*q)[4], int, int);
    int main()
    {
        int s[3][4]= {{1, 2, 3, 4},
                      {5, 6, 7, 8},
                      {9, 10, 11, 12}
                     };
        first (s[0], 3, 4);
        second (s, 3, 4);
        return 0;
    }
    int first (int *q, int x, int y)
    {
        int i, j;
        for (i=0; i<x; i++)
        {
            for (j=0; j<y; j++)
                printf ("%d ", *(q+i*y+j));
            printf ("\n");
        }
        printf ("\n");
        return (*(q+i*y+j));
    }
    int second (int (*q)[4], int x, int y)
    {
        int i, j, *p;
        for (i=0; i<x; i++)
        {
            p=q[i];
            for (j=0; j<y; j++)
                printf ("%d ", p[j]);
            printf ("\n");
        }
        return (*(p+j));
    }
    your code is trying to play the fact that pointer to the array and pointer to the first element of the array have same values (while not same type)

    *(q+i*y+j) - is a result of "flattening" of the 2D array into 1D array. This is pointer arithmetic that lies behind the simple expression s[i][j]

    PS. Noted that your first and second functions access out-of-bounds array member in their return statements
    Last edited by vart; 04-24-2015 at 11:25 PM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    The way you are passing the arguments to the first and second function are wrong. For the first function you have to pass it type int * and for the second of type int (*)[4]. Hence it is
    Code:
    first (&s[0][0], 3, 4);
    second (&s[0], 3, 4);

    And in function second p is of type int *, but q is type int (*)[4]. so redeclare it also as same type as q.

    In the printf statement in the second function it could probably be (*p)[j] or *(*p +j).

    same thing for return statement and i think return statements are not required in both the functions.

  5. #5
    Registered User
    Join Date
    Apr 2015
    Posts
    69
    Hi All,

    Thanks for the reply,

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Satya
    The way you are passing the arguments to the first and second function are wrong. For the first function you have to pass it type int * and for the second of type int (*)[4]. Hence it is
    Code:
    first (&s[0][0], 3, 4);
    second (&s[0], 3, 4);
    You are mistaken for the call to the function named second: since an array is converted to a pointer to its first element in this context, this:
    Code:
    second(s, 3, 4);
    is equivalent to:
    Code:
    second(&s[0], 3, 4);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2015
    Posts
    69
    Hello,

    thank you very much for your clarification.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 09-25-2012, 01:31 AM
  2. Passing Array To Function & Display Array Contents
    By mcertini in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2010, 01:32 PM
  3. passing 2d array to function
    By Tupcia in forum C Programming
    Replies: 3
    Last Post: 04-13-2008, 11:33 AM
  4. Passing my array to function
    By pooty tang in forum C Programming
    Replies: 8
    Last Post: 09-15-2004, 12:19 PM
  5. passing 2D array to a function
    By ashesh in forum C Programming
    Replies: 4
    Last Post: 06-09-2003, 11:16 PM