gpt4 book ai didi

c++ - 这是按内存地址对两个变量进行数字排序的有效方法吗?

转载 作者:行者123 更新时间:2023-11-30 02:57:46 25 4
gpt4 key购买 nike

我是 C++ 的新手,正在阅读这本名为 Jumping into C++ 的电子书。由 Alex Allain 撰写,非常有帮助。

我最近完成了指针章节。本章末尾有一道练习题,要求你编写一个程序,比较堆栈中两个不同变量的内存地址,并按照地址的数字顺序打印出变量的顺序。

到目前为止,我已经运行了程序,但如果我以正确的方式实现它,我并不满意,我希望专家对我的解决方案发表意见,以确定我是否朝着正确的方向前进。以下是我自己的问题解决方案(评论和提示会有所帮助):

// 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;
}

最佳答案

这是“正确的”,但不是明确定义的行为。您只能比较同一数组中元素的地址,或同一结构实例的成员。来自 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.

(强调我的)

所以这可能是您的主管正在寻找的东西,它可能会“起作用”,但就语言标准而言它仍然无效。


* C++ 标准的 [expr.rel] 节说了类似的话,但由于对类成员的警告等原因,它更加冗长。它还声明其他任何内容都是“未指定”而不是“未定义”。

关于c++ - 这是按内存地址对两个变量进行数字排序的有效方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14185698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com