2

My code:

#include "pch.h"
#include <iostream>

using namespace std;

   int main()
   {
      char** pptr = new char*[5];

      for (int i = 0; i < 5; i++)
      pptr[i] = new char[5];
   }

What I want to happen is that pptr now points to the beginning of an array of 5 pointers that each point to the beginning of an array of 5 characters.

I put a breakpoint at the end of the main function and added pptr to watch, and it only stores one pointer. Why does this happen and how do I do it correctly?

enter image description here

2
  • 1
    You said you wanted pptr to point to the beginning of an array of pointers... the beginning of an array is indeed one pointer - the first one in the array. The other pointers come later in the same array. Commented Jan 4, 2019 at 14:39
  • 1
    Please next time reduce the picture to its interesting part to not have that large white margin ;-) Commented Jan 4, 2019 at 14:44

3 Answers 3

6

This is the default knowledge of your pointer type in Visual Studio. You indicate in the code that char** pptr is a pointer, but it cannot know how big.

To fix this, you can add a watch on pptr[0], and then you can specify that it has a "size" of 5 by changing it to pptr[0],5. Also, if the size is variable you can do "ptr[0],[size]" where size is an expression that evaluates to the number of elements to show.

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

6 Comments

That's an interesting solution to an annoying problem, I'll try it out in the future. Though I'm surprised, since pptr[0],5 has a distinct meaning in c++. It seems inconsistent for the debugger to arbitrarily interpret certain operators differently when it treats most operators the same way the language does.
Yes, the , is specific to the debugger watch variable. I'm not sure it works with a comma operator as the expression. Probably triggeres a bunch of errors somewhere!
yes is strange, is it not possible to change the type of the element to display to be char*[5] being c++ as I proposed ? (I do not use Visual Studio)
No, it only displays the type as VS understands it. But then, you can always have a reinterprect_cast in the expression as well (I think).
Yes, you should be able to dereference the pointer and cast it to a reference to an array of 5 char* which I think is written char*(&)[5]. Edit : The watch expression (char*(&)[5])(*pptr) will give you pptr as an array of 5 char*. Edit 2 : Though pptr,5 is still a way nicer way of doing it.
|
2

Your program does what you want, but the debugger cannot know the number of elements, it just know it is a pointer, so it write the contains of that pointer.

I don't know what debugger you use, but probably when you display the values you can modify char** by char*[5]` to see all

Comments

-2
// #include "pch.h"
#include <iostream>

using namespace std;

int main(){
    char** pptr = new char*[5];

    for (int i = 0; i < 5; i++)
        pptr[i] = new char[5];

    for(int i=0;i<5;i++){
        char ch='A';
        pptr[0][i]=ch;
    }

    for(int i=0;i<5;i++){
        cout<<pptr[0][i]<<" ";
    }
}

Now the pptr[0] that is a pointer is pointing to an array of characters. Hope that it helps.

1 Comment

the problem doesn't concern the code but what the debugger shows by default

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.