How do I get the size of an array from a pointer to the array?

This is a discussion on How do I get the size of an array from a pointer to the array? within the C++ Programming forums, part of the General Programming Boards category; I need to get the size of a char array from a pointer to that array. How do I do ...

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question How do I get the size of an array from a pointer to the array?

    I need to get the size of a char array from a pointer to that array. How do I do this?

    I tried to use the sizeof operator on the char pointer, but that only tells me the size of the pointer, and not the size of the array its pointing at.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Programmer_P View Post
    I need to get the size of a char array from a pointer to that array. How do I do this?
    You can, if the array is a C-string, by finding the null terminator. If it is not null terminated, then you are out of luck.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And strlen just says how much memory is used -- there is no way, just from a pointer, to know how much memory is available on the other side (i.e., it's your job to keep track).

  4. #4
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Yes, the array I'm using is a c-string, so its null-terminated.

    So something like this then:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() {
    
      char array[] = "hello"; //5-character char array + null character
      char* pArray = array; //get a pointer to the array;
      int sizeOfArray = 0;
      while (pArray[sizeOfArray] != '\0') { //loop until NULL character is found
        sizeOfArray++; //increment size
      }
    
      cout<< sizeOfArray;
    
    }
    Last edited by Programmer_P; 05-27-2010 at 06:44 PM.

  5. #5
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Hmm...
    Code:
    char array[10];
    int sizeOfArray = sizeof(array);
    doesn't seem to work in all cases.

    When I use a function with a type like this:
    Code:
    bool containsString(const char* sourceString, const char searchString[]);
    then inside that function, get the "sizeof(searchString)" it ends up outputting the size + 2. I don't know why that is.

    When I called the function, I did this:
    Code:
    char array[] = "Yes"; //3-character c-string
    bool stringIsContained = containsString(array, "Ye");
    with other test code. I noticed that it was returning the wrong value out of the function. It said it was "false" that "Ye" was contained in "Yes". Obviously that is a lie. So I then outputted the calculated size of both the strings from the function, to see if they were accurate or not. The size of the sourceString was correct (i.e. 3), but the size of the searchString wasn't. It was a 4.
    Last edited by Programmer_P; 05-27-2010 at 07:30 PM.

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,803
    Is there a specific reason you are not using std::string?
    Woop?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Programmer_P View Post
    Hmm...
    Code:
    char array[10];
    int sizeOfArray = sizeof(array);
    doesn't seem to work in all cases.

    When I use a function with a type like this:
    Code:
    bool containsString(const char* sourceString, const char searchString[]);
    then inside that function, get the "sizeof(searchString)" it ends up returning the size + 2. I don't know why that is.

    When I called the function, I did this:
    Code:
    char array[] = "Yes"; //3-character c-string
    bool stringIsContained = containsString(array, "Ye");
    with other test code, I noticed that it was returning the wrong value out of the function. It said it was "false" that "Ye" was contained in "Yes". Obviously that is a lie. So I then outputted the calculated size of both the strings. The size of the sourceString was correct (i.e. 3), but the size of the searchString wasn't. It was a 4.
    I don't actually believe you here, to be honest. If you declared your containsString function as above, then sizeof would give 4 for both, since both parameters would be of type const char*.

    Again: if you are dealing with pointers, it is not possible to determine how much memory is on the other side of that pointer -- and once you pass an array to a function, all of a sudden you've got a pointer. You can find the length of the string that is pointed to with strlen, but that does not actually say how much memory is/was available.

  8. #8
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by tabstop View Post
    I don't actually believe you here, to be honest. If you declared your containsString function as above, then sizeof would give 4 for both, since both parameters would be of type const char*.

    Again: if you are dealing with pointers, it is not possible to determine how much memory is on the other side of that pointer -- and once you pass an array to a function, all of a sudden you've got a pointer. You can find the length of the string that is pointed to with strlen, but that does not actually say how much memory is/was available.
    Actually, that is not true. The "searchString" paramater is a const char array. Of course, though, maybe that's the same thing, in which case that explains why its outputting 4...
    See, I thought the char searchString[] parameter would accept a char array (or literal string) passed in, and copy the contents of the passed in array to a new array which it creates on the stack with the same size as the passed-in array.
    Last edited by Programmer_P; 05-27-2010 at 07:35 PM.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Programmer_P View Post
    Actually, that is not true. The "searchString" paramater is a const char array. Of course, though, maybe that's the same thing, in which case that explains why its outputting 4...
    It is not possible to pass an array to a function in C++, since the decay rules guarantee it will decay to a pointer-to-T.

    EDIT: Illustrative code:
    Code:
    #include <typeinfo>
    #include <iostream>
    
    void tester(char array[4]) {
        char other_array[4];
        if (typeid(array) == typeid(other_array)) {
            std::cout << "Both arrays!\n";
        } else {
            std::cout << "Not both arrays.\n";
        }
    }
    
    int main() {
        char array[4];
        tester(array);
        return 0;
    }
    Last edited by tabstop; 05-27-2010 at 07:38 PM.

  10. #10
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by prog-bman View Post
    Is there a specific reason you are not using std::string?
    Yes, actually, there is. I'm reading lines from a file into a char array[] buffer. So I have to pass to the "containsString" function the array (i.e. the "sourceString" paramter) as an array. If I have a string* parameter, it wont work. I could use a string object, of course, but by the time I realized that, I had already switched everything in the function to be geared to a char array, instead of a string*, and I'd rather edit a few lines rather than 50 lines.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's not really much of a reason, to be blunt. And even if you stay with C-strings, please tell us that you didn't just rewrite strstr() from the standard library.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    std::string includes .c_str(), which returns a C string. So if you need to use a C++ string as a C string, just use:
    Code:
    string mystring;
    some_function_or_whatever(mystring.c_str());
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by tabstop View Post
    I don't actually believe you here, to be honest. If you declared your containsString function as above, then sizeof would give 4 for both, since both parameters would be of type const char*.
    Just to clarify that point, in the function, I didn't actually use sizeof on both parameters (at least not after discovering it wouldn't work on a pointer to an array). I used a while loop to increment an int variable initialized to 0 as long as the current character of the array being pointed at wasn't a null character. That's why I got size 3 for the first parameter when I passed a c-string to it containing "Yes", and size 4 for the string passed to the second parameter which contained "Ye". But now I that I know both paramaters are basically the same thing, I'm no longer using sizeof to calculate the size of either string passed in to the function. I'm using the same method for both.

  14. #14
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by MK27 View Post
    std::string includes .c_str(), which returns a C string. So if you need to use a C++ string as a C string, just use:
    Code:
    string mystring;
    some_function_or_whatever(mystring.c_str());
    Thank you.
    I'll keep that in mind if I ever need to pass a C++ string to a function expecting a C-string.
    Of course the issue in my case was the variable that needs to be passed to the function starts off as a c-string, not a C++ string, and so rather than converting c-string to C++ string, then passing that, I just pass as is.

  15. #15
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Talking

    Quote Originally Posted by tabstop View Post
    That's not really much of a reason, to be blunt. And even if you stay with C-strings, please tell us that you didn't just rewrite strstr() from the standard library.
    I just rewrote strstr().

    C++ code - 265 lines - codepad

    And it is called containsString().

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By bennywhere in forum C Programming
    Replies: 16
    Last Post: 10-20-2009, 09:00 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Dynamic pointer array in C
    By MacFromOK in forum Windows Programming
    Replies: 14
    Last Post: 04-09-2005, 06:14 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21