简体   繁体   English

'reverse_string'函数的冲突类型

[英]Conflicting types for 'reverse_string' function

I was using this SO question as part of a program that needs to reverse a string. 我正在使用这个SO问题作为需要反转字符串的程序的一部分。 The problem I am having is that I cannot seem to get the function to work. 我遇到的问题是我似乎无法使功能发挥作用。 Here is the code I have: 这是我的代码:

int main(int argc, char *argv[]){
  char *test = "Testing";
  fputs(test, stdout);
  fputs(reverse_string(test), stdout);
}

char* reverse_string(char *str){
  char temp;
  size_t len = strlen(str) - 1;
  size_t i;
  size_t k = len;
  for(i = 0; i < (len +1)/2; i++){
    temp = str[k];
    str[k] = str[i];
    str[i] = temp;
    k--;
  }  
  return str;
}

I am getting an error that there is conflicting types for 'reverse_string' 我收到一个错误,即conflicting types for 'reverse_string'存在conflicting types for 'reverse_string'

Edit: For anyone wondering here is the code that works. 编辑:对于任何想知道这里的人来说,代码是有效的。 See @chux's answer for an explanation. 请参阅@ chux的答案以获得解释。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char* reverse_string(char *str){
  char temp;
  size_t len = strlen(str) - 1;
  size_t i;
  size_t k = len;
  for(i = 0; i < (len +1)/2; i++){
    temp = str[k];
    str[k] = str[i];
    str[i] = temp;
    k--;
  }
  return str;
}
int main(int argc, char *argv[]){
  char test[] = "Testing";
  fputs(test, stdout);
  fputs(reverse_string(test), stdout);
}

You can not pass a const char * to a char * 你不能将const char *传递给char *

char *test = "Testing";
fputs(reverse_string(test), ... // bad, attempting to change constant data.
// bad as  reverse_string() is assumed to return int, but fputs() expects char *

char* reverse_string(char *str) { // Bad, there's now a function conflict

Instead 代替

char* reverse_string(char *str);  // Need to declare/define function first

char test[] = "Testing";
fputs(reverse_string(test), ... // good

[Edit] [编辑]
You problem was well identified (missing function declaration) by others. 其他人很好地识别了您的问题(缺少函数声明)。 My suggestion takes care of the next problem. 我的建议处理下一个问题。 In C, a missing declaration of a function will assume int reverse_string(...) which does not match char* reverse_string(char *str) . 在C中,函数的缺失声明将假定 int reverse_string(...)char* reverse_string(char *str)不匹配。

[Edit] [编辑]
As @Shafik Yaghmou suggests, modifying a string literal char *test = "Testing" will result in undefined behavior. 正如@Shafik Yaghmou所建议的那样,修改字符串文字char *test = "Testing"将导致未定义的行为。 Hence the char test[] = "Testing" which initializes test with "Testing\\0", but may be modified. 因此char test[] = "Testing"用“Testing \\ 0”初始化test ,但可以修改。

[Edit] [编辑]
@GreenAsJade correctly points out OP's original error message is due to the assumed int reverse_string(...) supplying an int to s in int fputs(const char * s, FILE * stream); @GreenAsJade正确地指出OP的原始错误消息是由于假定的int reverse_string(...)int fputs(const char * s, FILE * stream);s提供了一个int int fputs(const char * s, FILE * stream);

char *test1 = "Testing" is not the same thing as char test2[] = "Testing" . char *test1 = "Testing"char test2[] = "Testing" test1 becomes a char * with the size of a pointer. test1变成一个带有指针大小的char * The initial pointer value is to a string "Testing" located elsewhere in memory. 初始指针值是位于内存中其他位置的字符串“Testing”。 test2 is a char array with size 8: length of "Testing" + 1 for '\\0'. test2是一个大小为8的char数组:长度为“Testing”+ 1为'\\ 0'。 The array test2 is initialized with 'T', 'e', ... '\\0' etc. 数组test2初始化为'T','e',...'\\ 0'等。

FWIW: FWIW:

(h2hh)momerath:Documents mgregory$ cat test.c
char* reverse_string(char *str) {
  return str;
}

char *test = "Testing";

int main() {

  reverse_string(test);
}
(h2hh)momerath:Documents mgregory$ gcc test.c
(h2hh)momerath:Documents mgregory$ 

I think that the answer to the OP's question is that reverse_string has to be declared before being used, to be not int. 我认为OP问题的答案是必须在使用之前声明reverse_string,而不是int。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM