problems passing pointers

This is a discussion on problems passing pointers within the C Programming forums, part of the General Programming Boards category; OK, I'm quite confused as to why my passing by pointers isnt working properly... Code: int main() { int type; ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    7

    problems passing pointers

    OK, I'm quite confused as to why my passing by pointers isnt working properly...

    Code:
    int main() {
    int type;
    char input[MAX_MSG];
    char username[MAX_ULEN];
    char message[MAX_MLEN];
    
    ..some code..
    
    type=0;
    TeardownPacket(&input, &type, &username, &message);
    printf("Type after TP: %i\n", type);
    ..some code...
    return 0;
    }
    
    /*teardown packet def (contained in a different .c file, included above)*/
    void TeardownPacket(char *input, int *type, char *username, char *message) {
    
    /*take first byte of input data and put it in type*/
    *(type) = (int) *(input);
    printf("type: %i\n", *type);
    
    ..some more code..
    
    return;
    }
    So I pass all those variables pointers to the function TeardownPacket. I change them successfully in TeardownPacket (I've printf'd them all and the values are correct corresponding to the data in the input string). But when TeardownPacket returns to the main function, the type variables's value that was applied in the TeardownPacket routine doesnt stay! The variable was changed in TP, but its like it went back to its original value after TP returned. I just dont get it....

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    520
    remove the address operator (&) they're already pointers. if you add that, you're passing the address of the pointer, not the address of the memory it points to. (which is what you want)

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    520
    Obviously you need to leave the address operator on the type variable though, as its an int.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    > TeardownPacket(&input, &type, &username, &message);
    You don't need the & for the char arrays
    TeardownPacket(input, &type, username, message);

    Did you actually prototype the function before trying to call it?

    On the face of it, you're doing the right sort of things. It's better if you can post an example complete program which goes wrong for you.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    7
    Here is the code for TeardownPacket, located in message.c

    Code:
    void TeardownPacket(char *input, int *type, char *username, char *message) {
      /*given the packet as input, break it down and return the appropriate values for each parameter*/
      int ulen=0;
      int mlen=0;
    
      /*type*/
      *type = (int) *input;
      printf("Msg received, type %i\n",*type);
      
      /*username*/
      ulen = (int) *(input+3);
      memset(username,0,MAX_MLEN);
      if (ulen>0) {
        /*username specified*/
        strncpy(username, input+4, ulen);    
        }
    
      /*message*/
      /*second verse, same as the first*/
      mlen = (int) *(input+4+ulen);
      memset(message,0,MAX_MLEN);
      if (mlen>0) { 
        /*message specified*/    
        strncpy(message, input+5+ulen, mlen);
        }
      }
    Here are some more sections of the code in main...

    Code:
    #include "message.c"
     ... some code ...
    
    int main()
    {
      char servername[SHTSTR];
    
      int lsocket;
      
      pthread_t serverth;
    
      struct sockaddr_in mysock;
      socklen_t mysockLen;
      struct sockaddr_in remoteAddr;
      socklen_t remoteAddrLen;
      struct pollfd skpoll;
      
      struct hostent *hostcfg;
      char myusername[MAX_ULEN];
      
      int type;
      char username[MAX_ULEN];
      char message[MAX_MLEN];
      char outputbuf[MAX_MSG];
      char inputbuf[MAX_MSG];
      
     ... some more code
    
        recvfrom(lsocket, &inputbuf, MAX_MSG, 0, (struct sockaddr *) &remoteAddr, &remoteAddrLen);
        type=0;
        TeardownPacket(inputbuf, &type, username, message);
        printf("Type after TP: %i\n", type);
        if (type==TYP_JOIN) {
          /*connect successful, exit username input loop*/
          break;
          }
        else if (type==TYP_MSG) {
          /*unsuccessful join, display message*/
          printf("%s\n", message);
          }
        else {
          /*unknown response*/
          printf("Server has responded with bad data, exiting program!\n");
          exit(0);
          }
        }
    The output I get in my shell is...
    "Msg received, type 1
    Type after TP: 0"

    I didnt think I need to prototype it since its located in a different file that is included.

    Maybe it has something to do with trying to cast a char onto an int? Would the different sizes make a problem? The value of the first byte of the input variable is 0x01, and thats why I get the 1 in the first line of output, but it goes back to zero when the function is returned.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #include "message.c"
    *cringe*

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    7
    Why is that a cringe? That file is shared between the client and server applications. So the packet handling routines are shared, so if a change is made to the packet definitions, I just change it in one place...

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't include .c files. It's bad form. You include header files, which reference functions inside .c files.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    Create a message.h file, which you include in client.c and server.c

    Then compile
    gcc -o client client.c message.c
    gcc -o server server.c message.c

    > char username[MAX_ULEN];
    ...
    > memset(username,0,MAX_MLEN);
    My guess is MAX_MLEN is larger than MAX_ULEN
    So when you clear the array, you're clearing more than you bargained for.
    Given the order of declarations, it would seem that type immediately follows username (for your compiler), so whatever you assign to type gets immediately blanked again by the memset

    > printf("Msg received, type %i\n",*type);
    Put this after the memset to see.

    It would be better if you passed some lengths to the function
    TeardownPacket(inputbuf, sizeof inputbuf, &type, username, sizeof username, message, sizeof message );
    The function itself then has all the information to hand in the input parameters, rather than having to use magic numbers (incorrectly it seems)
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    7
    Whoops!

    I guess I ought to go over my code better next time.... stupid copy/paste....

    That looks like it solved my problem (changing MAX_MLEN to MAX_ULEN). I've got other problems now but hopefully those wont be too difficult to solve...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  2. passing pointers to other files
    By AmazingRando in forum C Programming
    Replies: 6
    Last Post: 02-02-2005, 12:05 AM
  3. Passing a 2d Array of pointers to a Function
    By miclus in forum C Programming
    Replies: 6
    Last Post: 09-11-2004, 08:34 AM
  4. Problems with pointers
    By rhildebrand in forum C Programming
    Replies: 2
    Last Post: 10-29-2003, 07:57 PM
  5. Passing Pointers By Reference
    By hern in forum C Programming
    Replies: 15
    Last Post: 07-29-2003, 12:43 PM

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