2014-04-27 24 views
-2

我keepi讓我的代碼的一部分跳過了,它是當用戶選擇zerio,我去如果選擇0然後這樣做,但它只是完全跳過它程序傳遞if語句並需要指向對象類型的指針

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 
} 



    } 
+1

在'if',你使用''==而不是' ='表示平等。 – AntonH

+1

我甚至無法正確縮進代碼以幫助您。請刪除空格。 – lrobb

回答

0

真的常見的錯誤:

if (select = 0) 

應該

if (select == 0) 

嘗試。它通常是一個很好的做法做防禦性編程

if (0 == select) 

因爲

if (0 = select) 

不會工作,編譯器會嚷嚷

+1

請不要'if(0 == select)'。 – lrobb

+0

,爲什麼不呢?這是避免這種錯誤的一種非常基本的方式 – phyrrus9

+2

可能是因爲當某人學會了使用該順序的時候,他們也學會了不使用'='來表示'==',所以它最終變得毫無用處幾乎所有場合! –

1

=是賦值運算符老的分配。使用==進行比較。

你問一下,a第二個錯誤被聲明爲整數你main()功能:

int i,j,k,l,m, select, a, size; 

你可能是指在printf語句使用array而不是a

0

運營商=是分配。 運營商==用於比較。

相關問題