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.

6
  • 2
    Is this meant to be tagged [C#]? It sounds like you're actually having a problem with [C]. Commented Feb 22, 2012 at 7:20
  • This would be easier with C++ templates ... Commented Feb 22, 2012 at 7: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. Commented Feb 22, 2012 at 8: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. Commented Feb 22, 2012 at 9:08
  • You want to find maximum size or the no. of elements in that array? Commented Feb 22, 2012 at 11:24

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

3 Comments

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 ints. 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.
The simpler the question, the greater the bike shedding.
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.
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.

4 Comments

Shouldn't sz in fn() be 6?
No, there's 24 bytes in the array "Pax is a serious bloke!". I suspect you're confused with the integer array above that.
Whoops, sorry, I was thinking xyzzy = [3,1,3,1,5,9] from your previous example. Maybe use the same xyzzy value in both examples?
Good suggestion, @liwp, I've changed the names to hopefully remove any confusion.
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.