2

i have pointer of array(array which is in memory). Can i calculate the size of array from its pointer ? i dont know actually where is the array in memory. i only getting pointer adress(suppose 9001) using that adress i have to calculate array size.

Thanks.

harsh
  • 109
  • 1
  • 6
  • 22
  • 2
    Is this meant to be tagged [C#]? It sounds like you're actually having a problem with [C]. – porges Feb 22 '12 at 07:20
  • This would be easier with C++ templates ... –  Feb 22 '12 at 07:28
  • Please tag this question as either `[C]` or `[C++]` but not both. For this question, some answers applicable to one language will not apply to the other. – Dietrich Epp Feb 22 '12 at 08:48
  • sizeof(array)/sizeof(array[0]) will give you the number of elements in the array. Multiply this by sizeof(array_element_type) and you get the total size occupied by the array. – webgenius Feb 22 '12 at 09:08
  • possible duplicate of [Sizeof an array in the C programming language?](http://stackoverflow.com/questions/1975128/sizeof-an-array-in-the-c-programming-language) – Bo Persson Feb 22 '12 at 12:16
  • You want to find maximum size or the no. of elements in that array? – Sachin Mhetre Feb 22 '12 at 11:24

3 Answers3

11

No, you cannot calculate the size of the array. Objects in C do not carry type information, so you must arrange to know the size of the array ahead of time. Something like this:

void my_function(int array[], int size);
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • Personally I'd advise against ever using `int array[]` as a function parameter. In principle it might help readers, by telling them that `array` is a pointer to the first of one or more `int`s. In practice: (1) `size` already tells them that; (2) people are used to using `char*` when passing a pointer to the first character of a string, not `char[]`, so they can cope with `int*` too; (3) the different syntax frequently misleads newbies into thinking it's somehow different from `int *array`. – Steve Jessop Feb 22 '12 at 08:42
  • The simpler the question, the greater the bike shedding. – Dietrich Epp Feb 22 '12 at 08:45
  • 1
    Yeah, you're probably right. Because in a question where someone is confused/ignorant about how arrays (and pointers to their first elements) work, stuff that confuses people about how arrays (and pointers to their first elements) work is trivial compared to the sheer glory of your correct answer. – Steve Jessop Feb 22 '12 at 08:47
5

You cannot do this in C. The size of a pointer is the size of a pointer, not the size of any array it may be pointing at.

If you end up with a pointer pointing to an array (either explicitly with something like char *pch = "hello"; or implicitly with array decay such as passing an array to a function), you'll need to hold the size information separately, with something like:

int twisty[] = [3,1,3,1,5,9];
doSomethingWith (twisty, sizeof(twisty)/sizeof(*twisty));
:
void doSomethingWith (int *passages, size_t sz) { ... }

The following code illustrates this:

#include <stdio.h>

static void fn (char plugh[], size_t sz) {
    printf ("sizeof(plugh) = %d, sz = %d\n", sizeof(plugh), sz);
}

int main (void) {
    char xyzzy[] = "Pax is a serious bloke!";
    printf ("sizeof(xyzzy) = %d\n", sizeof(xyzzy));
    fn (xyzzy, sizeof(xyzzy)/sizeof(*xyzzy));
    return 0;
}

The output on my system is:

sizeof(xyzzy) = 24
sizeof(plugh) = 4, sz = 24

because the 24-byte array is decayed to a 4-byte pointer in the function call.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

No. arrays in C doesn't have this info. you should hold a variable "next" to the array with this data.

if you have the array it self you can use the sizeof to get his size in compilation stage.

char array1[XXX];
char* pointer1 = array1;
sizeof(array1); // this is XXX
sizeof(pointer1); // this is the size of a pointer in your system

you should do the following and use the variable in you program (or pass it to function when you passing the array to a function)

int array1size = sizeof(array1);
Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94