0

My question is related to my previous post:

explicit specialization: syntax error?

I am trying to pass arrays of pointer-to-chars as an argument to a function (which I will later incorporate to a specialized function from previous post), but I cannot get the syntax right.

The following were declared under main program:

char c0[30] = { "see if this works" };
char c1[30] = { "functions" };
char c2[30] = { "explicit specialization" };
char *d[] = { c0, c1, c2 }; 

the next line prints "functions " as I was expecting:

cout << "test print d[1] " << d[1] << endl;

The next step is to test whether I am able to return the character-array that I want to return, but my syntax is incorrect. The following returns 's' (from c0) instead of an entire char-array:

function call:

cout << "string compare is " << compare2(*d, 3);

function declaration:

char compare2(char const arr2[], int n) {
    char temp;
    temp = arr2[0];

    return temp;

appreciate the help!

0

1 Answer 1

0

Oh never mind. I figured it out :D

pointer declaration:

char *(d[]) = { c0, c1, c2 };

function call:

cout << "string compare is " << compare2(d, 3);

function definition:

char * compare2(char * const arr2[], int n) {
    char * temp;
    temp = *(arr2+2);

    return temp;
}
Sign up to request clarification or add additional context in comments.

6 Comments

The pointer declaration, function call, and function signature were fine. You were dereferencing a char array when you didn't want to. Unless you were trying to do something much different than what you had been doing.
char *d[] = and char *(d[]) = are the same
char *(d[]) looks very bad. Note that you're not using n in compare2. There's probably a reason you're passing it, right?
@JamesRoot Thanks. The initial code was giving me a character in the character array - I was trying to return the entire (char) string.
@M.M Thanks. I was desperately adding and removing characters from the code and forgot about this. It was not compiling most of the times I changed it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.