size of array of char*'s value

i wanna ask

if, for example, i made a variable like this:

 
const char * p_char [] = {"asd", "cvbz", "fghjbnm"};


why the sizeof () can't show each of its string's size?

1
2
3
std::cout << sizeof (p_char[0]) << std::endl; //4
std::cout << sizeof *(pchar[1]) << std::endl; //1
std::cout << sizeof *(pchar + 2) << std::endl; //4 


so, how to show each of its string's size..?? (include null-terminated char)
Last edited on
chipp wrote:
how to show each of its string's size..?? (include null-terminated char)


1
2
3
4
5
6
7
#include <iostream>
#include <cstring>
int main()
{
   const char * p_char [] = {"asd", "cvbz", "fghjbnm"};
   for ( auto p : p_char ) std::cout << strlen( p ) + 1 << '\n';
}

Last edited on
Note that 'p_char' is an array of pointers. An element in the array is a pointer.

p_char[0] is an element of the array. A pointer. Size of pointer is not size of string.

p_char[0] has alternate syntax:
p_char[0] == *p_char == *(p_char + 0)

The *(pchar + 2) is thus same as pchar[2] p_char[2] is another element in the array, and so is p_char[1]

The *(p_char[1]) could be written pchar[1][0]
Since p_char[1] is char*, the pchar[1][0] is char

There is no way for a pointer to know whether it points to one object or element in an array. Naturally, it can't have the info about lenght of array.


C-strings are arrays of char that have terminating null. The strlen() iterates from given address until it encounters that null.
Additional note: the string literal itself is an array and has not yet degraded into a pointer. sizeof "hello" is 6.
But you can't put such elements in an array without them degrading (at least, not that I know of).

You can also do this:
1
2
    const char (&str)[6] = "Hello";
    std::cout << sizeof str << '\n';

but again, that isn't an array of strings, just an individual one that you already the size of, so not very helpful.
Last edited on
@keskiverto:
The *(pchar + 2) is thus same as pchar[2]

my mistake, it should be p_char... it was typo...

C-strings are arrays of char that have terminating null.

i'm actually wondering, what is C-string means..? is there other type of string than C-string? are std::string are not C-string..?

@lastchance: i mean, a function to displayed the complete length, including null-terminated character, without any additional code (i.e. without +1 anymore)
are std::string are not C-string..?
Correct, std::string is a class that exists within the C++ standard library, accessible from the <string> header. "C-strings" are just the built-in null-terminated character arrays in the C and C++ languages. You can make your own functions to operate on them, or use the existing functionality (like strlen, strcpy) found within <cstring> (<string.h> in C).

std::strings take care of dynamically allocating/deallocating memory on their own, and can be resized dynamically; it takes a lot more work to dynamically resize c-strings.
Last edited on
The C language does not have "fancy objects". However, the C Standard Library has set of functions that operate on null-terminated array of char. The strlen() is one of those.

Those functions are declared in string.h; the cstring is a C++ Standard Library header -- a C++ wrapper for C's string.h.
It is clearly easier to say "string" than "null-terminated array of char", and "C-string" rather than "what is called string in C".

The C++ Standard Library std::string is a class. Whether it internally uses null-terminated array of char or not is not our concern.


If you want a function that does +1, you can write a helper:
1
2
3
4
size_t mystrlen( const char * str )
{
  return strlen( str ) + 1;
}

If you want a function that does +1, you can write a helper:
1
2
3
4
size_t mystrlen( const char * str )
{
  return strlen( str ) + 1;
}


ok, so i guess, there's no functions that included the null-terminated character


There is no way for a pointer to know whether it points to one object or element in an array. Naturally, it can't have the info about lenght of array.

and we can't display the size of the strings at the pointers' array as well...

@ganado: thx for the explntions
Topic archived. No new replies allowed.