Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9075745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:56:10+00:00 2026-06-16T18:56:10+00:00

I am a newbie in C++ and I’m reading this ebook called Jumping into

  • 0

I am a newbie in C++ and I’m reading this ebook called Jumping into C++ by Alex Allain which is extremely helpful.

I have recently finished the pointers chapter. There is a practice problem at the end of the chapter where it asks you to write a program that compares the memory addresses of two different variables on the stack and print out the order of the variables by numerical order of their addresses.

So far I got the program running but I am not satisfied if I implemented it the right way and I want an expert opinion about my solution to figure out if I am heading the right direction. Below is my own solution to the problem (comments and tips will be helpful):

// pointersEx05.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>


int _tmain(int argc, _TCHAR* argv[])
{
    int x,y; // two integer type variables
    int *firstVal, *secondVal; // two pointers will point out to an int type variable


    std::cout << "enter first value: ";
    std::cin >> x; // prompt user for the first value
    std::cout << std::endl << "enter second value: ";
    std::cin >> y; // prompt user for the second value
    std::cout << std::endl;

    firstVal = &x; // point to the memory address of x
    secondVal = &y; // point to the memory address of y

    std::cout << firstVal << " = " << *firstVal; // print out the memory address of the first value and also the value in that address by dereferencing it
    std::cout << "\n" << secondVal << " = " << *secondVal;  // print out the memory address of the second value and also the value in that address by dereferencing it

    std::cout << std::endl;


    if(firstVal > secondVal){ // check if the memory address of the first value is greater than the memory address of the second value
        std::cout << *secondVal << ", "; // if true print out second value first  then the first value
        std::cout << *firstVal;
    }else if(secondVal > firstVal){ // check if the memory address of the second value is greater than the memory address of the first value
        std::cout << *firstVal << ", "; // if true print out first value first then the second value
        std::cout << *secondVal << ", ";
    }
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T18:56:12+00:00Added an answer on June 16, 2026 at 6:56 pm

    This is “correct”, but it’s not well-defined behaviour. You can only compare addresses of elements in the same array, or members of the same instance of a struct. From C99 (6.5.8):*

    When two pointers are compared, the result depends on the relative
    locations in the address space of the objects pointed to. If two
    pointers to object or incomplete types both point to the same object,
    or both point one past the last element of the same array object, they
    compare equal. If the objects pointed to are members of the same
    aggregate object, pointers to structure members declared later compare
    greater than pointers to members declared earlier in the structure,
    and pointers to array elements with larger subscript values compare
    greater than pointers to elements of the same array with lower
    subscript values. All pointers to members of the same union object
    compare equal. If the expression P points to an element of an array
    object and the expression Q points to the last element of the same
    array object, the pointer expression Q+1 compares greater than P. In
    all other cases, the behavior is undefined.

    (empahsis mine)

    So this is probably what your supervisor was looking for, and it will probably “work”, but it’s still not valid as far as the language standard is concerned.


    * Section [expr.rel] of the C++ standard(s) say something similar, but it’s more verbose, due to caveats for class members and so on. And it also states that anything else is “unspecified” rather than “undefined”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Newbie question. I have Django models that look like this: class Video(models.Model): uploaded_by =
Newbie question: I have a loop which places a number of elements on a
Newbie to Python, so this may seem silly. I have two dicts: default =
Newbie question here but for some reason I cant figure this out. I have
Newbie question, let me try and make this as clear as possible. I have
Newbie Q. In my MainViewController, which is the first visible view. I have a
Newbie Question. I have a UIWebView that I push HTML code into. In the
newbie rails question coming up. I have a class like this: class Thing <
newbie doing Java homework here. I have one class named Album which contains the
newbie here, so thanks in advance for help! I have a Wordpress site with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.