|
|
|
|
|
by trealira
712 days ago
|
|
> Also.. why is `node + 1' even a valid comparison here? In C and C++, when you add 1 to a pointer, you actually make it point to the next object of that size in memory, e.g. if I have a pointer to a 4-byte integer at address 0x8000, incrementing it will make it point to address 0x8004. Because arrays are contiguous chunks of memory, you can use this to iterate through arrays. int array[50];
int *p = &array[0]; // Take the address of the first array element
int sum = 0;
for (int i = 50; i != 0; i--) {
sum += *p;
p++; // Point to the next element
}
After this loop, p will be equal to &array[50], which is one past the last element of the array, because the loop will have run 50 times, and p is incremented once per loop.What OP did is allocate an array of linked list nodes, and test to see if the next linked list node was actually just the next array element. |
|