Lot of questions.

This is a discussion on Lot of questions. within the C++ Programming forums, part of the General Programming Boards category; Exactly it. *i becomes 4, but k remains 3*...

  1. #16
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    905
    Exactly it.

    *i becomes 4, but k remains 3*
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #17
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43
    Ok so when using i++, the int k will become what i was but then i will change and the k will not change because the post-increment makes it so that only the i changes. But the pre-increment makes the i change before the int k makes itself equal to i so they would both be the same.(Does that make sense?)


    PS: This is completely unrelated to anything we were talking about but i was just wondering, how do you get a signature?

  3. #18
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    905
    That's one way to think about it. Think about it more literally and it might be simpler.

    ++i adds one to i and returns i

    i++ returns a temporary copy of i and then adds one to the real i.

    *edit* "user cp" for the signature.
    Last edited by CodeMonkey; 01-04-2009 at 01:09 AM.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #19
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43

    Smile

    Thanks alot!

  5. #20
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43
    I still dont completely understand how to use pointers. I barely understand what their actual purpose is. Apparently they help to make sure that you have enough space for your program and that you arent wasting any space either. Please help, the whole purpose of pointers just isnt clear. How are they supposed to help your program? when should you use them? HOW do you use them? They just dont seem to make any sense.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Quote Originally Posted by Ryan0773
    I still dont completely understand how to use pointers. I barely understand what their actual purpose is.
    Have you read the pointers tutorial?

    Quote Originally Posted by Ryan0773
    Apparently they help to make sure that you have enough space for your program and that you arent wasting any space either. Please help, the whole purpose of pointers just isnt clear. How are they supposed to help your program? when should you use them? HOW do you use them? They just dont seem to make any sense.
    Well, one use of pointers is to avoid copying an expensive to copy object when you want to pass that object around. However, in C++ this use is largely replaced by references, unless you also want to make passing an object optional (i.e., the caller can pass a null pointer instead of a pointer to an object).

    Another use would be to manage a dynamically allocated array, or to create links between the nodes of a linked list. However, this use is largely replaced by containers that abstract away the use of pointers.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43

    Question Laserlight reply

    Yes, i did read the tutorial. I can sortof make a basic pointer that points to another integer. but whats the purpose of codes like

    Code:
    int *pnt = new int;
    and

    Code:
    delete ptr;
    The first one is supposed do something like take a free pointer and make it point to something that becomes unavailable to other programs or something (dont really understand what the means)
    The second one is supposed to delete a pointer that was allocated with new. Thats doesn't really help me if I dont understand the purpose of new.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Quote Originally Posted by Ryan0773
    The first one is supposed do something like take a free pointer and make it point to something that becomes unavailable to other programs or something (dont really understand what the means)
    You can think of it as "creating an object of type int". The pointer named pnt points to this object.

    Quote Originally Posted by Ryan0773
    The second one is supposed to delete a pointer that was allocated with new.
    You can think of it as "destroying the object of type int which ptr points to".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43
    Quote Originally Posted by laserlight View Post
    You can think of it as "creating an object of type int". The pointer named pnt points to this object.
    But then how can i give the object of type int a term?

    Quote Originally Posted by laserlight View Post
    You can think of it as "destroying the object of type int which ptr points to".
    So this makes the ptr a null pointer?

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Quote Originally Posted by Ryan0773
    But then how can i give the object of type int a term?
    What do you mean?

    Quote Originally Posted by Ryan0773
    So this makes the ptr a null pointer?
    No, ptr merely points to a destroyed object and hence may not be dereferenced unless and until you make it point to an object that exists. To set ptr to be a null pointer after the object that it points to is destroyed you would write:
    Code:
    ptr = 0;
    or if the <cstddef> header is directly or indirectly included you can also write:
    Code:
    ptr = NULL;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43
    Quote Originally Posted by laserlight View Post
    What do you mean?
    Like when you write a code that has an int with a certain interger assigned to it

    Code:
    int x(10);
    Make it so that x has a value of ten. When i make a pointer point to something else, how can i make it so that the int that it points to has a value (srry i think i might have used the wrong word to explain it)

    Quote Originally Posted by laserlight View Post
    No, ptr merely points to a destroyed object and hence may not be dereferenced unless and until you make it point to an object that exists. To set ptr to be a null pointer after the object that it points to is destroyed you would write:

    Code:
    ptr = 0;
    or if the <cstddef> header is directly or indirectly included you can also write:

    Code:
    ptr = NULL;
    So if i want to make a pointer null (or point at 0 or nothing) then i would have to do it manually by inserting the code. it wouldn't do it by itself.

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Quote Originally Posted by Ryan0773
    Like when you write a code that has an int with a certain interger assigned to it
    Oh, you can initialise the int with the value of 10 by writing:
    Code:
    int* ptr = new int(10);
    After the object has been created, you can assign the value of 10 to it by writing:
    Code:
    *ptr = 10;
    This latter syntax was covered by the tutorial.

    Quote Originally Posted by Ryan0773
    So if i want to make a pointer null (or point at 0 or nothing) then i would have to do it manually by inserting the code. it wouldn't do it by itself.
    Yes, and for further reading read Stroustrup's answer to the FAQ: Why doesn't delete zero out its operand?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43
    Quote Originally Posted by laserlight View Post
    This latter syntax was covered by the tutorial.
    I'm not sure that it was.

    Quote Originally Posted by laserlight View Post
    Yes, and for further reading read Stroustrup's answer to the FAQ: Why doesn't delete zero out its operand?
    Alright ill check it out,thanks. But does is there a way that i can change the characteristics of a certain pointer like *p which point to int i, could i change it so that later in the script *p would go from i to x?

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Quote Originally Posted by Ryan0773
    But does is there a way that i can change the characteristics of a certain pointer like *p which point to int i, could i change it so that later in the script *p would go from i to x?
    Just assign the address of x to p.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #30
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43
    Quote Originally Posted by laserlight View Post
    Just assign the address of x to p.
    How would i write that in code would it be something like...

    Code:
    {
    int i;
    int x;
    int *p
    
    p = &i
    cout<< *p <<"\n";
    int *p = new int(x);
    cout<< *p <<"\n";
    delete p;
    *p = 0;
    cin.get();
    }
    I have no idea if this would work or not. Can you tell me if i made any small errors or if i did it completely wrong?

Page 2 of 7 FirstFirst 1234567 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  2. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM
  3. Questions.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-19-2004, 02:16 PM
  4. C++ test questions
    By Mister C in forum C++ Programming
    Replies: 9
    Last Post: 09-08-2002, 12:05 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21