help "wrapping" data types in char array

This is a discussion on help "wrapping" data types in char array within the C Programming forums, part of the General Programming Boards category; Hi, I've been working on a problem of mine, and I'm currently tearing my hair out...... basically what I'm trying ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    4

    help "wrapping" data types in char array

    Hi, I've been working on a problem of mine, and I'm currently tearing my hair out...... basically what I'm trying to do is implement a simple client / server protocol in C. My problem lies in the fact that I have a char* (char array) which I use for my buffer to pass to a C function to send it across the socket to the server/client. What I want to do is to fill this char array with structs OR other data types such as floats. Now I thought this would be possible by filling every 4th char in the array with a float (a float is 4 bytes), and then casting it at the other end, but apparently this isn't working. Can anyone help me before I start stomping on my computer?
    Many Thanks
    Tom

  2. #2
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    If that is what you have done then it shouldn't be any problem. Did you cast it as a float or a float pointer that you later dereferenced? I can see the possibility of the compiler trying to convert it from an int to a float when casting.
    But I can't tell for sure if you're not posting any code.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    4

    array fiddling

    hmmm, right something very odd is going on... i'll post the important part of my code:
    Code:
    typedef struct enemyType
    {
      char header;
      char playerNo;
      float eyex, eyey, eyez;
    } enemyType;
    
    enemyType enemy[MAX_PLAYERS];
    char noPlayers;
    
    
    void writeStuff(char *writeBuffer) {
      char playerCount;
      int currentByte;
      
      writeBuffer[0] = (char) 2;
      currentByte = 1;
      for (playerCount = 0; playerCount < noPlayers; playerCount++) {
        writeBuffer[currentByte++] = playerCount;
        writeBuffer[currentByte]   = enemy[playerCount].eyex;
        currentByte += 4;
        writeBuffer[currentByte]   = enemy[playerCount].eyey;
        currentByte += 4;
        writeBuffer[currentByte]   = enemy[playerCount].eyez;
        currentByte += 4;
      }
    }
    now it seems in the function which i call this function, i can read the values of writeBuffer[2 / 6 / 10] using the %f notation in printf, and I will get a print value of 0.0000000; however trying this using the %d notation will get the correct truncated, increased by a power of ten!?!? i am really not sure what's going on. Here's I'll put in a initialisation function for you to try
    Code:
    void testInit(void) {
      noPlayers = 2;
      enemy[0].eyex = 1.0;
      enemy[0].eyey = 1.0;
      enemy[0].eyez = 65.34;
      enemy[0].header = 2;
      enemy[0].playerNo = 0;
      
      enemy[1].eyex = 2.0;
      enemy[1].eyey = 2.0;
      enemy[1].eyez = 2.0;
      enemy[0].header = 2;
      enemy[0].playerNo = 1;
      printf("Test data plugged in\n");
    }

  4. #4
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Code:
    writeBuffer[currentByte]   = enemy[playerCount].eyex;
    This should cause a problem, the float is casted to a char. Try outputting the raw data of what you're inserting to the buffer and then outputting the raw data of the buffer and see how it differs.
    I think that
    Code:
    *((float*) &writeBuffer[currentByte])   = enemy[playerCount].eyex;
    would do the trick

    Code:
    currentByte += 4;
    Don't do this, even if float always will be 4 bytes it's better to use sizeof(float) instead to eliminate the use of magic numbers.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by hampycalc
    basically what I'm trying to do is implement a simple client / server protocol in C. My problem lies in the fact that I have a char* (char array) which I use for my buffer to pass to a C function to send it across the socket to the server/client.
    Wouldn't you just want to point to the data and send it?
    Code:
    int sent = send(mysocket, (char*)enemy, noPlayers * sizeof *enemy, 0);
    /* ... */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    4

    Re:

    I don't think that would work would it? Passing the pointer to the array of structures will just be passing a pointer to a pointer which wouldn't transfer the actual data across the network. Correct me if I'm wrong, but that's only my thinking...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  4. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 04:16 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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