Thread: Pointer to variable length array

  1. #1
    Registered User
    Join Date
    Jul 2020
    Posts
    7

    Pointer to variable length array

    Refer below code for variable length array:
    Code:
    #include <stdio.h>
    
    
    int main()
    {
        int row, col;
    
    
        printf("Enter row: ");
        scanf("%d", &row);
    
    
        printf("Enter column: ");
        scanf("%d", &col);
    
    
        int X[row][col], i, j;
    
    
        printf("Enter the numbers:\n");
    
    
        for(i=0; i<row; i++)
        {
            for(j=0; j<col; j++)
            {
                scanf("%d", &X[i][j]);
            }
        }
    
    
        printf("You have entered...\n");
    
    
        for(i=0; i<row; i++)
        {
            for(j=0; j<col; j++)
            {
                printf("%d\n", X[i][j]);
            }
        }
    
    
        return 0;
    }
    Now in order to retrieve the array values using an UDF (display), I can write following code in main:
    Code:
    int (*p)[c];
    p=X;
    void display(X);
    But how do I write the function as compiler doesn't allow me to define something like:
    Code:
    void  display(int (*p)[c])
    {
        //some code
    }
    Would appreciate any guidance in this regard.
    Last edited by dungeon; 08-05-2020 at 12:18 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,091
    Try:
    Code:
    void  display(int row, int col, int x[row][col])
    I rarely use VLAs so I could be mistaken, but I believe you could leave out the row in the brackets as it's the usual array decay to pointer, but you do need col.
    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
    Registered User
    Join Date
    Jul 2020
    Posts
    7
    Quote Originally Posted by laserlight View Post
    Try:
    Code:
    void  display(int row, int col, int x[row][col])
    I rarely use VLAs so I could be mistaken, but I believe you could leave out the row in the brackets as it's the usual array decay to pointer, but you do need col.
    Thanks... but do you mean to say that we can't achieve this task (in C) using a pointer to the array?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,091
    x would still be a pointer to the first element of the variable length array
    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

  5. #5
    Registered User
    Join Date
    Jul 2020
    Posts
    7
    Quote Originally Posted by laserlight View Post
    x would still be a pointer to the first element of the variable length array
    true but... there is difference between passing a pointer to the first element & passing a pointer to the (whole) array

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,091
    Quote Originally Posted by dungeon
    there is difference between passing a pointer to the first element & passing a pointer to the (whole) array
    Yes, but unless you're dealing with an array of array of arrays, there's usually no point passing a pointer to an entire array of arrays. Hence, what you want here is a pointer to the first element of the array of arrays, i.e., an array itself.

    EDIT:
    I read that again and my apologies, it's probably more confusing.

    Firstly, remember that when you pass an array as an argument, it is converted to a pointer to its first element. If you pass a 2D array, the same thing happens, except that an element is itself an array.

    When I wrote that "x would still be a pointer to the first element of the variable length array", that's what I meant: x would be a pointer to the first element of the variable length array, and since the array is a 2D array, that first element would also be an array.
    Last edited by laserlight; 08-05-2020 at 01:49 AM.
    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
    Jul 2020
    Posts
    7
    Quote Originally Posted by laserlight View Post
    Yes, but unless you're dealing with an array of array of arrays, there's usually no point passing a pointer to an entire array of arrays. Hence, what you want here is a pointer to the first element of the array of arrays, i.e., an array itself.

    EDIT:
    I read that again and my apologies, it's probably more confusing.

    Firstly, remember that when you pass an array as an argument, it is converted to a pointer to its first element. If you pass a 2D array, the same thing happens, except that an element is itself an array.

    When I wrote that "x would still be a pointer to the first element of the variable length array", that's what I meant: x would be a pointer to the first element of the variable length array, and since the array is a 2D array, that first element would also be an array.
    Thanks... made it work using below function prototype & function call in main
    Code:
        void display(int, int, int[][col]);
        display(row, col, X);
    and below function definition
    Code:
    void display(int row, int col, int A[][col])
    {
        int i, j;
    
    
        for(i=0; i<row; i++)
        {
            for(j=0; j<col; j++)
            {
                printf("%d\n", *(*(A+i)+j));
            }
        }
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,091
    That's good to hear. For ease of reading, *(*(A+i)+j)) should be written as A[i][j]
    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

  9. #9
    Registered User
    Join Date
    Jul 2020
    Posts
    7
    Quote Originally Posted by laserlight View Post
    That's good to hear. For ease of reading, *(*(A+i)+j)) should be written as A[i][j]
    Sure will consider this henceforth... I read that accessing array elements using pointer is much faster than using subscript but as of now it doesn't matter much to me...
    PS: As array dimensions need to be explicitly passed on along with pointer to the array, I must declare the function prototype inside main function (and that too only after initializing variables 'row' & 'col'). Please do share even if at some later stage, if you happen to come across some means to figure out the dimensions using just pointer to the array...

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,091
    Quote Originally Posted by dungeon
    I read that accessing array elements using pointer is much faster than using subscript
    They are identical except for spelling.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable length 2d array
    By Sauloqf in forum C Programming
    Replies: 2
    Last Post: 07-09-2015, 12:53 AM
  2. variable length array
    By brassbin in forum C Programming
    Replies: 14
    Last Post: 11-14-2013, 12:14 AM
  3. Problem with variable length array
    By skiabox in forum C Programming
    Replies: 11
    Last Post: 11-06-2010, 04:15 PM
  4. variable length array
    By fatsrir in forum C Programming
    Replies: 18
    Last Post: 06-04-2010, 10:52 AM
  5. Variable length array
    By 671 in forum C Programming
    Replies: 2
    Last Post: 09-13-2006, 01:05 PM

Tags for this Thread