Parameters by Reference

This is a discussion on Parameters by Reference within the C Programming forums, part of the General Programming Boards category; Hi all, I'll start by saying sorry for my ignorance - I am 100% a hobby C programmer for a ...

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

    Parameters by Reference

    Hi all,

    I'll start by saying sorry for my ignorance - I am 100% a hobby C programmer for a text-based game. I am a professional C# developer (sorry) so most of my problems are around pointers - and this is one of them.

    I have two functions, headed:

    Code:
    void	one_hit					args((CHAR_DATA *ch, CHAR_DATA *victim, int dt, int wpn));
    int		get_one_hit_damage		args((CHAR_DATA *ch, CHAR_DATA *victim, int dt, int wpn));
    the first is small so I'll post it:

    Code:
    void one_hit(CHAR_DATA *ch, CHAR_DATA *victim, int dt, int wpn)
    {
    	int dam = get_one_hit_damage(ch, victim, dt, wpn);
        if(dam == -1) return;
        damage(ch, victim, NULL, dam, dt, wpn);
        tail_chain();
        return;
    }
    the second is huge but to cut a long story short, it returns an integer and, importantly, changes the value of dt.

    Having read all I can about passing parameters by reference in C, I have come to this conclusion (echo'd in the header, or without - same error):

    Code:
    int get_one_hit_damage(CHAR_DATA *ch, CHAR_DATA *victim, int &dt, int wpn)
    {
        ...
        dt = 1;
        return 100;
        ...
    }
    ... I get the impression that using the & here infront of the dt parameter should make it a reference parameter. However when I compile I get...

    Code:
    fight.c:611: error: expected ';', ',' or ')' before '&' token
    I've tried putting the same & in the function header but it doesn't like that either.

    I'm sure this is simple but I just don't know how to work it...

    Can anyone help?

    Cheers,

    Dom

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    You cannot pass by reference in C. What you remember is C++. In C++, there is indeed the option to use & to pass by reference. In C, there are only pointers.
    Now, is there a reason you're using C instead of C++?
    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.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Haha that is an excellent question!

    I don't know is the honest answer. The game has been running for decades which is the most likely reason - I inherited it and have been working with it for the last 3 years and this kind of thing is always getting me.

    Is it a difficult thing to do to move it to C++? There is a /lot/ of code...

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    C++ is backwards compatible. Some code will break (such as conversions from void*), but mostly it should be fine.
    It should allow you to use C++ features in the future to interact with the legacy C code.
    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 2008
    Location
    TX
    Posts
    2,047
    Don't confuse pointer usage, like int *, with obtaining the address of an object using & operator.
    Code:
    int *ip;   /* ip is a variable of type pointer to integer */
    int i;     /* i is a variable of type integer */
    ip = &i;   /* now ip points to i */

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    The OP was not confusing the address-of operator with pointers. He/she was confusing the C++ syntax of passing by reference with C.
    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.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Ok excellent thank you... that's very interesting.... one more question then, if you would - I am not a *nix user so the environment is much as it was and I use a makefile to compile my application.

    Presumably to upgrade to C++ all I really need to do is compile the code with a C++ compiler instead of a C one and fix any errors that arise.

    My current makefile looks like this

    Code:
    CC      = gcc
    TARGET  = dw 
    DEBUG   = -g 
    DEFINES = -DNOCRYPT 
    C_FLAGS = -O -Wall $(DEBUG) $(DEFINES)
    L_FLAGS = $(DEBUG)
    
    O_FILES = act_comm.o act_info.o act_move.o act_obj.o act_wiz.o bit.o board.o clan.o cledit.o comm.o const.o db.o dice.o fight.o file.o handler.o hedit.o hunt.o interp.o magic.o mob_prog.o olc.o quest.o sedit.o spec_mob.o spec_obj.o ssm.o string.o trap.o update.o
    
    $(TARGET): $(O_FILES)
    	$(CC) $(L_FLAGS) -o $@ $^
    
    %.o: %.c merc.h bit.h
    	$(CC) $(C_FLAGS) -c $<
    
    clean:
    	rm -f *.o $(TARGET)
    There is likely to be a default C++ compiler on the server - it's used for lots of stuff - do you have any idea what it may be called and what would need to change in the makefile to compile?

    Thank you so much for your advice!

    Dom

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    IBTD, simply because how the function header is coded
    Code:
    int get_one_hit_damage(CHAR_DATA *ch, CHAR_DATA *victim, int &dt, int wpn)
    tho' apparently the op does fine with the CHAR_DATA ones!
    Last edited by itCbitC; 10-07-2010 at 09:46 AM.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Quote Originally Posted by dominicshaw View Post
    Ok excellent thank you... that's very interesting.... one more question then, if you would - I am not a *nix user so the environment is much as it was and I use a makefile to compile my application.

    Presumably to upgrade to C++ all I really need to do is compile the code with a C++ compiler instead of a C one and fix any errors that arise.

    My current makefile looks like this

    There is likely to be a default C++ compiler on the server - it's used for lots of stuff - do you have any idea what it may be called and what would need to change in the makefile to compile?

    Thank you so much for your advice!

    Dom
    It would probably be called g++ since you seem to be using GCC.

    Quote Originally Posted by itCbitC View Post
    IBTD, simply because how the function header is coded
    Code:
    int get_one_hit_damage(CHAR_DATA *ch, CHAR_DATA *victim, int &dt, int wpn)
    Exactly. C++ syntax.
    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.

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    No she is quite right I'm afraid - I read that putting & in front of it would make it a reference parameter on what I now realise to have been a C++ website.

    I did experiment with getting a pointer to the dt parameter and passing that in as a pointer but because of everything else going on in the page (other calls using a constant to get_one_hit_damage) - i couldn't get anywhere...

    I did something like

    Code:
    int *p_dt;
    p_dt = &dt;
    int dam = get_one_hit_dam(ch, victim, p_dt)
    but i got nowhere.

    how do you do this kind of thing in C?

    Basically the two functions used to do the same thing but one just returned a number and the other actually did something with the number - I'm jsut trying to tidy it up so I dont have to update both functions for every change...

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Code:
    void foo(int* p)
    {
        *p = 10;
    }
    
    int main()
    {
        int x;
        foo(&x);
        // x == 10.
    }
    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.

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Fair enough - right - well I want to move with the times - or a few decades behind them at least so I am going to go ahead and try to upgrade. I have started and got over 3000 errors which is a great beginning.

    I've got that down a lot already but I am slightly (very) confused by something which I think you have already hinted at - something to do with void* - a generic pointer, as I understand it.

    This is an area of the application I've skillfully avoided for the last 3 years, and it is to do with memory allocation. The problem is in this code which, I take it, allocates a load of memory for a load of objects (structures) to load into memory...

    Code:
        OBJ_DATA **prgpstrShow;
        int *prgnShow;
        //get count
        prgpstrShow = alloc_mem(count * sizeof(OBJ_DATA *));
        prgnShow = alloc_mem(count * sizeof(int));
    ...and...
    Code:
    void *alloc_mem(int sMem)
    {
        void *pMem;
        int iList;
    
        for (iList = 0; iList < MAX_MEM_LIST; iList++)
        {
            if (sMem <= rgSizeList[iList])
                break;
        }
    
        if (iList == MAX_MEM_LIST)
        {
            bug("Alloc_mem: size %d too large.", sMem);
            exit(1);
        }
    
        if (!rgFreeList[iList])
        {
            pMem    = alloc_perm(rgSizeList[iList]);
        }
    
        else
        {
            pMem              = rgFreeList[iList];
            rgFreeList[iList] = * ((void **)rgFreeList[iList]);
        }
    
        return pMem;
    }
    I know this is now completely separate but if you could point me in the right direction it would save me a lot of time! I think it'll be the main hurdle as I can see a lot of these types of errors on the horizon.

    Thanks,

    Dom

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Change
    Code:
        //get count
        prgpstrShow = alloc_mem(count * sizeof(OBJ_DATA *));
        prgnShow = alloc_mem(count * sizeof(int));
    to
    Code:
        //get count
        prgpstrShow = reinterpret_cast<OBJ_DATA **>( alloc_mem(count * sizeof(OBJ_DATA *)) );
        prgnShow = reinterpret_cast<int*>( alloc_mem(count * sizeof(int)) );
    C++ does not allow implicit conversion from void* to T*, where T is any type except void. You must do an explicit cast.
    In C++ (not C code compiled as C++), casts are usually avoided due to C++'s type system which typically works on the fact that the type can be known at all times.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined reference
    By TheEngineer in forum C Programming
    Replies: 17
    Last Post: 08-12-2009, 10:34 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. reference parameters
    By LowLife in forum C Programming
    Replies: 8
    Last Post: 01-25-2006, 10:50 AM
  4. Reference parameters and calculating change
    By Cstudent2121 in forum C Programming
    Replies: 6
    Last Post: 11-04-2005, 02:19 PM
  5. Passing parameters by reference
    By aker_y3k in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2003, 05:46 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21