After running the following C program with Valgrind, it says the packets.payload
variable is uninitialised. But I cannot figure out why since I've already put "1234567890" in packets.payload
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct packet {
int payload_length;
char payload[10];
} packet_t;
int main () {
packet_t packet;
strncpy(packet.payload, "1234567890", 10);
puts(packet.payload);
}
A pointer to a string (a sequence of characters terminated by null-character) must be passed to puts()
.
Your packet.payload
doesn't contain terminating null-character, so you cannot use it for puts()
.
You can use printf()
with specifying length to print to print that:
printf("%.*s\n", 10, packet.payload);