-3

I am trying to learn C to develop my skills in programming and I am trying to make an algorithm which could change the uppercase letter to lowercase letter and lowercase letter to uppercase letter.In simple algorithm I have written this code but an error occurs on it,my question is the compiler says "operand types are incompatible".What does it mean?I called my length as a pointer function to store a memory in the address to enter the console random characters.Error occurs in i is smaller than length in for loop part.I am looking forward to learn new things from your replys."int" and "int(*)char *s") it says

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

int length(char *s)
{
    int i = 0;
    int counter = 0;
    while (s[i] != NULL)
    {
        i++;
        counter++;
    }
    return counter;
}

void upperlower(char *s)
{
    int i;
    for (i = 0; i<length; i++)
    {
        if (s[i] >= 65 && s[i] <= 90)
            s[i] += 32;
        else if (s[i] >= 97 && s[i] <= 122)
            s[i] -= 32;
    }
}

int main()
{
    printf("Please write down the character string: ");
    scanf("%s", upperlower);
    printf("The character string is %s", upperlower);
    getch();
    return 0;
}
dozgunay
  • 31
  • 8
  • 1
    You have not mentioned which line the compiler complained about. And why are you calling getch at all? – bmargulies Apr 24 '17 at 18:13
  • `s[i] != NULL` ? – Rohan Kumar Apr 24 '17 at 18:14
  • 1
    `NULL` is the null pointer macro; `\0` is the null terminator character. – ad absurdum Apr 24 '17 at 18:15
  • 2
    The scanf function is expecting as its second parameter a character or integer buffer to store what the user types at the console, you are passing it the name of your conversion function whose data type does not match what the function expects, thus the compiler error. You want to pass a char to scanf, then pass that char to your function as a separate call. – Jeff D. Apr 24 '17 at 18:15
  • 1
    `char s[64]; scanf("%63s", s); upperlower(s); printf("The character string is %s", s);`, `i `i – BLUEPIXY Apr 24 '17 at 18:17
  • 2
    Use of the magic numbers `32`, `65`, `97`, etc. is entirely implementation dependent. Character encoding is not necessarily ASCII or UTF-8. Better, and portable, to use `toupper()` and `tolower()` functions from the Standard Library. – ad absurdum Apr 24 '17 at 18:19

4 Answers4

2

You seem to be confused about how to call a function.

When you do this:

for (i = 0; i<length; i++)

You are not calling the length function. You are using the name of the function in an expression, so what you have instead is a function pointer. You need to add the function call operand () along with the parameter to be passed in:

for (i = 0; i<length(s); i++)

Similarly in the main function, you are not actually calling upperlower. Also, you never define a character array to receive the string you want to read. Include that array as a local variable, pass it to scanf to get a string from the user, then pass it to the function.

char str[50];
printf("Please write down the character string: ");
scanf("%49s", str);
upperlower(str);
printf("The character string is %s", str);
dbush
  • 205,898
  • 23
  • 218
  • 273
  • May I do the 'char str[50];' part using malloc like '(char*)malloc(50);' – dozgunay Apr 24 '17 at 18:25
  • 1
    @dozgunay Yes, you can use a dynamically allocated buffer instead. Just [don't cast the result of malloc](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – dbush Apr 24 '17 at 18:33
1

scanf("%s", upperlower); expects a void* type (char* in this case) and you're providing a function pointer void (*)(char*), which cannot be cast to void*, hence the error.

If you're learning, C is not the easiest language to start with, especially starting with pointers. Think about memory layout and it will start to make more sense.

Matthieu
  • 2,736
  • 4
  • 57
  • 87
0

In the for loop of the upperlower function, you compare make the comparison i<length. i is an int variable and length is a function that you declared earlier.

When you reference here, it refers to a pointer to that points to the function itself, it does not provide the result of the function.

Instead, try changing i<length to i<length(s), this way, length return a result that can be compared to an int.

The same could be said for the scanf in the main function. Here you should declare an array to store the string and then pass that to the functions:`

char str[32];
scanf("%s", str);
upperlower(str)
printf("The character string is %s", str);
getch();
0

You're doing some mistakes in your code like:

  • comparing character to a null pointer s[i] != NULL, maybe you meant \0
  • Passing function name instead of string on line 33. You're supposed to print converted string.
  • Not allocating the memory for the string passing function in scanf Line 31 : scanf("%s", upperlower);
  • Comparing i which an integer to a function pointer, Line 20: for (i = 0; i<length; i++) maybe you mean i < length(s)

Apart from these i changed main like this:

  char aStr[1000];
  printf("Please write down the character string: ");
  scanf("%s", aStr);
  upperlower(aStr);
  printf("The character string is %s", aStr);
  return 0;

On running it i was able to see the desired output:

~/Documents/src : $ gcc testType.c 
~/Documents/src : $ ./a.out 
Please write down the character string: Rohan
The character string is rOHAN
Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40