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

1.9k views Asked by At

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

There are 2 answers

5
Mahonri Moriancumer On BEST ANSWER

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);
}
0
savram On

I am going to comment your code

#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)] = '\0';//add this line, you need to end with a null terminating char
    printf("%s\n", temp);
}