1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Very simple C Function

Discussion in 'Programming' started by Silver89, Apr 29, 2010.

  1. #1
    How do I use the following function?

    
    /* sets the variable referenced by r to zero and returns the previous value of r */
    int testfunc(int *r);
    
    Code (markup):
    I'm trying the following but no luck?

    
    #include <stdio.h>
    #include "testfunc.h"
    
    int main()
    {
            int testValue=1;
            int testResult;
    
            testfunc(testValue);
    
            printf("Test value now equals %d\n",testValue);
            printf("Returned value equals %d\n",testResult);
    
            return 0;
    }
    
    
    
    Code (markup):
     
    Silver89, Apr 29, 2010 IP
  2. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #2
    Hi,

    try this:
    
    int main(){
            int testResult,testValue=1;
    testResult = testfunc(&testValue);
    printf("Test value now equals %d\nReturned value equals %d\n",testValue,testResult);
    return 0;
    }
    
    PHP:
     
    koko5, Apr 29, 2010 IP
  3. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #3
    What does the:

    
    int *r
    
    Code (markup):
    Acutally mean? I've not come across the * before.
     
    Silver89, Apr 29, 2010 IP
  4. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #4
    This is pointer - check this link
     
    koko5, Apr 29, 2010 IP
  5. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #5
    I'm receiving the following error when compiling:

    
    gcc testlscore.c -o testscore
    /tmp/ccQRp93p.o: In function `main':
    /tmp/ccQRp93p.o(.text+0x26): undefined reference to `testfunc'
    collect2: ld returned 1 exit status
    
    Code (markup):
     
    Silver89, Apr 29, 2010 IP
  6. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #6
    It seems you forgot to include the file with testfunc().

    testfunc.h holds only function prototype definition (I guess), not the function itself: testfunc.c or testfunc.cpp?
    Here is my test and it works under Xubuntu:
    #include <stdio.h>
    
    int testfunc(int *r);
    int main(void){
            int testResult,testValue=1;
    testResult = testfunc(&testValue);
    printf("Test value now equals %d\nReturned value equals %d\n",testValue,testResult);
    return 0;
    }
    int testfunc(int *r){
    int tmp;
    tmp=*r;
    *r=0;
    return tmp;
    }
    
    PHP:
     
    koko5, Apr 29, 2010 IP
    Silver89 likes this.