# Thread: N00b to C++ here, have some general questions.

1. Is there a way I can sort 3 integers using only the ternary operator and the usual <, <=, >, >=, ==, != ? My prof wants it for 4 integers, but I can't seem to figure out how to compare 3 digits with just the ternary operator and the above comparisons (I'm not supposed to use if else at all). Any help (I'm hoping that if I see a 3 integer comparison, I can figure out a 4 integer comparison)? It seems like I can't compare more than 2 values in one ternary operator. For example, the following code doesn't seem to work:

Code:
` (a > b > c > d) ? (cout<<a <<b <<c <<d) : (b > c > d > a)? (cout<< b <<c <<d <<a) : (c > d > a > b) ? (cout<<c <<d <<a <<b) : (d > a > b > c)? (cout <<d <<a <<b <<c) : (cout << "=[" <<endl);`
Does this mean that I can't embed one ternary operator into another? What are its restrictions? I'm totally lost here...

sorry for messing up the page =[

2. >> Does this mean that I can't embed one ternary operator into another?
You can, but you have to have the cout outside of it.

3. (a > b > c > d)
This doesn't do what you think it does. A single operand cannot be operand to more than one operator.

4. Originally Posted by CornedBee
This doesn't do what you think it does. A single operand cannot be operand to more than one operator.
OH!

should I have to write it like so:

Code:
`(a>b),(c>d)`
@Daved: You mean cout outside of the ternary operator? Can you give me an example?

5. Originally Posted by Bash
OH!

should I have to write it like so:

Code:
`(a>b),(c>d)`
@Daved: You mean cout outside of the ternary operator? Can you give me an example?
Something like this:
Code:
`cout << (a > b)?b:a;`
and (a >b), (c < d) doesn't quite do what you want, unless you think that comparing (a>b), throwing away that result and using the result of (c < d) is what you want - and I don't think that's what you expected it to do. You need to use && or || when combining comparisons.

--
Mats

6. a < b && b < c && c < d

7. I see =) Thanks for all the input.

I've been reading about pointers, and there's something that has been bothering me and driving me nuts. What exactly does a pointer point to? To the address in the memory block, or to the value inside the address in the memory block? And I am totally lost when it comes to managing the dynamic memory space. I've been reading online and I start off fine, but when I get to where it explains what it points to, I get really lost. Anything I can do to make me understand what exactly is going on? I tried editing a sample code and fiddling with it to check what it does but I still can't figure it out...

And what's teh difference between these?

Code:
```my_pointer = &my_int;
*my_pointer = &my_int;
*my_pointer = my_int;```
I have a feeling that the third one is totally erroneous, one of them gives me the value inside the address in the memory block and the other one gives the address of the memory block. But I've no idea...

8. To the address in the memory block, or to the value inside the address in the memory block?
It contains the address, thus it points to the value. But pointing to is a very abstract concept anyway.

And what's teh difference between these?
Depends on the types involved. If my_pointer is int*, that is, a pointer to int, then the first makes my_pointer point to my_int, the second is invalid, and the third sets the value of whatever my_pointer points to to the value of my_int.
If my_pointer is int**, that is, a pointer to a pointer to int (yep, that's possible), then the first and the third are invalid and the second sets the value of what my_pointer points to (which is a pointer to int, int*) to the address of my_int, i.e. it makes the pointer that my_pointer points to point to my_int.

9. Originally Posted by Bash
I see =) Thanks for all the input.

I've been reading about pointers, and there's something that has been bothering me and driving me nuts. What exactly does a pointer point to? To the address in the memory block, or to the value inside the address in the memory block? And I am totally lost when it comes to managing the dynamic memory space. I've been reading online and I start off fine, but when I get to where it explains what it points to, I get really lost. Anything I can do to make me understand what exactly is going on? I tried editing a sample code and fiddling with it to check what it does but I still can't figure it out...

And what's teh difference between these?

Code:
```my_pointer = &my_int;
*my_pointer = &my_int;  Error!!
*my_pointer = my_int;```
I have a feeling that the last one is totally erroneous...
Only the one marked Error in red is incorrect.

The first one says that "I have a pointer (my_pointer), it has the value of the address of my_int".
The third one says, set the value pointed to by my_pointer to the value of my_int.

Briefly: Pointers have two properties: Their own value, and the value of the data they point to. The former is the variable content itself, the latter is *pointer - so content of the memory at the address that is the pointer value.

If we do (note this code WILL NOT RUN - it is to explain things):
Code:
```int *my_pointer = (int *)1000;

*my_pointer = 7;   // 1
my_pointer = (int *)1020;
*my_pointer = 8;```
After this code has run [if it could run!] address 1000 in the memory would have the value 7, and address 1020 would have the value 8.

[(Do not worry if you don't understand this bit, it is just in case you want to understand more - at some point you will probably understand why this is and how it works) The reason it won't run is that you can't just assume that some random memory location, like 1000, is available to write to - it most likely won't be a valid memory address, and even if it is a valid address, it may be used for something already, so you'd be overwriting some other data. In a modern system, "low" addresses like 0 and a few thousand upwards will be permamently marked as "not present" just to catch people using a 0 pointer - 0 (or NULL) pointers should not be used, as that's an indication of an invalid pointer]

--
Mats

10. Originally Posted by CornedBee
Depends on the types involved. If my_pointer is int*, that is, a pointer to int, then the first makes my_pointer point to my_int, the second is invalid, and the third sets the value of whatever my_pointer points to to the value of my_int.
I know that the "*" is a dereferencing thingy, and this is how I think about it:

A pointer generally points to an address. But I see an asterisk before it, which means it's not pointing to teh address anymore, but to whatever is inside the address.
Is that a wrong way to think about it?

And for the first one:

There is teh "&" symbol, so it's the address of whatever that follows, and that's being stored inside the pointer
That fine too?

And out of curiosity, is there a difference in teh following:

Code:
```&Address and Address&
*Pointer and Pointer*```
Edit:
I think I confirmed that I'm not wrong in my way of trying to memorize how pointers point when I studied matsp's explanation. Thanks for putting a small bit of confidence and self-assurance in me

Edit 2: I still would like to know the difference in whatever I've written in the code box, lol. Please?

11. Originally Posted by Bash
I know that the "*" is a dereferencing thingy, and this is how I think about it:

Is that a wrong way to think about it?

And for the first one:

That fine too?

And out of curiosity, is there a difference in teh following:

Code:
```&Address and Address&
*Pointer and Pointer*```
It's not really "Address&" and "&Address". You should look at it as:
Variable& and &Variable. And they are (sort of) related but not quite the same.

& before something means "Take the address of the data"
& after somethign means "This is a reference". A reference is "pass this as a pointer but hide it" - or another way to put it "make this an alias of the variable specified". E.g:
Code:
```int b = 0;
int &a = b;  // a references b.

a = 7;   // b is also 7 now.```

*Pointer is correct - Pointer * is not - it is Type *, e.g. int *, or char *, or mytype *.

* in front means "Use the next stuff as a pointer"
* after something means "This points to what's in front", e.g. an integer, a char or a data structure.

I hope this clarifies things.

--
Mats

12. Originally Posted by matsp
It's not really "Address&" and "&Address". You should look at it as:
Variable& and &Variable. And they are (sort of) related but not quite the same.

& before something means "Take the address of the data"
& after somethign means "This is a reference". A reference is "pass this as a pointer but hide it" - or another way to put it "make this an alias of the variable specified". E.g:
Code:
```int b = 0;
int &a = b;  // a references b.

a = 7;   // b is also 7 now.```
I hope this clarifies things.

--
Mats
It does indeed clarify a lot. But I still have a couple more questions...

1) Shouldn't
Code:
`int &a = b;  // a references b.`
be
Code:
`int a& = b;  // a references b.`
2) If I swap the address of the two variables would the values inside the address be swapped too?

