1

Well, this is my program:

#include <iostream>

using namespace std;

int main()
{
    char str[6][80] = { "Robert Redford",
        "Hopalong Cassidy",
        "Lassie",
        "Slim Pickens",
        "Boris Karloff",
        "Oliver Hardy" };

    const char* pstr[] = { "Robert Redford",
        "Hopalong Cassidy",
        "Lassie",
        "Slim Pickens",
        "Boris Karloff",
        "Oliver Hardy" };

    cout << "str:\n";
    for (int i = 0; i < 6; i++)
    {
        for (int j = 0; j < 80; j++)
        {
            cout << str[i][j];
        }
    }

    cout << endl;

    cout << "pstr:\n";
    for (int i = 0; i < strlen(*pstr) ; ++i)
    {
        cout << pstr[i] << endl;
    }

    cout << "Size of str:" << sizeof str << endl;
    cout << "Size of pstr:" << sizeof pstr << endl;

    return 0;
}

The problem is, the program stops running abruptly without showing the size of str and pstr and I get a message box saying that Pointers3.exe stopped working. What I want to know is, even though it prints out pstr, why does the program stop working?

Due to reputation limit I can't post images yet, but here's a screenshot for your consideration.

Screenshot

2
  • Use std::string and std::vector and stay away from this pointer madness. Commented Jul 11, 2014 at 15:36
  • I know about those. :D I'm doing this for the sake of college. I'm weak in pointers. Commented Jul 11, 2014 at 15:39

1 Answer 1

3

The expression strlen(*pstr) give you the length if the first string in the pstr array, not the size of the array.

For that you can use sizeof(pstr) / sizeof(*pstr). Do note that this trick only works with proper arrays, not with pointers. So if you e.g. pass the array to a function, which makes the array decay to a pointer, you can't use this trick.


What happens is that you go out of bounds of the pstr array, which leads to undefined behavior and most likely crashes the program.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks a lot mate! But I've got another question- Size of str is 480 and I get why it is so. But how is the size of pstr 24? Something tells me that using pointers is way advantageous over using normal arrays.
@ParthibBiswas If the pstr array is 24 bytes, it tells me that you're on a 32-bit system, where pointers are 32 bits (four bytes). Since there are six pointers, you have 6*4 bytes in the array.
Actually, I compiled this for 32-bit system. After compiling for 64-bit, the pointers seem to be of 8 bytes. So the pointers have the advantage of taking up less space. I mean there's a huge difference between 480 bytes and 24 bytes.
@ParthibBiswas For the pstr array, you have to remember to count the length of the strings (including their terminator) as well. It still won't be 480 bytes, but will be much more that "just" 24 bytes.
Robert Redford = 15 bytes, Hopalong Cassidy = 17 bytes, Lassie = 7 bytes, Slim Pickens = 13 bytes, Boris Karloff = 14 bytes, Oliver Hardy = 13 bytes. So, the total bytes of pstr = 79 bytes?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.