Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this pointer-heavy C code do?

Tags:

c

pointers

Could someone explain to me what should two following lines do:

s.httpheaderline[s.httpheaderlineptr] = *(char *)uip_appdata;
++((char *)uip_appdata);

This is taken from uIP code for microcontrollers.

s - structure
httpheaderline - http packet presented as a string
httpheadrlineptr - integer value
uip_appdata - received ethernet packet (string)

If some more info is needed please let me know.

BTW. Eclipse is reporting an error on the second line with message Invalid lvalue in increment so I'm trying to figure out how to solve this.

like image 853
justRadojko Avatar asked Jun 11 '26 09:06

justRadojko


1 Answers

The intention behind first line is to grab a character pointed to by uip_appdata:

*(char *)uip_appdata

casts uip_appdata to char*, then dereferences it, thus taking the first character.

The second line tries to increment uip_appdata. The trouble is, it does not do it properly, because results of a cast cannot be incremented "in place".

Here one way of doing it that works:

char *tmp = uip_appdata;
uip_appdata = ++tmp;

With this code fragment the compiler can take care of converting between pointer types in cases when the platform requires it.

Here is a demo of this concept on ideone.

like image 109
Sergey Kalinichenko Avatar answered Jun 13 '26 02:06

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!