13. Originally Posted by Bash
Is that a wrong way to think about it?
Not exactly wrong, but I'd call it misleading.
The pointer always points to the variable and its value (the distinction between the two doesn't matter at this time - it will matter when you encounter the terms "r-value" and "l-value"). It never points to the address (it contains the address - that's what makes it a pointer).
Dereferencing doesn't change the pointer, it changes the expression. Hmm ... example without pointer:
Code:
```int x = 5;
std::cout << -x```
"A variable holds a value. But I see a - in front of it, which means it doesn't have the value anymore, but its arithmetic negation."
See the problem with the formulation? x doesn't change just because a - is in front of it. You've changed the expression, so the value of the expression changes. But not the part of it that is x.
* works the same way. The expression's value is no longer the address the pointer holds, but the value at that address, the thing the pointer pointed to.

That fine too?
That's fine.

And out of curiosity, is there a difference in teh following:

Code:
```&Address and Address&
*Pointer and Pointer*```
Well, three of them are invalid.
An address is what you get when you do &variable. Thus, &address would be & &variable, and that's invalid. (Try it. The error message will probably contain the term "r-value" or "l-value".)
You can use postfix & on types:
Code:
```int i;
int &ri = i;```
This creates a reference. References are similar to pointers, but they don't advertise that fact. They are used like variables. You cannot change what they point to. You cannot have references to references. They cannot be NULL. They are, in fact, alias names for the variable they reference. In the example above you can treat i and ri exactly the same. There is no detectable difference. (And the compiler will probably completely remove the reference and use i directly.)

*Pointer
Now this takes the value of the pointer (an address) and applies the dereference operator to it. This means, as we know by now, that the result is the value at the address.

Pointer*
Syntax error. If you have type*, like matsp said, you declare/define pointers.

But I still have a couple more questions...
1) No. The & is part of the type.

2) You cannot swap the addresses of variables. The address of a variable is its identity.