我是 C 新手,我找不到任何关于为什么它给我一个错误的答案:第 11 行和第 20 行表达式不可分配
。
这是一个简单的代码,用于创建随机数组,然后使用子例程对其进行排序。我仍在学习 C,并且无法通过任何研究找到解决方案。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
int myArray[100];
int* ptr = myArray;
int i;
int n = sizeof(myArray);
int SortedArray[100];
int MakeRandArray(ptr, i)
{
for (i = 0; i < n; i++) {
(ptr + i) = rand()
}
}
int SortArray(ptr)
{
int a;
int j = (i + 1);
for (i = 0; i < n; i++) {
if ((ptr + i) > (ptr + j)) {
(ptr + i) = (ptr + j)
}
}
}
int main()
{
MakeRandArray(*ptr, i);
SortArray(*ptr);
}
最佳答案
您没有正确取消引用指针。
第 11 行:
ptr[i] = rand();
第 20 行:
if (ptr[i] > ptr[j]){
ptr[i] = ptr[j];
}
或者,您可以指定 *(ptr + i)
来获取 ptr[i]
处的值。
关于c - 出现错误 : expression not assignable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44377418/