Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the length of an array from a pointer? [duplicate]

Tags:

I'm having trouble finding the length of an pointer array. Let's say I have:

char array[40] = "hello"; // size 40
int length =  sizeof array / sizeof array[0]; // no problem returns 40

//How do I get the length of the array with only a pointer to the first element in that array?

 char* pchar = array;

//if

std::strlen(pchar); // this returns the length of 5... i want length 40

//if

int count = 0;
while(true)
{
  if(*(pchar + count) == '\0') // returns 5...
    break;
   count++;
}

How do I get it to return length 40 just from a pointer to the first element in the array?
I found that I can do this.

int count = 0;
    while(true)
    {
      if(*(pchar + count) == '\0' && *(pchar + count + 1) != '\0') 
             break;

       count++;
    }

This returns 39, this is good but I feel like this can be buggy in some situations.

like image 279
Anthony Raimondo Avatar asked Apr 02 '13 21:04

Anthony Raimondo


People also ask

How do you get the length of an array from a pointer?

Using Pointers to Find Array Length in C++The expression *(arr+1) gives us the address of the memory space just after the array's last element. Hence, the difference between it and the arrays starting location or the base address(arr) gives us the total number of elements present in the given array.

How do I get the size of a pointer in C++?

The size of a pointer in C/C++ is not fixed. It depends upon different issues like Operating system, CPU architecture etc. Usually it depends upon the word size of underlying processor for example for a 32 bit computer the pointer size can be 4 bytes for a 64 bit computer the pointer size can be 8 bytes.

Can you use sizeof on a pointer?

The code calls sizeof() on a malloced pointer type, which always returns the wordsize/8. This can produce an unexpected result if the programmer intended to determine how much memory has been allocated. The use of sizeof() on a pointer can sometimes generate useful information.


2 Answers

You can't, I'm afraid. You need to pass the length of the array to anyone who needs it. Or you can use a std::array or std::vector or similar, which keep track of the length themselves.

like image 52
RichieHindle Avatar answered Sep 29 '22 11:09

RichieHindle


C++ has proper string type:

std::string

which you may find helpful here. Even if you're passing it to function that accepts const char*, it has .c_str() method that allows you to pass it to function that accept a pointer. If the other function needs to modify the string, you can use &str[0] which is valid for many implementations of C++, and is required to work for C++11. Just make sure you resize() them to the correct size.

Some of the other containers in C++ are:

std::array (C++11) Array of constant size. Better than plain old C array, as it has size() method. std::vector Dynamic array (Java ArrayList equivalent)

As for your question - there is no way to find size of a pointed array. How could you even do that? It's just a stupid pointer.

like image 28
milleniumbug Avatar answered Sep 29 '22 12:09

milleniumbug