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

  1. #31
    Registered User
    Join Date
    Oct 2007
    Posts
    24
    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 =[
    Last edited by Bash; 10-16-2007 at 06:17 PM.

  2. #32
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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.
    Last edited by Daved; 10-16-2007 at 09:35 PM.

  3. #33
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    (a > b > c > d)
    This doesn't do what you think it does. A single operand cannot be operand to more than one operator.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #34
    Registered User
    Join Date
    Oct 2007
    Posts
    24
    Quote Originally Posted by CornedBee View Post
    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. #35
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Bash View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #36
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    a < b && b < c && c < d
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #37
    Registered User
    Join Date
    Oct 2007
    Posts
    24
    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...
    Last edited by Bash; 10-18-2007 at 03:32 AM.

  8. #38
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #39
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Bash View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #40
    Registered User
    Join Date
    Oct 2007
    Posts
    24
    Quote Originally Posted by CornedBee View Post
    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?
    Last edited by Bash; 10-18-2007 at 03:57 AM.

  11. #41
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Bash View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #42
    Registered User
    Join Date
    Oct 2007
    Posts
    24
    Quote Originally Posted by matsp View Post
    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. #43
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Bash View Post
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Password program / general programming questions
    By plr112387 in forum C++ Programming
    Replies: 13
    Last Post: 11-04-2007, 10:10 PM
  2. n00b questions
    By C+noob in forum C++ Programming
    Replies: 43
    Last Post: 07-09-2005, 03:38 PM
  3. A Few General C Questions
    By Jedijacob in forum C Programming
    Replies: 13
    Last Post: 02-17-2005, 02:47 AM
  4. Taking up OpenGL(extreme n00b questions)
    By Xterria in forum Game Programming
    Replies: 3
    Last Post: 03-26-2004, 04:49 PM
  5. FAQ: Examples of good questions (General)
    By Prelude in forum FAQ Board
    Replies: 1
    Last Post: 10-11-2002, 08:57 PM