lukaswilkeer
lukaswilkeer

Reputation: 345

incompatible types when returning type

I have a problem on convertToPoint functions.

int convertToPoint(int argc, char *argv[]) {
  struct point p;
  int x, y;

  p.x = atoi(argv[1]);
  p.y = atoi(argv[2]);

  return p;
}

Expect to return a struct of type point, but receive the follow error:

error: incompatible types when returning type ‘struct point’ but ‘int’ was expected return p;

What's the problem?

Upvotes: 1

Views: 7171

Answers (2)

4386427
4386427

Reputation: 44284

It's a pretty simple problem. You say you want to return a struct point but your code says that the function shall return int.

int convertToPoint(
^^^
ups, shall return int

So simply change it to struct point - like:

#include <stdio.h>

struct point 
{
    int x;
    int y;
};

struct point convertToPoint(int argc, char *argv[]) {
    struct point p;
    p.x = atoi(argv[1]);
    p.y = atoi(argv[2]);
    return p;
}


int main(int argc, char *argv[]) {
    struct point p = convertToPoint(argc, argv);
    printf("%d %d\n", p.x, p.y);
}

That said - it's a bit strange to pass argc when it's not used. Either remove that function argument or use it to check that sufficient arguments was given. Like:

    p.x = (argc > 1) ? atoi(argv[1]) : 0;
    p.y = (argc > 2) ? atoi(argv[2]) : 0;

Also notice that I removed int x, y; as those variables ain't used.

Upvotes: 2

munk
munk

Reputation: 12983

The trouble is you're telling the compiler you're returning an int with int convertToPoint(...). You want to say struct point convertToPoint(...)

The error message you're seeing tells you this, if you know how to parse it

error: incompatible types when returning type ‘struct point’ but ‘int’ was expected return p;

  1. return p; -> this is the troublesome statement as far as the compiler can tell.

  2. incompatible types when returning -> you're returning the wrong thing, check what you're returning and what the signature is

  3. type ‘struct point’ -> this is the thing you're returning in the body

  4. but ‘int’ was expected -> this is the value from your function signature.

Here is a complete example

// convert.c
#include <stdio.h>
#include <stdlib.h>

struct point {
  int x;
  int y;
};


struct point convertToPoint(int argc, char *argv[]) {
  struct point p;
  int x, y;

  p.x = atoi(argv[1]);
  p.y = atoi(argv[2]);

  return p;
}

int main(int argc, char** argv) {
    struct point p = convertToPoint(argc, argv);
    printf("%d, %d", p.x, p.y);
}

Proof it works

~/src ❯❯❯ gcc -ansi convert.c -o convert                                                                                                                                               ✘ 139 
~/src ❯❯❯ ./convert 1 2
1, 2%   

Finally, you can do a little refactoring to clean this up

// convert.c
#include <stdio.h>
#include <stdlib.h>

struct point {
  int x;
  int y;
};


struct point convertToPoint(char x[], char y[]) {
  struct point p;

  p.x = atoi(x);
  p.y = atoi(y);

  return p;
}

int main(int argc, char** argv) {
    //TODO: check for 2 args and print a helpful error message
    struct point p = convertToPoint(argv[0], argv[1]);
    printf("%d, %d", p.x, p.y);
}

Upvotes: 0

Related Questions