So I have the following:
char * buffer = (char *) malloc(2*80 + 4);
uint32_t networkedRin = htonl(student->rin);
printf("RIN %u\n", student->rin);
//Clear all memory and copy the first last and rin into the new buffer
memset(buffer, '\0', 164);
memcpy(buffer, student->firstname, 80);
memcpy(buffer+80, student->lastname, 80);
memcpy(buffer+160, &networkedRin, 4);
printf("Networked rin: %u\n", networkedRin);
printf("L5: %s %s %u\n", buffer, buffer+80, buffer+160);
and I am very confused because when I do the printing at the end the expected value is the same as Networked rin value printed earlier but it is not, in fact its garbage because it is constantly changing with every run. Now I know I should be using sizeof or whatever but we were told to just use the hard coded value of 4 for uint32 and what not. I’m stuck as to why I’m getting garbage when trying to display the networked rin number from the buffer.
For example I am receiving this output after two runs of the program
RIN 60
Networked rin: 1006632960
L5: loller cats 16375984
RIN 60
Networked rin: 1006632960
L5: loller cats 10260656
I’m sure its something simple but I just can’t see it.
buffer+160is not the value you are looking for.buffer+160is a a pointer to yourint, not yourintitself.You must type-cast and de-reference the pointer to see the value you want.