Thread: Pointer from a cast warning

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Pointer from a cast warning

    Am I doing this the C++ way? I am trying to write a program that reads in
    two values send them to a function and calulates if the second is a multiple
    of the first. It compiles fine, but i get a seg fault and a warning about
    an integer to a cast. I belive the problem is in the return statements but I
    am having trouble decipering what I am doing wrong

    Code:
    #include <stdio.h>
    #include <string.h>
    
    /*function prototype*/
    char isMultiple ( int, int );
    
    /*main function - begins program execution -----------------------------------*/
    int main ( void )
    {
       int num1;
       int num2;
    
       printf("Enter two values: ");
       scanf("%d %d", &num1, &num2);
    
       printf("\nResult is: %s", isMultiple ( num1, num2));
    
       getch(); /*freeze console output window*/
    
       return 0; /*return value from int main*/
    }
    
    /*function to define if second is a multiple of the first*/
    char isMultiple ( int x, int y )
    {
       char result1[ 35 ];
       char result2[ 20 ];
    
       strcpy(result1, "First is a multiple of second");
       strcpy(result2, "No multiple found");
    
       if (( x % y ) == 0 )
       {
          return result1;
       }
    
       else
       {
          return result2;
       }
    }
    Double Helix STL

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    1) You need to return a pointer to char, not char itself.
    2) After making that change, I get errors about returning an address of a local variable. That can be fixed just by returning the strings directly, for example:
    Code:
      return "First is a multiple of second";
    and get rid of the char arrays result1 and result2, since you don't need them. After this, I get an error

    /tmp/ccKcYJZQ.o: In function `main':
    swgh.c:(.text+0x5a): undefined reference to `getch'
    collect2: ld returned 1 exit status

    and I can't help you there since I'm not familiar with getch().

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    getchar() instead perhaps, more portable too.

    Or simply return a numerical result (or error), or alternatively pass in a char pointer you could write to (designed to store the 'text result').

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    char isMultiple ( int x, int y )
    {
       char result1[ 35 ];
       char result2[ 20 ];
    
       strcpy(result1, "First is a multiple of second");
       strcpy(result2, "No multiple found");
    
       if (( x &#37; y ) == 0 )
       {
          return result1;
       }
    
       else
       {
          return result2;
       }
    }
    You cant return a local variable of that functioin. You should be getting an error for that. What you could do is returna literal string const.

    That should get rid of all your error and function prototype should reflect.

    ssharish2005

  5. #5
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Code:
    char *result1="First is a multiple of second";
    char *result2="no multiple found";

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-27-2009, 02:33 PM
  2. Replies: 5
    Last Post: 08-12-2007, 05:26 PM
  3. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM