C 传递if语句并需要指向对象类型的指针的程序
当用户选择zerio时,我会跳过我的代码的一部分,如果选择0,那么我会这样做,但它只是完全跳过它C 传递if语句并需要指向对象类型的指针的程序,c,arrays,function,C,Arrays,Function,当用户选择zerio时,我会跳过我的代码的一部分,如果选择0,那么我会这样做,但它只是完全跳过它 if (select = 0) { for ( i = 0; i < 20; ++i) printf("%d", array[i]); //// whenever I enter zero it skips over this if entirely } 另外,当我尝试调用一个函数并打印它时,我不断得到一个指向对象类型的指针 if (select = 1) {
if (select = 0)
{
for ( i = 0; i < 20; ++i)
printf("%d", array[i]);
//// whenever I enter zero it skips over this if entirely
}
另外,当我尝试调用一个函数并打印它时,我不断得到一个指向对象类型的指针
if (select = 1)
{
square(array, 20);
for( j = 0; j < 20; ++j);
printf("%d", a[j]);
//the j reads that I need a pointer-to-object type
}
这是我的全部代码。这是一项老任务,我正在尝试重新学习
#include<stdio.h>
#include<conio.h>
void initialize(int a[], int size)
{
int i;
}
void square(int a[], int size)
{
int j;
for( j = 0; j < size; ++j)
a[j] = a[j] * a[j];
}
int main (void)
{
int array[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,19};
int i,j,k,l,m, select, a, size;
void initialize ( int a[], int size);
void square ( int a[], int size);
printf("Please select an option from the form \n\n 0 - initialize \n\n 1 - square \n\n 2 ");
scanf("%d", &select);
if (select = 0)
{
for ( i = 0; i < 20; ++i)
printf("%d", array[i]);
//// whenever I enter zero it skips over this if entirely
}
if (select = 1)
{
square(array, 20);
for( j = 0; j < 20; ++j);
printf("%d", a[j]);
//the j reads that I need a pointer-to-object type
}
}
=是赋值运算符。使用==比较值
在您询问的第二个错误中,a在主函数中声明为整数:
int i,j,k,l,m, select, a, size;
您可能打算在printf语句中使用array而不是a。Operator=用于赋值。
运算符==用于比较。非常常见的错误:
if (select = 0)
应该是
if (select == 0)
试试看。进行防御性编程通常是一种很好的做法
if (0 == select)
因为
if (0 = select)
如果不起作用,编译器会在if中大喊,使用==和not=表示相等。我甚至无法正确缩进代码以帮助您。请删除空白。如果0==select,请不要这样做。为什么不这样做?这是避免这种错误的一种非常基本的方法,可能是因为当有人学会了使用这种顺序时,他们也学会了不使用“=”,因为他们的意思是“=”,所以它几乎在所有情况下都是无用的!另一种避免这个错误的方法是注意编译器的警告。我仍然一直在这样做,而且我已经编程7年了。有时你会分心或者打字不够慢。这只是一个很好的做法