#include <iostream>
#define SPACE 40
usingnamespace 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) = newchar [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 :(
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?
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.