1

I have a section of code that I am unsure of what it is doing.

static int bufindw = 0;
static char buffer[4][96];
static char *strchr_pointer;
int result;

result = (int)strtod(&buffer[bufindw][strchr_pointer - buffer[bufindw] + 1], NULL)

The part where I don't understand what is happening is strchr_pointer - buffer[bufindw] + 1. strchr_pointer and buffer[bufindw] will be strings. It gives the result I expect it to give because I know in the end it needs the correct index value in buffer[][] and in addition the number 1 is being added to it as well. How can a string minus a string?

1
1

Let's break it down logically for you.

You have 4 strings, each capable of containing 96 characters:

static char buffer[4][96];

That is a block of RAM that covers 4*96=384 bytes. Let's say that block of ram starts at address 0x100.

result = (int)strtod(&buffer[bufindw][strchr_pointer - buffer[bufindw] + 1], NULL)

The phrase buffer[x] returns the address of the string number x in your array of strings. It's the same as &buffer[x][0] - the address of the first character in string x.

You also have a pointer strchr_pointer which contains something - what that is I don't know because you haven't told us. Chances are it's the result of a strchr() call, which:

returns a pointer to the first occurrence of the character c in the string s.

So you have looked for a character in a string and found that it's at a specific address. Let's say that's address 0x111 for the sake of argument.

Ok, so if we take bufindw as 0 for the first string in your array, and work from the inside out, we have:

  • buffer[bufindw] = 0x100
  • strchr_pointer = 0x111
    • strchr_pointer - buffer[bufindw] = 0x111 - 0x100 = 0x11
    • strchr_pointer - buffer[bufindw] + 1 = 0x100 - 0x111 + 1 = 0x12
  • Replace that into the function's parameters:
    • &buffer[bufindw][strchr_pointer - buffer[bufindw] + 1] = &buffer[bufindw][0x12]

So you have the address of the 0x12'th (18th) character (or character number 16 since they count from 0 not 1), which is address 0x112.

That address is then passed to strtod() to convert it into a double-precision number: strtod(0x112, NULL);

That value is then cast to an integer:

(int)strtod(0x112, NULL);

So the decimal portion is discarded (not rounded, chopped off).

Say your string contains: "This is the value:23.83589743" and is located at 0x100.

You use strchr() to find the first occurrence of ':', and it finds it at address 0x111, which is the 18th character of the string, so 0x100 + 17 = 256 + 17 = 273 = 0x111. You subtract the start of the string and add one to it, so you end up with 18, which is the start of the numeric value (the 18th character). The string from that point on contains "23.83589743". That is then passed to strtod() and cast to an integer, and so is truncated to 23.

It's not surprising you're confused, the whole thing can be re-written much simpler:

strchr_pointer = strchr(buffer[bufdindw], ':');
result = (int)strtod(strchr_pointer, NULL);

All that manipulating of the buffer address is like doing "x = 6 + y - 3 + 4 - 7" - completely pointless, since you might just as well write "x = y".

1
  • And yes, strchr_pointer is the result of a strchr() call. Aug 27 '15 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.