# Thread: Why would they use this segment instead of this one?

1. ## Why would they use this segment instead of this one?

Why use this segment

Code:
```#include <iostream>
using namespace std;

int main ()
{
int numbers[5];
int * p;
p = numbers;  *p = 10;
p++;  *p = 20;
p = &numbers[2];  *p = 30;
p = numbers + 3;  *p = 40;
p = numbers;  *(p+4) = 50;
for (int n=0; n<5; n++)
cout << numbers[n] << ", ";
return 0;
}```

Code:
```#include <iostream>
using namespace std;

int main ()
{
int a[5];
int *b;
b = a;  *b = 10;
b = a;  *(b+1) = 20;
b = a;  *(b+2) = 30;
b = a;  *(b+3) = 40;
b = a;  *(b+4) = 50;
for (int n=0; n<6; n++)
cout << a[n] << ", ";
return 0;
}```
Does the first, seemingly more complicated block do something more than the second block? Even though both print the same thing?

NOTE in the first block, p is equivalent to b in the second block. and "numbers" is equivalent to a.

2. Obviously you would do it for educational purposes, to demonstrate various ways you can access an offset from a pointer.

By the way, the second version iterates one element too many on output.

In the real world, what you would do is
Code:
`int numbers[] = { 10, 20, 30, 40, 50  };`
and leave all the dangerous pointer stuff out.

3. ## Too much needless instructions

Simply because they are slow, and take too many lines to do something pretty simple and easy:

Code:
```int main()
{
int a[5], *b = a;
for(int i = 0; i < 5;)
cout << (*(b+i) = (++i * 0xA)) << (char)0x20;
return 0;
}```
Also, why not simply use a pointer from the start?

Code:
```int main()
{
int *a = new int[5];
for(int i = 0; i < 5;)
cout << (*(a+i) = ((i++ + 0x14 / 0x2 + i) * 0x5) - 0x28) << (char)0x20;
return 0;
}```

you could also do it using inline asm, then it would get even more ureadable, if that is your goal.

As you can see, there is a lot of ways of making something more and more unreadable. Use your criativity.

4. Originally Posted by Dante Wingates
Simply because they are slow, and take too many lines to do something pretty simple and easy:

Code:
`  cout << (*(b+i) = (++i * 0xA)) << (char)0x20;`
This line causes undefined behaviour because some compilers will evaluate 'i' before the assigment and others will evaluate ++i after the assignment first