How do I get the address of a dynamic pointer?

How do I assign a 2 level (**) pointer to just a dynamic pointer (new type *)?

Is this possible?

I've tried this.
1
2
char ** g; 
g = new (nothrow) char *[i];


but as you can see there's no reference etc.
That should work.
let me print the whole code

code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#define SPACE 40
using namespace std;

void generic(void (*funct)(char *), char *p);


void reverse(char ** g); 

int main()
{

int i; 

char question[] = "How many strings would you like to reverse?";
char question2[] = "Enter the string, quick!";
cout << question << endl;
cin >> i;

char ** g; 

g = new (nothrow) char *[i];
if (g == 0)
	cout << "Dynamic memory not allocated" << endl << "Exiting" << endl;
else
{
	for (int n = 0; n < i; n++)
		*(g+n) = new char [SPACE];
}

	for (int n = 0; n < i; n++)
	{
		cout << question2 << endl;
		cin >> **(g+n);
	}

	for (int n = 0; n < i; n++)
	{
	cout << **(g+n) << endl;;
	}

return 0;

}


edit: jsut before I saw your post, I was about to declare a triple level pointer and point it to g... well i forgot what i was going to do... i thought of it so quick... and forgot quicker :(
Last edited on
bump

Well I've noticed that my code is correct... I have noticed that there is something wrong with the char pointers and what not.

As it works with integers.

Hmm.... isn't the empty dynamic chars still compatible with initializing through the cin??
I've noticed that using sizeof() on the **(p+n) yields that it's only 1 bit long when it should be 40... why is that?
Last edited on
typedef is your friend.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

const size_t SPACE = 40;

typedef char* pchar;

int main()
{
    char question[] = "How many strings would you like to reverse?";
    char question2[] = "Enter the string, quick!";

    cout << question << endl;
    int n;
    cin >> n;

    if (pchar *array = new (nothrow) pchar[n])
    {
        for (int i = 0; i < n; ++i)
            array[i] = new char [SPACE];

        for (int i = 0; i < n; ++i)
        {
            cout << question2 << endl;
            cin >> array[i];  // let's hope SPACE is large enough
        }

        for (int i = 0; i < n; ++i)
            cout << array[i] << endl;

        for (int i = 0; i < n; ++i)
            delete [] array[i];
        delete [] array;
    }
    else
    {
	cout << "Dynamic memory not allocated" << endl << "Exiting" << endl;
    }

    return 0;
}
I've noticed that using sizeof() on the **(p+n) yields that it's only 1 bit long when it should be 40... why is that?
1 byte (not bit). That is because
**(p+n) => char
while
*(p+n) => char *
/ the expected string. No way that it returns 40 since it's dynamic and sizeof() sees only the pointer/type

I recommend that you use std::string and std::vector instead which would make life significant simplier.

Btw: line 8 should be void reverse(char *g); // Note only one * since you want to reverse only 1 string or you need another paramter that holds the number of string to convert.
Topic archived. No new replies allowed.