sizeof char* array in C/C++

后端 未结 6 699
旧巷少年郎
旧巷少年郎 2021-02-14 07:57

There are plenty of similar inquires, but in my case I don\'t understand what isn\'t working:

int mysize = 0;
mysize = sizeof(samplestring) / sizeof(*samplestrin         


        
相关标签:
6条回答
  • 2021-02-14 08:19

    4 isn't the size of the string, because samplestring isn't a string. It's a char*, whose size is (on your platform) 4, divided by 1 (size of char) is, correctly, 4.

    In C++, you'd use std::string and the length() method.

    In C, you'd use strlen which takes as parameter a NULL-terminated char pointer.

    0 讨论(0)
  • 2021-02-14 08:22

    you are asking the size of a pointer on a char. So I guess you're using a 32bit system.

    If you're using C++, use std::string :

    std::string samplestring("Hello world");
    std::cout << samplestring.size() << std::endl;
    
    0 讨论(0)
  • 2021-02-14 08:25

    First of all, sizeof(samplestring[0]) is the same as sizeof(*samplestring), they're both returning the size of the first element of the samplestring array. And, assuming samplestring is an array of chars, sizeof(char) is defined to be 1.

    You haven't shown how samplestring is declared. It could be one of the following:

    char const *samplestring = "Hello, World!";
    

    or

    char *samplestring = malloc( ... );
    

    or

    char samplestring[10];
    

    In the first 2 cases the type of samplestring is char *, so sizeof(samplestring) returns sizeof(char *), which, on your platform is 4.

    In the third case, the type of samplestring is char[10] (array of 10 chars), but if you call a function that takes a char * as its parameter, the char array will decay to a pointer pointing to the first element of the array. In this case, trying to print sizeof within the function will still result in the size of a pointer being printed.

    If you want the size of the original array to be printed from within the function, then the function parameter needs to be a pointer to the original array type (and the type includes size of the original array).

    #include <stdio.h>
    
    void foo( char (*arr)[42] )
    {
      printf( "%u", (unsigned)sizeof(*arr) );
    }
    
    int main()
    {
      char arr[42];
      foo( &arr );
    
      return 0;
    }  
    

    Output:

    42
    

    This fixes the size of the array that can be passed to the function and is not desirable in a lot of cases. The only other solution is to keep track of the array yourself (or use strlen if you have a NULL terminated string).

    0 讨论(0)
  • 2021-02-14 08:26

    The sizeof of an element returns the size of the memory allocated for that object. In your example the string is probably declared somewhere as

    samplestring[4]
    

    In which case the size of the memory is 4. The method you probably want in your application is

    strlen(samplestring);
    

    Which returns the size of the null terminated string (without the termination)

    0 讨论(0)
  • 2021-02-14 08:36

    It looks as though you have a pointer, not an array. Arrays are converted to pointers when the program requires it, so you'd get:

    size_t size(char * p) { // p is a pointer
        return sizeof(p) / sizeof(*p); // size of pointer
    }
    
    size_t size(char p[]) { // p is also a pointer        
        return sizeof(p) / sizeof(*p); // size of pointer
    }
    

    although, since sizeof (char) == 1, the division is redundant here; if the pointer were to a larger type, then you'd get a differently unexpected result.

    In C++ (but obviously not C), you can deduce the size of an array as a template parameter:

    template <typename T, size_t N>
    size_t size(T (&)[N]) {
        return N;  // size of array
    }
    

    or you can use classes such as std::vector and std::string to keep track of the size.

    In C, you can use strlen to find the length of a zero-terminated string, but in most cases you'll need to keep track of array sizes yourself.

    0 讨论(0)
  • 2021-02-14 08:39

    There are two ways of making a string constant, and your technique only works on the first one. The first one makes an array of characters which you can get the size of at compile time, and the other creates a pointer to an array of characters.

    char samplestring[] = "hello";
    char * samplestring = "hello";
    

    Trying to take the size of the second case the way you're doing it just gives you the size of a pointer. On a 32-bit build the size of a pointer is 4 characters, i.e. a pointer takes the same amount of memory as 4 characters.

    The following will always give the correct length for a properly null-terminated string, but it's slower.

    mysize = strlen(samplestring);
    
    0 讨论(0)
提交回复
热议问题