Pointer Usage

Okay, so I got a program that I'm suppose to analyze and I pretty much understand most of it, but I'm having some difficulties understanding funcC. The output is fairly obvious, but I'm not entirely sure how to refer to the parameter in funcC, or how to refer to the first statement. Also, why exactly does the second statement need to be dereferenced before the ++. My prof specifically said I would need sources outside my book to analyze this program, but it's kind of difficult to find any useful information when I'm not even sure how to refer to it. Any help or links would be greatly appreciated, thankyou.

#include <iostream>
using namespace std;

void funcA(char *s)
{
s[0] = 'x';
s++;
}

void funcB(char* &s)
{
*(s+1) = 'y';
s++;
}

void funcC(char** s)
{
*((*s)+1) = 'z';
(*s)++;
}

int main()
{
char str[] = "abc";
char *ptrA = str, *ptrB = str;

funcA(ptrA);
cout << "ptrA = " << ptrA << endl;

funcB(ptrB);
cout << "ptrB = " << ptrB << endl;

char* ptrC = ptrB;
funcC(&ptrC);
cout << "ptrC = " << ptrC << endl;
}

Output looks like this
ptrA = xbc
ptrB = yc
ptrC = z
When function funcC is called its local variable s points to ptrC. So expression ( *s ) is in fact ptrC. Expression ( *s )++ is equivalent to expression ptrC++. So now ptrC points to the next (last) character in the original string. This last character in function funcC was changed to 'z'. After exiting the function ptrC keeps its new value that is pointer to character 'z' in the original string.

void funcC(char** s)
{
*((*s)+1) = 'z';
(*s)++;
}
Last edited on
ooo okay thank you that was a lot of help!
Topic archived. No new replies allowed.