Thread: Function returning pointer variable...

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    24

    Function returning pointer variable...

    Code:
    int *createFunction(int x);
    
    
    
    
    int main(){
        int *foo = createFunction(35);
        
        printf("%d\n", *foo);
        
        return 0;
    }
    
    
    int *createFunction(int x){
        int *var;
        var = malloc(sizeof(int));
        
        *var = x;
        
        return var;
    }
    When I change the type of createFunction, 'int' to 'void', it works fine.

    What's wrong with my code?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,748
    So which of these things are you doing?

    1. Not including stdlib.h ?
    2. Not compiling with a C compiler (ie, using C++ by mistake).

    You need to do better than "it's not working".
    Post your actual results and/or error messages.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    24
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef int gbType(int);
    int *createFunction(int x);
    
    
    
    
    int main(){
        gbType *foo = createFunction(35);
        
        printf("%d\n", *foo);
        
        free(foo);
        
        return 0;
    }
    
    
    int *createFunction(int x){
        int *var;
        var = malloc(sizeof(int));
        
        *var = x;
        
        return var;
    }

    Code:
    test.c: In function 'main':
    test.c:9:19: warning: initialization from incompatible pointer type [enabled by default]
         gbType *foo = createFunction(35);
    Well it's not really an error...but why am I getting warning? and is there way to remove this warning?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,748
    Yes, you change typedef int gbType(int); to be something else.

    cdecl> explain int gbType(int);
    declare gbType as function (int) returning int

    This isn't int pointer (which is implied by everything else), so it just complains.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. returning a pointer to local variable
    By Satya in forum C Programming
    Replies: 2
    Last Post: 08-18-2014, 12:39 AM
  2. Function returning *this pointer
    By 4c0 in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2013, 05:35 AM
  3. returning a pointer to stack variable
    By justAlamer in forum C++ Programming
    Replies: 10
    Last Post: 03-09-2010, 08:41 AM
  4. Returning zero valued variable from function doesn't work
    By thetinman in forum C Programming
    Replies: 7
    Last Post: 10-19-2006, 05:53 PM
  5. returning a pointer to a destroyed variable?
    By krygen in forum C++ Programming
    Replies: 6
    Last Post: 11-06-2004, 02:22 PM