Search code examples
cvisual-studiopointersredefinition

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


I am supposed to be writing two functions. One that will take a char array and make all the letters uppercase and another that will reverse the array and print the names out. I am to use pointers. I'm pretty confident I have the functions written correctly, except I am very new to C and seem to be struggling with the pointers aspect. I am receiving two errors, one for each function. They both say " 'Upper/Reversed': redefinition; different basic types". I have tried changing multiple things but can't seem to fix the problem. Can you see what I am missing. Thank you for your help.

#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);
}

Solution

  • The C compiler is methodical. It expects things to be defined prior to using them. Hence there are several ways to resolve the problem:

    One way is to order functions so that they are declared above where they are called:

    #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);
    }
    

    Another way is to prototype the functions (above where they are called):

    #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);
    }