首页 > 解决方案 > Reverse String with a pointer to a function, which executes the String reverse

问题描述

I would like to reverse a String with an pointer to a function, which executes the String reverse. I have the feeling that I did not grasp the concept of using pointer to variables or functions correctly, so I would be very thankful if someone could expain me, where I am thinking wrong here:

1) Define a pointer to a function:

char *strrev(char *str)
{
  char *p1, *p2;

  if (! str || ! *str)
        return str;
  for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
  {
      *p1 ^= *p2;
      *p2 ^= *p1;
      *p1 ^= *p2;
   }
   return str;
}

2) Now in my main I define a pointer, which matches the function I defined above:

int main(void) {

    char (*functPtr)(char);
    functPtr = &strrev;

3) Now I define the String, which I want to reverse, define a new pointer and let the pointer point to the address space of the String.

char str[50] = "Hello, World";
char *pointer[50];
pointer[50] = &str[50];

4) Lastly I define a new String and write the result of the function, which call through the pointer, which points to the pointer to the function.

char t[50] = (*functPtr)(pointer[50]);
printf("%s\n", str);
return(0);
}

Unfortunaly I get all kinds of error message such as:

Ü1.c:29:10: error: array initializer must be an initializer list or string literal
    char t[50] = (*functPtr)(pointer[50]);
         ^
    Ü1.c:27:5: warning: array index 50 is past the end of the array (which contains 50 elements) [-Warray-bounds]
        pointer[50] = &str[50];
        ^       ~~
    Ü1.c:26:5: note: array 'pointer' declared here
        char *pointer[50];
        ^
    Ü1.c:29:30: warning: array index 50 is past the end of the array (which contains 50 elements) [-Warray-bounds]
        char t[50] = (*functPtr)(pointer[50]);
                                 ^       ~~
    Ü1.c:26:5: note: array 'pointer' declared here
        char *pointer[50];
        ^
    2 warnings and 1 error generated.

标签: cpointersreversec-stringsfunction-definition

解决方案


I summarize my comments in this answer, as it gives more space for details of the comments.

char (*functPtr)(char); should be char *(*functPtr)(char *); as it takes a pointer to a char, not a char. Likewise it returns a pointer.

char *pointer[50]; would be an array of 50 pointers, You want to say "a pointer to an array of 50 chars". In C we don't say that. We just say "a pointer to a char" and don't say how many. So char *pointer; would be enough.

char t[50] = (*functPtr)(pointer[50]); is not correct in C.

You want to assign the result of funcPtr to the array t. But here you mix initialization with assignment. char t[50]; declares an array of 50 chars. You can initialize it by giving it a value, for example char t[50] = "Hello World"; which will have the compiler copy "Hello World" to the array.

But you try to assign the function pointer to the array. You probably intend to put the result of the function into the array.

Note also that you cannot "assign" an array to another array. You can only copy it.

So the correct code would be:

char *(*functPtr)(char *);
functPtr = &strrev;

char str[50] = "Hello, World";

char t[50];
char *s= funcPtr(str);  // call the function and save the returned pointer
strcpy(t, s);           // now copy the result to your array.
printf("%s\n", t);      // and print it

Note: char str[50] = "Hello, World"; is correct and, just so you'll know, char *str = "Hello, World"; is wrong. Why? Because the second str will point to read-only memory (a "string literal") and any attempt to modify it would abort the program. But here you did it right.


推荐阅读