I want find size of array by pointer in a function.example :

//char array[] is a random array,i can alloc dynamic,so i can know size,i want access a random possition in array.

char* pointer;
void myfunc (char * array)
{
pointer = array;

//how many elements in the array???
}

i use C programming.

Dani AI

Generated

Short answer: you cannot reliably discover the allocated length of an array from a bare pointer in portable C. The language gives you an address, not metadata; once an array decays to char * the compiler no longer has the original size information. Several posters have already pointed this out (see and ), so the practical work is choosing a safe pattern that preserves the length where it’s needed.

Common, reliable patterns (pick one that fits your MCU constraints):

  • pass length alongside the pointer (the simplest and most portable API);
  • encapsulate pointer+length in a small struct and pass that; or
  • allocate a single block that stores the length in a header and returns a pointer to the payload (stores the length where you control it).

Example of a safe allocation wrapper using a header (flexible-array member):

typedef struct {
    size_t len;
    char data[];
} Blob;

Blob *blob_alloc(size_t len) {
    Blob *b = malloc(sizeof *b + len);
    if (b) b->len = len;
    return b;
}

/* use b->data and b->len; pass Blob* to functions */

Notes for MCU / embedded work (relevant to ): avoid heap bookkeeping tricks—they are nonportable and often unavailable on small toolchains. If dynamic allocation is undesirable, use statically-sized buffers plus an explicit “used length” variable or a ring buffer with head/tail indices. The sentinel approach mentioned by is only acceptable when the sentinel value can never appear in real data (e.g., C strings); it’s fragile for binary buffers.

Practical rule: design functions to accept either (pointer, length) or a small typed descriptor. That makes ownership, lifetime, and bounds-checking explicit, and prevents the undefined behavior that can follow blind pointer arithmetic.

Recommended Answers

All 11 Replies

This is impossible. There is no way to know when you find the end of the array. It is up to you as the programmer to keep track of the size of the array when you make it.

My purpose is get size of array that pointer b point to.

void func ( char *ptr ) {
    char* px;
    char size_of_ptr m,k;
//  m= size of array that ptr point to.I can't get size of array.
    px=ptr;
    char *b =(char*) malloc(m);
    memcpy(b,px,m);
//  k= size of array that b point to.I can't get size of array. 
    printf("sizeof len =  %d ",k);
}
int main ( ) {
   // char a[100] = "hello";
    char a[6] ={ 1,2,3,0,4,5   };
    char n[8] ={ 1,2,0,5,4,5,8   };  
    func(a);
    func(n);
}

For lines 15 and 16 it is not possible to get the length of the string because neither string is NULL-terminated. If you ended each of those strings with '\0' then func() could call strlen() to get the length of the strings. In any event, its impossible for func() to know the actual size of the array, which may or may not be the same as the length of the string contained in the array.

have any ways to solution this problem? please help me get some idea.Now i dont have any good ideal for this problem.

have any ways to solution this problem? please help me get some idea.Now i dont have any good ideal for this problem.

How about you read the replies? There is no solution to your problem because it's not possible to do what you want. Add another argument to "func" that contains the size of the buffer pointer to by "ptr".

To make it even clearer, in the C programming language, nothing in the language keeps track of the size of an array. That information does not exist once the array has been created. You cannot discover information that does not exist. The only thing you can do is create another variable to store the size of the array, and keep track of it yourself.

To make it even clearer, in the C programming language, nothing in the language keeps track of the size of an array. That information does not exist once the array has been created.

You can extract that information, iff you have access to the array object itself (a pointer won't work):

size_t size = sizeof(array) / sizeof(*array);

But that's unlikely to be a suitable answer to the original problem, given that the "array" is actually a pointer (since it's a function parameter). However, I did want to make it clear that the size of an array isn't lost, per se, but you absolutely must be working with the array object itself and not a pointer for sizeof to work properly.

I programming for MCU,i want track any position of array,so i want to get length of array through a pointer point to it,array is changeable any position and it is a dynamic array.I thinks func() need a varian to store length of array was allocated dynamic.

I think everyone understands what you want, but what you want is impossible with the given information. You must store the size somewhere in a separate variable where the size is known if you want access to that size from func().

Somehow I doubt that your embedded compiler supports something like _msize(), but that's an alternative that you need to RTFM to see if something is available.

you can work around it if you know the the array values range.

let say you never expect the array to have value of -1 or a value or '$' you can do the following.

#define END_OF_STR '$'

size_t getArrSize(const char * ptr)
{
    char *ptrIn = ptr;
    size_t size = 0;
    while (*ptrIn != END_OF_STR) {
        size++;
        ptrIn++;
    }
    return size;
}

void func ( char *ptr ) {
    char* px;
    char size_of_ptr, m,k;
        //  m= size of array that ptr point to.I can't get size of array.
    m = getArrSize(ptr);
    //px=ptr;
//    char *b =(char*) malloc(m);
//    memcpy(b,px,m);
        //  k= size of array that b point to.I can't get size of array. 
    printf("sizeof len =  %d ", m );
}
int main ( ) {
        // char a[100] = "hello";
    char a[] ={ 1,2,3,0,4,5, END_OF_STR };
    char n[] ={ 1,2,0,5,4,5,8, END_OF_STR };  
    func(a);
    func(n);

    getchar();
    return 0;
}

End of array have to be a 1 any char but not NULL.
Thanks you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.