Is there a way for me to take a memory address and advance it a certain amount that is stored in a variable? And what would that variable type have to be?
For example, in the following code I’d like to first look at data + 0, and then for each step after that look at data + sent. If I’m looking at this correctly, sent is stored as bytes, and data is a memory address.
bool sendAll(int socket, const void *data, ssize_t size) {
ssize_t sent = 0;
ssize_t just_sent;
while (sent < size) {
just_sent = send(socket, data + sent, size - sent, 0);
if (just_sent < 0) {
return false;
}
sent += just_sent;
}
return true;
}
That’s what
char*will do. Pointer math, when the pointer has typeT*, always works on increments ofsizeof (T). Andsizeof (char) == 1by definition.So try: