C - Buffer size reduced after being passed to another function -


I am working on an assignment in C and when I found an error, I started something, understand Not come

* Hostname), int port) {// to store in buffer variable char * msg = "Hi, I'm a message"; // Stellen (msg) == 17 uint16_t sender_id = hayton (1); // size (sender_id) == 2 uint16_t packet_size = htons (sizeof (packet_size) + sizeof (sender_id) + strlen (msg)); // size (packet_size) == 2 // buffer four buff [100]; // Copy everything Memc (& amp; Buff, & Sender_ID, Size (Sender_ID)); Memcpy (& amp; buf + sizeof (sender_id), & amp; packet_size, sizeof (packet_size)); Memcpy (& amp; buf + sizeof (sender_id) + sizeof (packet_size), & amp; msg, strlen (msg)); Printf ("l1_connect - sizeof (buf):% d \ n", (integer) size (buf)); // == 21 // buffer to another function int bytes_sent = l1_send (1, buf, sizeof (buf)); Return bytes_sent; }

At the end of this function, buffer size is 21. After passing it to another function, it is clearly 8.

  Int l1_send (int device, const char * buf, int length) {printf ("l1_send - sizeof buf:% d \ n", (int) sizeof (buf)); // == 8 printf ("l1_send - sizeof and buf:% d \ n", (integer) size (& amp; buf)); // == 8 printf ("l1_send - Sizeof * buf:% d \ n", (int) size (* buf)); // == 1 printf ("l1_send - sizeof and buf [0]:% d \ n", (integer) size (& amp; buf [0])); // == 8 return1; // for now}  

Will someone tell me what am I missing?

I am trying to send the buffer to the UDP socket, and then all three of these re-extract the variable sender_id and packet_size work fine, But the messages give errors or blank-values, and I'm assuming this is due to the buffer size problem.

Thank you!

Edit: Greatly appreciated, guys! I realized why length to l1_send ()

sizeof is an operator that returns the size of the variable type, not the size of the allocated buffer.

B64-bit machine:

  // l1_send sizeof (buf) - & gt; Size of four * - & gt; 8 bytes size (& buf) - & gt; Size size ** - & gt; 8 bytes size (* buf) - & gt; Size of four - & gt; 1 byte size (& amp; buf [0]) - & gt; Size of four * - & gt; 8 bytes / l1_connect sizeof (buf) - & gt; Size of four [100] - & gt; 100 bytes (this is what this should be)  

Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -