混淆 ' int*s = new int[i]; ' 和 ' int*f = new int; '

Confusion with ' int*s = new int[i]; ' and ' int*f = new int; '

int t, i;
cin >> i;

int*s = new int[i];

for (t = 0; t < i; t++) {
    s[t] = t;//How??
    cout << s[t] << "_" << &s[t] << endl;
}

int*f = new int;
*f = t; // Whhhhhy???
delete f;
delete[] s

我声明了一个数组类型和一个整数作为动态内存。

虽然我可以将任何普通整数值分配给 *f(以及 f 的指针值), 它在为 *s[t].

分配整数值时不断警告我

我不知道为什么。

您收到警告的原因是 s[t]*(s+t) 的 shorthand,所以 *s[t]**(s+t) 的 shorthand .太多了 *.

[] 运算符包含一个隐含的“*”。将您的值分配给 s[t] 而不是 *s[t]