How to find out the length of the array pointed by a pointer?

This is a discussion on How to find out the length of the array pointed by a pointer? within the C Programming forums, part of the General Programming Boards category; Hi all, i passed an array of integer Code: int myarr[10]; into a function like this: Code: int *function(int *array, ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Location
    Southampton, United Kingdom.
    Posts
    18

    How to find out the length of the array pointed by a pointer?

    Hi all,

    i passed an array of integer
    Code:
    int myarr[10];
    into a function like this:
    Code:
    int *function(int *array, size){
    ...
    }
    however when i tried to find out the length of myarr in the function using:
    Code:
    int arrSize = sizeof(array)/sizeof(int);
    it returns only (i think) the size of the memory location *array pointed to, which is the start of myarr?

    How should i find out the length of myarr in the function without passing in one more argument?

    Thanks in advance!

  2. #2

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    23
    however when i tried to find out the length of myarr in the function using:
    Code:
    int arrSize = sizeof(array)/sizeof(int);
    it returns only (i think) the size of the memory location *array pointed to, which is the start of myarr?
    yes, an array isn't passe by value, there's an implicit conversion array-pointer and in the function you have a pointer to the first element, in fact in the function write array[3] or *(array+3) is the same thing.
    That code works only in the function where the array is declared
    How should i find out the length of myarr in the function without passing in one more argument?
    you can't, you can use many solution if you strongly need a function with 1 parameter:
    1)you can declare your array of the size needed +1 and assign to the last element in position [n-1] a special value (es -9999) you're sure that the element from 0 to n-2 can't assume
    2) a struct, for example:
    Code:
    typedef struct{
    int dim;
    int *a;
    }myarray;


    etc..

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by ymc1g11 View Post
    Hi all,

    i passed an array of integer
    Code:
    int myarr[10];
    into a function like this:
    Code:
    int *function(int *array, size){
    ...
    }
    however when i tried to find out the length of myarr in the function using:
    Code:
    int arrSize = sizeof(array)/sizeof(int);
    it returns only (i think) the size of the memory location *array pointed to, which is the start of myarr?

    How should i find out the length of myarr in the function without passing in one more argument?

    Thanks in advance!
    This will give you the sizeof the pointer,which can be 2,4 or 8 depending on the system.Value 2 is very rare.

    What you should do, is what you already did.Pass the size of the array as second parameter.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,062
    Quote Originally Posted by ymc1g11 View Post
    Code:
    int *function(int *array, size){
    ...
    }
    How should i find out the length of myarr in the function without passing in one more argument?
    Simple answer: you don't. You need to somehow make the size known to the function in order for it to work. However, if you're using C99 mode then you can declare the function as follows:

    Code:
    #define NELEMS(x) (sizeof((x)) / sizeof((x)[0]))
    void function(int size, int inArray[size], int outArray[size])
    {
    ...
    }
    The parameters inArray and outArray are still passed implicitly as pointers, but C treats them as pre-initialized arrays for the purpose of the function. That means that NELEMS(inArray) will correctly give you the number of elements whether you use it inside the function or outside.

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by c99tutorial View Post
    The parameters inArray and outArray are still passed implicitly as pointers, but C treats them as pre-initialized arrays for the purpose of the function. That means that NELEMS(inArray) will correctly give you the number of elements whether you use it inside the function or outside.
    Nope. Try it out for yourself.

    The arguments are still treated as pointers, and the "array sizes" are ignored by the compiler. They're not anything like a variable-length array. The sizeof operator gives you the size of a pointer, because it is only a pointer.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    1,062
    Quote Originally Posted by christop View Post
    Nope. Try it out for yourself.

    The arguments are still treated as pointers
    I stand corrected on this point. sizeof(array) within the function is equal to sizeof(void*). The NELEMS macro cannot be used.

    Quote Originally Posted by christop View Post
    and the "array sizes" are ignored by the compiler.
    The sizes are not necessarily ignored. They can affect the way the array may be indexed within the function. Consider this function declaration:

    Code:
    void matprint(int y, int x, int a[y][x]) {
        for (int i=0; i < y; i++) {
            for (int j=0; j < x; j++)
                printf("%3d ", a[i][j]);
            printf("\n");
        }
    }
    Within the function, the array may be indexed as if it were a VLA of size [y][x]. If the sizes were reversed, for instance, to `int a[x][y]' then the printed result will be different if x != y.
    Last edited by c99tutorial; 11-10-2012 at 07:11 PM.

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by c99tutorial View Post
    The sizes are not necessarily ignored. They can affect the way the array may be indexed within the function. Consider this function declaration:

    Code:
    void matprint(int y, int x, int a[y][x]) {
        for (int i=0; i < y; i++) {
            for (int j=0; j < x; j++)
                printf("%3d ", a[i][j]);
            printf("\n");
        }
    }
    Within the function, the array may be indexed as if it were a VLA of size [y][x]. If the sizes were reversed, for instance, to `int a[x][y]' then the printed result will be different if x != y.

    The first dimension is essentially always ignored*. With a two-dimensional array parameter, as in your example, only the second dimension must be specified, so sizeof then knows how big the array (in the second dimension) is. C99 just allows the size to be variable, similar to a VLA.

    *The compiler might use the dimensions for optimizations, but the first dimension doesn't otherwise affect what the code does. I actually haven't had much need for passing multidimensional arrays to functions lately, and I haven't explored the various syntaxes that were introduced in C99.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to determine length of pointer array
    By Dr.Zoidburg in forum C Programming
    Replies: 12
    Last Post: 02-16-2009, 05:52 PM
  2. resizing array pointed to by pointer
    By sys_x in forum C++ Programming
    Replies: 3
    Last Post: 12-27-2008, 11:06 AM
  3. how do I find the length of and array using sizeof()
    By raymond1234 in forum C Programming
    Replies: 9
    Last Post: 04-27-2007, 02:38 AM
  4. changing the value pointed to by a pointer
    By peterchen in forum C Programming
    Replies: 7
    Last Post: 06-16-2006, 07:49 AM
  5. Replies: 5
    Last Post: 10-29-2001, 09:16 AM

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