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!

Community
  • 1
  • 1
mi5tch
  • 1
  • 2

1 Answers1

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;
}
mi5tch
  • 1
  • 2
  • 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. – Weak to Enuma Elish Nov 23 '15 at 01:33
  • `char *d[] = ` and `char *(d[]) = ` are the same – M.M Nov 23 '15 at 02:21
  • `char *(d[])` looks very bad. Note that you're not using `n` in `compare2`. There's probably a reason you're passing it, right? – erip Nov 23 '15 at 02:24
  • @JamesRoot Thanks. The initial code was giving me a character in the character array - I was trying to return the entire (char) string. – mi5tch Nov 26 '15 at 09:00
  • @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. – mi5tch Nov 26 '15 at 09:03
  • @erip Thanks. 'n' will later be used - the function is supposed to compare char-string lengths but I wanted to make sure that I am returning the correct array first. – mi5tch Nov 26 '15 at 09:04