Thread: passing variables by reference

  1. #1
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    passing variables by reference

    I'm writing a winsock function that call's recv(). Since the recv() function directly changes the variable directly, and uses the return value for something else, I need the user of the function to be able to create a variable, pass it to my function, then use the variable after it has been changed. However, I'm not sure how I could do this. What I have so far is similar to this:

    Code:
    INT MyFunc(CHAR *&mybuff)
    {
    INT iRecv = recv(socket, mybuff, strlen(mybuff), 0);
    }
    There's more code that makes the function more intricate, but that's the only part where I need to send a pointer to a buffer to the recv() function. Am I passing the variable to my function the right way? Basically, what I'm aiming for is so I can do something like this:

    Code:
    char *buff="";
    if (MyFunc(buff) == 0)
    {
    printf(buff); //display the changed data;
    }
    Thanks for any help
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    Code:
    INT MyFunc(CHAR * mybuff) //Without the ampersand
    {
    INT iRecv = recv(socket, mybuff, strlen(mybuff), 0);
    }

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Thanks for the answers.

    vVv: The function I'm using involves a little more than just that. I was just having problems with changing the buffers around and such. Thanks for the help.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing struct vs. reference
    By reakinator in forum C Programming
    Replies: 4
    Last Post: 06-14-2008, 10:11 PM
  2. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  3. passing struct to function by reference
    By ironfistchamp in forum C++ Programming
    Replies: 11
    Last Post: 07-03-2006, 02:05 PM
  4. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM
  5. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 04:54 PM