Thread: incompatible type return

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    35

    incompatible type return

    Hi,

    I am testing a code from c tutorial and tried below code and did compile and it fails to incompatible type in return.. and I am not sure why ?

    Code:
    #include <stdio.h>
    
    struct invStruct {
       char manuf[25];
       char model[15];
       int diskSpace;
       int memSpace;
       int quantity;
       float cost;
       float price;
    }
    
    main()
    {
      int ctr;
      struct invStruct items[3];   /* Array of 3 structure variables */
    
      for ( ctr = 0; ctr < 3; ctr++ )
      {
          printf( "What is the manufacturer of item #%d?\n", (ctr + 1) );
          gets( items[ctr].manuf );
          puts( "what is the mode? ");
          gets(items[ctr].model);
          puts("how many megabytes of disk space? ");
          scanf(" %d", &items[ctr].diskSpace);
          puts("How many megabytes of memory space? ");
          scanf(" %d", &items[ctr].memSpace);
          puts("How many are there? ");
          scanf(" %d", &items[ctr].quantity);
          puts("How much does the item cost? ");
          scanf(" %d", &items[ctr].quantity);
          puts("How much does the item cost? ");
          scanf(" %f", &items[ctr].cost);
          puts("How much does the item retail for? ");
          scanf(" %f", &items[ctr].price);
          getchar();   /* clears input of last newline pressed for next round of input */
       }    /* Now prints the data */
       printf("\n\nHere is the inventory: \n");
       for ( ctr = 0; ctr < 3; ctr++ )
       {
          printf("#%d: Manufacturer: %s", (ctr + 1), items[ctr].manuf);
          printf("\nModel: %s", items[ctr].model);
          printf("\nDisk: %d megabytes\t", items[ctr].diskSpace);
          printf("Memory: %d megabytes\t", items[ctr].memSpace);
          printf("Quantity: %d units\n", items[ctr].quantity);
          printf("Cost: $%.2f\t", items[ctr].cost);
          printf("Selling price: $%.2f\n\n", items[ctr].price);
        }
        return 0;
    }

    result of gcc'ing
    Code:
    root@my-laptop:/code_upload/c# gcc -o struct.o struct.c
    struct.c: In function âmainâ:
    struct.c:51: error: incompatible types in return
    struct.c:16: warning: return type of âmainâ is not âintâ
    root@my-laptop:/code_upload/c#

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    int main() would be a start

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    And adding a missing semicolon would be the next step.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Getting rid of gets would be a 3rd.
    SourceForge.net: Gets - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    Unhappy same problem

    I'm getting the same error, and just can't figure it out. Someone said to add a semicolon, and I can't see where he needs it. I'm guessing that's my problem.

    Here's the directions if this helps:
    Show how to declare a tag named complex for a structure with two members, real and imaginary, of type double.

    use the complex tag to declare variables named c1,c2, and c3.

    write a function named make_complex that stores its two arguments (both of type double) in a complex structure, then returns the structure.

    #include <stdio.h>

    double make_complex (double a, double b){
    struct {
    double real;
    double imaginary;
    }p;
    p.real = a;
    p.imaginary = b;
    return p;

    }


    int main() {
    struct complex {
    double real;
    double imaginary;
    };
    struct complex c1,c2,c3; // (b)
    c3 = make_complex(1.0, 2.0);

    }
    thanks in advance for the help!




    denver electrician

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    First, Welcome to the Forum, VCrewChief!

    I don't see a missing semi-colon. I do see an int main() function with no return of the expected int, though.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    Thank you for the warm welcome

    I'm still getting the same error. i tried the return0; and void main()





    electrician in denver

  8. #8
    Registered User
    Join Date
    Jan 2010
    Location
    Ca, US
    Posts
    29
    Your main needs to be int main(.....
    and you also need the return at the end.

    Looks like you used one or the other each time.

    Dylan

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    In your code you are defining structures inside your functions. Move the structure in main to before make_complex. Delete the structure inside make_complex.

    Use the structure you moved outside the functions inside make_complex and main.


    Jim

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    Thank you Jim

    You are the man. Here's what I did:


    #include <stdio.h>


    struct complex {
    double real;
    double imaginary;
    };

    struct complex make_complex (double a, double b){
    struct complex p;
    p.real = a;
    p.imaginary = b;
    return p;

    }

    void main() {

    struct complex c1,c2,c3; // (b)
    c3 = make_complex(1.0, 2.0);
    //printf("%.1f, %.1f", c3.real, c3.imaginary);
    }



    Induction retrofit

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Glad I could help.

    For future reference when you have a question not directly related to the current thread, create an new thread.

    Also main() should be int main() and return 0 before exiting.

    It also helps to indent your code sections. It makes it easier to read the code.

    Code:
    #include <stdio.h>
    
    
    struct complex {
    	double real;
    	double imaginary;
    };
    
    struct complex make_complex (double a, double b) {
    	struct complex p;
    	p.real = a;
    	p.imaginary = b;
    	return p;
    
    }
    
    int main() {
    
    	struct complex c1,c2,c3; // (b)
    	c3 = make_complex(1.0, 2.0);
           //printf("%.1f, %.1f", c3.real, c3.imaginary);
          return(0);
    }
    Jim

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Awesome, thanks for the advice. it's indented in my compiler, it was just copied funkily.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Return from incompatible pointer type
    By aladin in forum C Programming
    Replies: 2
    Last Post: 03-22-2009, 08:58 AM
  2. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 01:56 AM
  3. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  4. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  5. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 10:40 AM