Okay, well I am having some problems with double pointers. Now, I am hardly a programming expert and I would be the first to admit it. First off this is not homework. I can use a normal pointer okay and have had no problems doing this, but I have actually never used double pointers either in my programming classes when I was in college or in my job. I recently was asked about this though in a job interview even though the job never said anything about C or C++ expect for it would be nice to have C#. That's microsoft for you. I would like to understand this so in case I ever get asked this again.
Anyway I was given this below which I totally flubbed.
Code:
void Reverse(char* str, char** rev)
Since then about a week now I have tried to learn how to use the double pointer but have not had much luck. So, I thought maybe someone here could help me understand what I am doing wrong. I can get the same function with a normal pointer working find, but not using a double pointer. The code is below.
Oh, the function Reverse2 is what I am having trouble with.
Code:
#include <iostream>
#include <string>
using namespace std;
int strlen(char* str)
{
int count = 0;
while( *str++ != '\0')
{
count++;
}
return count;
}
void Reverse(char* str, char* rev)
{
int ptrcnt = 0;
ptrcnt = strlen(str)-1;
cout << "ptrcnt: " << ptrcnt << endl;
for(int i=ptrcnt; i >= 0; i--)
{
*(rev++) = *(str+i);
}
*rev = '\0';
}
void Reverse2(char* str, char** rev)
{
int ptrcnt = 0;
ptrcnt = strlen(str)-1;
cout << "ptrcnt: " << ptrcnt << endl;
for(int i=ptrcnt; i >= 0; i--)
{
**(rev++) = *(str+i);
}
**rev = '\0';
}
int main()
{
cout << "Hello world!" << endl;
char* string1 = new char;
char* string2 = new char;
char** string3 = new char*;
string3 = &string2;
cin.getline(string1, 80);
cout << string1 << endl << endl;
//Reverse(string1, string2);
//Reverse2(string1, string3);
Reverse2(string1, &string2);
cout << string1 << endl;
cout << string2 << endl;
delete string1;
delete string3;
delete string2;
return 0;
}