"Redefinition - Different Basic Types" 在 C 中使用指针时出错

"Redefinition - Different Basic Types" Error while working with pointers in C

我应该写两个函数。一个将采用 char 数组并将所有字母变为大写,另一个将反转数组并打印出名称。我要使用指针。我非常有信心我已经正确编写了函数,除了我是 C 的新手并且似乎在指针方面苦苦挣扎。我收到两个错误,每个函数一个。他们都说“'Upper/Reversed':重新定义;不同的基本类型”。我尝试更改多项内容,但似乎无法解决问题。你能看到我错过了什么吗?感谢您的帮助。

#include <stdio.h>
#include <ctype.h>
#include <string.h>

void main()
{
    char firstName [10] = "John Smith";
    char secondName[10] = "Mary Cohen";
    char thirdName[13] = "Carl Williams";

    UpperCase(firstName);
    UpperCase(secondName);
    UpperCase(thirdName);

    Reversed(firstName);
    Reversed(secondName);
    Reversed(thirdName);
}

void UpperCase(char* name)
{
    for (int i = 0; i < strlen(name); i++)
    {
        *(name + i) = toupper(*(name + i));
    }
}

void Reversed(char* name)
{
    char temp[13];
    int count = 0;
    for (int i = strlen(name); i > 0; i--)
    {
        temp[count] = *(name + i);
        count++;
    }
    printf("%s\n", temp);
}

C 编译器是有条不紊的。它期望在使用它们之前定义事物。因此有几种方法可以解决这个问题:

一种方法是对函数进行排序,以便在调用函数的上方声明它们:

#include <stdio.h>
#include <ctype.h>
#include <string.h>

void UpperCase(char* name)
{
    for (int i = 0; i < strlen(name); i++)
    {
        *(name + i) = toupper(*(name + i));
    }
}

void Reversed(char* name)
{
    char temp[13];
    int count = 0;
    for (int i = strlen(name); i > 0; i--)
    {
        temp[count] = *(name + i);
        count++;
    }
    printf("%s\n", temp);
}

void main()
{
    char firstName [10] = "John Smith";
    char secondName[10] = "Mary Cohen";
    char thirdName[13] = "Carl Williams";

    UpperCase(firstName);
    UpperCase(secondName);
    UpperCase(thirdName);

    Reversed(firstName);
    Reversed(secondName);
    Reversed(thirdName);
}

另一种方法是对函数进行原型设计(在调用函数的上方):

#include <stdio.h>
#include <ctype.h>
#include <string.h>

void UpperCase(char*);
void Reversed(char*);

void main()
{
    char firstName [10] = "John Smith";
    char secondName[10] = "Mary Cohen";
    char thirdName[13] = "Carl Williams";

    UpperCase(firstName);
    UpperCase(secondName);
    UpperCase(thirdName);

    Reversed(firstName);
    Reversed(secondName);
    Reversed(thirdName);
}

void UpperCase(char* name)
{
    for (int i = 0; i < strlen(name); i++)
    {
        *(name + i) = toupper(*(name + i));
    }
}

void Reversed(char* name)
{
    char temp[13];
    int count = 0;
    for (int i = strlen(name); i > 0; i--)
    {
        temp[count] = *(name + i);
        count++;
    }
    printf("%s\n", temp);
}

我要评论你的代码

#include <stdio.h>
#include <ctype.h>
#include <string.h>

void main()//change to int main. People say main function should always return an int. It's probably on the C standard or something, although personally I have never seen a code that doesn't work just because the return type is void. But you know, just to avoid arguing over it
{
    char firstName [10] = "John Smith";//it should be char firstName [11] for the null terminating char(you need the null terminating char because you use printf, which expects a null terminated string)
    char secondName[10] = "Mary Cohen";//same as above
    char thirdName[13] = "Carl Williams";//it should be char thirdName[14] for the same reason

    UpperCase(firstName);
    UpperCase(secondName);
    UpperCase(thirdName);

    Reversed(firstName);
    Reversed(secondName);
    Reversed(thirdName);
}

void UpperCase(char* name)
{
    for (int i = 0; i < strlen(name); i++)
    {
        *(name + i) = toupper(*(name + i));
    }
}

void Reversed(char* name)
{
    char temp[13];//since your biggest string has a size of 14, including the null-terminating char, this should be temp[14]
    int count = 0;
    for (int i = strlen(name); i > 0; i--)//it should be for (int i = strlen(name) - 1; i >= 0; i--)
                                          //that is because strlen doesn't count the null terminating char and in C you start counting at 0, not 1. So if your string has 10 chars including the null terminating char, it goes from 0 to 9, and strlen returns 9, not 10. If you let i > 0 you will stop at 1, but as I said, it starts at 0
    {
        temp[count] = *(name + i);
        count++;
    }
    temp[strlen(name)] = '[=10=]';//add this line, you need to end with a null terminating char
    printf("%s\n", temp);
}