Size of int pointer

Hello!
My name is Sikander!

My question is simple:

is there a single way to get the size/length of an int pointer?
1
2
3
4
5
6
7
 // like char point
//char *charptr="Hello";
// its length is 5
//but how to get the length of following int pointer
int *intptr={1,2,5};
//its supposed to be 3
//my question is how to get 3 using a library or own function  

Need help...
I have tried sizeof();
Last edited on
There is some apparent confusion that needs to be cleared first.

// like char point
//char *charptr="Hello";
// its length is 5


"Hello" is an array of six const char (it holds the characters 'H', 'e', 'l', 'l', 'o', and '\0';). To see that, print its sizeof:
std::cout << sizeof "Hello" << '\n';

charptr is a pointer. Like any pointer, its size is the same regardless of what it points to, it is the size necessary to store an address on your platform, typically 4 or 8 bytes, You can see that if you print its sizeof:

std::cout << sizeof charptr << '\n';

The line char *charptr="Hello"; is an error in C++ (it used to be supported for C compatibility), it's properly written as const char *charptr="Hello";. This line puts into charptr the address of the character 'H' from your string.

The only way to get the number 5 that you saw is to examine the memory locations after 'H' until you find '\0' and count the number of characters seen. The C library function strlen() does that:

std::cout << strlen(charptr) << '\n';

Finally, the line int *intptr={1,2,5}; is an error. You can do int *intptr= new int[3]{1,2,5}; in modern C++, but there is no way to get that 3 from the resulting pointer. Use vector<int> if you're looking for dynamically allocated arrays in C++, then you can obtain the number of elements by calling the member function .size()
Last edited on
Ohhh I see!
the charptr is just an example to get to intchar...
I thought there is a way to get length/size of intptr...
One more thing...
Is there a way to copy intptr from intptr
Example:
strcpy();
is there any function for int..??
closed account (zb0S216C)
When "sizeof( )" is applied to a pointer, the size [in bytes] of the pointer is the resulting value of "sizeof( )". When "sizeof( )" is applied to an array, it returns the number of elements the array contains. For instance:

1
2
sizeof( "ABC" ); // ...will be 4 (that's including the null-terminator).
sizeof( int * ); // ...can be anything. 

In the comments of the code you posted, you're basically asking how one can count the number of elements within an array through a pointer. The short answer is: it depends. With strings, they're terminated with the null-character. Functions such as "strcpy( )" and "strlen( )" rely on the null-character to calculate the size of a string. However, with other types, there's no such value to denote the end of an array. One solution would be to define some value for a given type which would be equivalent to the null-character. For example:

1
2
3
4
5
6
7
8
9
10
11
int const IntNull_( -1 );
int Array_[] = { 1, 2, 3, 4, IntNull_ };

unsigned int SizeOfIntArray( int *Array_ )
{
  unsigned int Count_( 0u );
  while( *Array_ != IntNull_ )
    ++Count_, ++Array_;

  return( Count_ );
}

Wazzak
Last edited on
Your logic is magnificent!
Thankyou for such wonderful advice... :D
> strcpy();
> is there any function for int..??
`memcpy()' or `std::copy'
Topic archived. No new replies allowed.