Thread: Not sure where the mistake is coming from?

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    78

    Not sure where the mistake is coming from?

    The value is correct...almost. I don't get the 25 value the boook gets:

    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
    	int *num_ptr{ nullptr };
    
    
    	num_ptr = new int;
    
    
    	cout << &num_ptr << endl;
    
    
    	cout << *num_ptr << endl;
    
    
    	num_ptr - 25;
    
    
    	cout << *num_ptr << endl;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,156
    Well num_ptr - 25; by itself does nothing.

    Perhaps you meant *num_ptr = -25;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2019
    Posts
    78
    I tried that and it did not compile. When I do compile I get rubbish memory instead of the value in the code.
    Quote Originally Posted by Salem View Post
    Well num_ptr - 25; by itself does nothing.

    Perhaps you meant *num_ptr = -25;

  4. #4
    Registered User
    Join Date
    Dec 2019
    Posts
    78
    Quote Originally Posted by Salem View Post
    Well num_ptr - 25; by itself does nothing.

    Perhaps you meant *num_ptr = -25;
    Sorry, I meant to say with the = sign nothing compiles and get an error.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,390
    Don't show us the actual code.
    It's always better if we just guess.
    I'm guessing your code looked like this:
    Code:
    #include <iostream>
    using namespace std;
     
    int main()
    {
        int *num_ptr{ I_Pooped_My_Pants };
     
        num_ptr = new int;
     
        cout << &num_ptr << endl;
        cout << *num_ptr << endl;
     
        *num_ptr = -25 sir;
     
        cout << *num_ptr << endl;
    }
    Well, firstly, I_Pooped_My_Pants is undefined!
    What were you thinking?! That makes no sense at all!
    Try either nullptr, or just leave the braces empty, which also initializes the pointer to null.

    Secondly, and I can't emphasize this enough, you don't need to say "sir" after an assignment statement. Just get rid of that part.

    BTW, &num_ptr is just the relatively uninteresting address of the num_ptr pointer variable itself. The actual address of the new int variable is the value of num_ptr, not it's address.
    A long time ago being crazy meant something. - Charles Manson

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,156
    > Sorry, I meant to say with the = sign nothing compiles and get an error.
    What?

    Oh wait, you wrote this on your phone rather than copy/pasting it from your text editor.

    Come back when you're at your desk and can make a proper post.

    Half remembered gibberish doesn't help anyone and is just a waste of time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Dec 2019
    Posts
    78
    Quote Originally Posted by john.c View Post
    Don't show us the actual code.
    It's always better if we just guess.
    I'm guessing your code looked like this:
    Code:
    #include <iostream>
    using namespace std;
     
    int main()
    {
        int *num_ptr{ I_Pooped_My_Pants };
     
        num_ptr = new int;
     
        cout << &num_ptr << endl;
        cout << *num_ptr << endl;
     
        *num_ptr = -25 sir;
     
        cout << *num_ptr << endl;
    }
    Well, firstly, I_Pooped_My_Pants is undefined!
    What were you thinking?! That makes no sense at all!
    Try either nullptr, or just leave the braces empty, which also initializes the pointer to null.

    Secondly, and I can't emphasize this enough, you don't need to say "sir" after an assignment statement. Just get rid of that part.

    BTW, &num_ptr is just the relatively uninteresting address of the num_ptr pointer variable itself. The actual address of the new int variable is the value of num_ptr, not it's address.
    Thank you, daddy. I love you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Not sure where the mistake is coming from?
    By solidusMGS in forum C++ Programming
    Replies: 11
    Last Post: 07-23-2021, 05:16 PM
  2. my second coming
    By thestien in forum Game Programming
    Replies: 6
    Last Post: 12-17-2007, 09:54 AM
  3. Where are all these errors coming from?
    By Tride in forum C++ Programming
    Replies: 5
    Last Post: 08-24-2004, 09:37 AM
  4. Interview coming up
    By thomashdavies in forum C++ Programming
    Replies: 2
    Last Post: 06-30-2004, 08:23 PM
  5. Job Interview Coming Up
    By adamg in forum C Programming
    Replies: 5
    Last Post: 05-02-2004, 11:25 PM

Tags for this Thread