Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Passing by reference in C

I learned C++ all through High School and am having to go back and relearn C for college, and have hit a wall. I know how to pass by reference in C++, but is there any way to do it in C. My guess was pointers, but I have yet to use them and got lost trying. My program works fine using global variables, but apparently those arent allowed. I guess my question is can anyone show me an example of "passing by reference" between two functions using more than one variable in C. Thanks

Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : I learned C++ all through High School and am having to go back and relearn C for college, and have hit a wall. I know how to pass by reference in C++, but is there any way to do it in C. My guess was pointers, but I have yet to use them and got lost trying. My program works fine using global variables, but apparently those arent allowed. I guess my question is can anyone show me an example of "passing by reference" between two functions using more than one variable in C. Thanks
    :
    [blue]In "C" there are no references - I mean '&' in declarations. But, there are pointers and you can use them to return more than one values:[/blue][code]
    void GetCoords (int* px, int* py)
    {
    px [0] = 32;
    py [0] = 16;

    // OR THE SAME CODE WITH FREAKING '*'...
    *px = 32;
    *py = 16;
    }

    int main ()
    {
    int x = 1;
    int y = 2;

    printf ("
    BEFORE: x=%d y=%d", x, y);
    GetCoords (&x, &y);
    printf ("
    AFTER: x=%d y=%d", x, y);

    return 0;
    }
    [/code]
  • green_96teggreen_96teg Member Posts: 3
    [b][red]This message was edited by green_96teg at 2002-9-23 14:42:3[/red][/b][hr]
    Ok, I think I understand a little bit better. I took my program which used global variables and tried to convert it to using only ptrs. I still have a problem somewhere in my conv or output function that is messing it all up. Id apreciate it if someone would look through it and help me out. It probably has something to do with my array, I'm clueless as to how I should go about it. Thanks
    [code]

    #include
    #include
    void input(int* pbase,int* pnum);
    void conv(int* pbase,int* pnum,int* pi, int* pconver[20]);
    void output(int* pbase,int* pnum,int* pi, int* pconver[20]);


    int main()
    {
    int ans=0,base=0,num=0,i=0;
    int conver[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    do
    {
    input(&base,&num);
    conv(&base,&num,&i,&conver[20]);
    output(&base,&num,&i,&conver[20]);
    printf("
    Would you like to try again using different numbers?
    Enter 1 for yes and 0 for no
    ");
    scanf("%d",&ans);
    }while(ans!=0);
    return 0;
    }
    void input(int* pbase, int* pnum)
    {
    int check=0;
    do
    {
    printf("
    Enter a base between 2 and 20 (inclusive) for the conversion
    ");
    scanf("%d",& *pbase);
    if( *pbase>=2&& *pbase<=20)
    check=0;
    else
    {
    check=1;
    printf("
    You entered an unusable number. Try again
    ");
    }
    }while(check!=0);
    do
    {
    printf("
    Enter a non-negative number between 1 and 1000 (inclusive) you wish to convert into base %d
    ", *pbase);
    scanf("%d",& *pnum);
    if( *pnum>=1&& *pnum<=1000)
    check=0;
    else
    {
    check=1;
    printf("
    You entered an unusable number. Try again
    ");
    }
    }while(check!=0);
    printf("
    You have chosen a base of %d and the number %d
    Press any key to continue", *pbase, *pnum);
    getch();
    }
    void conv(int* pbase, int* pnum,int* pi, int* pconver[20])
    {
    int quot=0, x=0;
    x=*pi;
    quot= *pnum;
    do
    {
    *pconver[x]=quot% *pbase;
    quot=quot/ *pbase;
    x++;
    }while(quot!=0);
    *pi=x;
    }
    void output(int* pbase,int* pnum,int* pi,int* pconver[20])
    {
    int y=0;
    y=*pi;
    printf("
    The input decimal integer %d is equal to ",*pnum);
    do
    {
    if(*pconver[y]>9)
    {
    *pconver[y]=*pconver[y]+55;
    printf("%c",(char) *pconver[y]);
    }
    else
    {
    printf("%d",*pconver[y]);
    }
    y--;
    }while(y>=0);
    printf(" in base %d
    ",*pbase);
    *pi=y;
    }
    [/code]


  • green_96teggreen_96teg Member Posts: 3
    thanks for the help, ill probably be needing more later...
  • Chris BrownChris Brown USAMember Posts: 4,496 ✭✭

    _______ < http://forcoder.org /> free ebooks and video tutorials about < Delphi Python JavaScript MATLAB C Swift Visual Basic .NET Perl PHP Java C++ Objective-C R Ruby Assembly C# Go Scratch Visual Basic PL/SQL Kotlin Apex Logo Bash ABAP ML Erlang Prolog Julia Scala Lua Alice Rust Lisp Fortran VBScript Scheme Ada SAS COBOL D Dart Awk FoxPro Transact-SQL Crystal Clojure Hack LabVIEW F# /> __________

Sign In or Register to comment.