what type is unsigned size
unsigned
is short for unsigned int
.
Why would you add an int to an array of ints?
Pointers and arrays are not the same thing. The code you've shown is using pointers, not arrays. After the int * b = y + size;
line, b
is a pointer that points to the entry size
entries from where y
is pointing. For example, if size
were 2
, b
would be pointing to the third entry. ASCII-art:
+---------+
| entry 0 |<--- `y` points here
| entry 1 |
| entry 2 |<--- `b` points here if `size` is `2`
| entry 3 |
| entry 4 |
+---------+
I have the most difficulty understanding the following.
while (1) if (*(--b)==z){y[0] = tmp; return b - y;};
The loop looks at the entries in the memory pointed to by y
starting with the entry before the one identified by size
. If the entry is ==
to z
, it sets y[0]
to tmp
and returns the index at which the entry was found (by using pointer arithmetic, b - y
returns the number of entries between where b
is pointing and the beginning of y
. Since --b
decrements the pointer, the loop works backward through the memory.
are the following 3 lines ever even reached?
No. The return
will exit the function when the first matching entry is found, which may be at the beginning (as y[0]
is set to z
early on). As Ted Hoff points out in the comments, though, the loop will start and continue past the beginning (where y
is pointing) if size
is 0
on entry, which would probably eventually cause the program to fail with a memory access violation.