c++ - 指针符号和数组

标签 c++ arrays pointers notation

(C++) 我的程序应该让用户输入数字,这将决定数组的大小及其元素。然后我需要按升序对元素进行排序。我的问题是每当我运行我的程序时它都会显示排序后的数字;相反,它显示地址(如果我是正确的)。

    #include <iostream>
    using namespace std;


int main()
{

   int size;
   int i,x,tempVar;
   cout << "Enter how many students took the test: ";
   cin >> size;
   int *myArray = new int [size];

for  ( i = 0; i < size; i++)
{
    cout << "Enter a score:  ";
    cin >> myArray[size];
}

for ( i = 0; i < size; i++)
{
    for ( x = 0; x < size; x++)
    {
        if (myArray[i] < myArray[x])
        {
            tempVar = myArray[i];
            myArray[i] = myArray[x];
            myArray[x] = tempVar;
        }
    }
}

cout << "The scores have been sorted out in an ascending order\n";
for (i = 0; i < size; i++)
{
    cout << *(myArray + i) << endl;
}

delete [] myArray;
}

最佳答案

它不显示地址;它可能显示垃圾值,因为您有:

cin >> myArray[size];

代替

cin >> myArray[i];

关于c++ - 指针符号和数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21372212/

相关文章:

c++ - 将dll编译为静态库时如何处理DLL_EXPORT?

c++ - 你如何破译指针+数组的复杂声明?

pointers - 在 Go 中比较指针

c - 指针数组 - 需要更大

c - 如果只在运行时知道,如何调用函数指针,其中参数的数量

java - 读取文件并将名称和数字存储在两个数组中

模板类型的C++条件构造

c++ - 与列表初始化语法的自动分配混淆

c++ - 为什么 C++ 标准允许 std::max_align_t 和 __STDCPP_DEFAULT_NEW_ALIGNMENT__ 不一致?

arrays - Swift 4 func prepare(for segue : UIStoryboardSegue, sender : Any?)不使用indexPath.row更新Int变量