pointer arithmetics debugging

I have a code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main(){
	
	char * * stringg;
	
	cin >> * stringg;
	
	cin >> *(*(stringg) + 1);
	
	cout << * stringg << endl;
	
	cout << *(*(stringg) + 1);
	
}//---------main() 


and I want it to produce output something like:

if my input is as following:

this is a stream output


output should be something like:
this
is


but the output I'm receiving is:

tiis
i
char * * stringg; is an uninitialized pointer, i.e. an invalid pointer. You cannot deference an invalid pointer.
cin >> *(*(stringg) + 1); haven't I initialized it ???
cin >> *(*(stringg) + 1); haven't I initialized it ???
No. That's where you dereference it.

Something like this is one way to initialise it. A pointer needs something to which it points.
1
2
3
4
    char  text[100];
    char * buf = text;
	
    char * * stringg = &buf;
Initialize means put some value in it.

wouldn't this be valid
*(* (stringg) + 1) = "some string";


because this is saying store "some string" in whatever 2nd pointer(in stringg)
(* (stringg) + 1)
is pointing to
wouldn't this be valid
*(* (stringg) + 1) = "some string";

no that's a complete mismatch of types.

After dereferencing the pointer stringg twice, the left hand side *(* (stringg) + 1) has a type of char. But of course unless the pointer was initialised properly, it isn't an accessible char.

The right hand side, "some string" has a type of const char *, that is a pointer to a constant array of characters.

Clearly it doesn't make any sense to assign to an individual character, a pointer. Even if you used some sort of cast to force it, the sizes don't match, a pointer requires more than one byte of storage.

I'm getting much of what you said but you know I'm starter so a few confusions yet.

Tell me is it possible to use double pointer as dynamic array of string?

like in first pointer I store size 5 string in next I store size 70 string in next I store 35 and I keep going on as long as I wish?

wish there was option to put images then I might have said clearly what I want to say here.
Point to array with a pointer? Yes, possible:
1
2
Foo * p;
p = new Foo [7];

But, you say, and then I reveal earlier code:
typedef Bar* Foo;

Expanding the typedef, we really had:
1
2
Bar * * p;
p = new Bar * [7];

We have now, not one but 8 pointers; the 'p' and 7 unnamed in an array. However, the latter 7 are uninitialized.

For convenience, we remember that Bar means char. Lets initialize some:
1
2
3
4
5
6
7
8
9
10
11
p[0] = "Hello";  // type mismatch; "Hello" is a const string literal.
  // You can have it there, but you cannot modify the memory that the characters occupy.

p[1] = new char [42] {"world"}; // Fine and dynamic
  // The "world" is a const string literal, but those characters are copied to the 42-char array -- initialization

char arr[] = "Dolly";
p[2] = arr; // Fine, although the pointed array is a local variable with scoped lifetime

char X = '@';
p[3] = &X; // fine, although not an array and thus cannot have a C-string 


A pointer is a variable. Its value is a memory address. That memory address should have an another variable, i.e. that memory location should be allocated for a variable (of appropriate type). Creating a pointer does not create that other memory location.
Last edited on
For convenience, we remember that Bar means char. Lets uninitialize some:
uninitialize or initialize

p[0] = "Hello"; // type mismatch; "Hello" is a const string literal.
// You can have it there, but you cannot modify the memory that the characters occupy.


which character can I not modify???

p[1] = new char [42] {"world"}; // Fine and dynamic
// The "world" is a const string literal, but those characters are copied to the 42-char array -- initialization


p[2] holds the address of a character pointer which further points to array of 42pointers---right can I latter change it from 42 to 70 or something

char arr[] {"Dolly"};
p[2] = arr; // Fine, although the pointed array is a local variable with scoped lifetime


wouldn't it be [qoute]char arr[] = {"Dolly"};[/qoute] OR [qoute]char arr[] = "Dolly";[/qoute] both are correct right

char X = '@';
p[3] = &X; // fine, although not an array and thus cannot have a C-string


what do you mean by cannot have C-string
Initialize. Typo.

The "Hello" is Const Data. See http://www.gotw.ca/gotw/009.htm

p[1] = new char [42] {"world"};
p[2] holds the address of a character pointer which further points to array of 42pointers---right can I latter change it from 42 to 70 or something

No. p[1] is a pointer. That pointer has the address of a memory block that has been allocated from Free Store and is large enough to contain 42 characters.
A resize is not possible, but one can deallocate current and allocate new:
1
2
delete [] p[1];
p[1] = new char [70];


I think I had a syntax error on the array initialization.
1
2
3
4
5
char a1[] = "Dolly";
char a2[] = {'D', 'o', 'l', 'l', 'y', '\0'};
char a3[] {'D', 'o', 'l', 'l', 'y', '\0'};
char a4[6];
strcpy(a4, "Dolly");


C-string:
The convention used by "string" functions in C is that a string is a null-terminated char array. The a2, a3, and a4 above show explicitly that there are 6 characters in "Dolly". The strlen("Dolly") returns 5.

The p[3] is a char* and therefore a statement like cout << p[3]; will assume that p[3] points to a C-string and will print characters from consecutive bytes until a '\0' is encountered. However, we did set the p[3] to point to the byte that holds value of X and we have no idea whatsoever what will be in the next bytes during runtime.

That p[3] is an example of "valid" syntax that is most likely logically utterly, horribly wrong, unexpected, etc.


Do I have to explicitly note that the p was an array of 7 pointers, but my example initialized only 4 of them and thus the 3 last pointers are still invalid?

C++ standard library has std::string, std::vector, etc so that you would not need to use new, delete, arrays, C-strings, pointers, etc for mundane things.
one thing is clear we can't initialize a double pointer at declaration time like

char * * p = somethig; not possible

A resize is not possible, but one can deallocate current and allocate new:
if I don't resize it could I change the contents within limit 42 character???
Also
* p = "something"; executes but gives warning saying "deprecated conversion from string constant to char *"

Why is this conversion bad???
Its not necessarily 'bad', just deprecated. This is because technically a C-string is a constant bit of memory - you'd have some problems if you could change all references of "Hello" to "Hfllo" throughout your program.

Therefore, in C++, you will most commonly say that you are having a pointer to const char, like so:
1
2
3
const char** p; // pointer to pointer to const char
// ...
*p = "Something";
Last edited on
could I change the contents within limit 42 character???

Could you change the values in a non-const array? Yes.

Why is this conversion bad???

You did not read the description of Const Data from the GotW link.
Well I did try to read as much as I could from GotW link the Heap VS Free Strore part is little difficult but obviously helped a lot, but I at-least have the general idea of memory layout for a C++ program now

Actually one question leads to the other and the other to another and that another to some more others so you know I need some time to get sense.

But thanks to all of you for helping.
Topic archived. No new replies allowed.