2016-10-09 5 views
0

コードブロックの長さは申し訳ありませんが、私は迷っています。変数currentProcessは配列に追加された最後の構造体のインデックスを保持します。私は、要素が追加されている間、currentProcessの値が正しくインクリメントされていることを証明するprintステートメントを含めました。しかし、この変数を関数printCurrent()に渡すと、値0が渡されます。このエラーがどこから来ているのかわからないので、プログラム全体が含まれています。前もって感謝します。 (ブランクスイッチケースブロックのお詫び、これは進行中の作業です)Cなぜこのintを値渡しすると、毎回0を渡すのが間違っています

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

#define SIZE 10 

typedef struct { 
    int pCount; 
    float pAccul; 
    int pAddress; 
}pState; 
typedef struct { 
    int id; 
    char status[10]; 
    pState state; 
    char priority; 
}PCB; 

//function prototypes 
int addProcess(PCB*, PCB, int*); 
PCB getPcb(PCB); 
void printCurrent(PCB[], int); 


int main() 
{ 
    PCB process; 
    PCB pArray[SIZE]; 
    PCB* pcbPtr; 
    char option = ' '; 
    int i; 
    int currentProcess; 
    int* cpPtr; 

    for(i=0; i<SIZE; i++) 
    { 
     pArray[i].id = 0; 
    }//end for 

    cpPtr = &currentProcess; 
    pcbPtr = pArray; 

    //simple menu with 4 options 
    while(option != '0') 
    { 
     printf("\n-----Menu-----\n"); 
     printf("\n1) Add Process\n"); 
     printf("\n2) Delete Process\n"); 
     printf("\n3) Display PCB\n"); 
     printf("\n0) Quit\n"); 
     scanf("%1s", &option); 

     switch(option) 
     { 
     case '1': 
       addProcess(pcbPtr, process, cpPtr); 
       printf("\nCHECK CHECK %d CHECK CHECK\n", currentProcess);//ERROR CHECK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
       break; 

     case '2': 
       //deleteProcess(); 
       break; 

     case '3': 
       printCurrent(pArray, currentProcess); 
       break; 

     case '0': 
      exit(0); 
     default: 
       printf("Error! Choose From Available Options!!"); 
       break; 

     }//end main switch/case 
    }//end main while 
}//end main 


int addProcess(PCB* ptr, PCB newSt, int* currentProcess) 
{ 
    int i; 

    for(i=0; i<SIZE; i++) 
    { 
     if((ptr+i)->id == 0) 
     { 
      ptr[i] = getPcb(newSt); 
      *currentProcess = i; 
      printf("%d", *currentProcess);//ERROR CHECK!!! 
      return 0; 
     }//end if 
    }//end for 
    return 1; 
}//end addProcess() 


PCB getPcb(PCB new) 
{ 
    printf("\nEnter Id\n"); 
    scanf("%d", &new.id); 
    printf("\nEnter Status\n"); 
    scanf("%s", new.status); 
    printf("\nEnter Process Counter Value\n"); 
    scanf("%d", &new.state.pCount); 
    printf("\nEnter Acculumator Value\n"); 
    scanf("%f", &new.state.pAccul); 
    printf("\nEnter Process Address (Unsigned Int)\n"); 
    scanf("%d", &new.state.pAddress); 
    printf("\nEnter Priority (l/h)\n"); 
    scanf("%1s", &new.priority); 

    return new; 
}//end getPcb() 


void printCurrent(PCB array[], int currentProcess) 
{ 
    printf("!!!!!!!!%d!!!!!!!!!!!!!!!!!!!!!", currentProcess);//!!!!!!!!!! 
    printf("\n---------------------------\n"); 
    printf("\nProcess ID: %d\n", array[currentProcess].id); 
    printf("Status: %s\n", array[currentProcess].status); 
    printf("Process Counter: %d\n", array[currentProcess].state.pCount); 
    printf("Acculumator Value: %.2f\n", array[currentProcess].state.pAccul); 
    printf("Process Address: %d\n", array[currentProcess].state.pAddress); 
    printf("Priority: %c\n", array[currentProcess].priority); 
    printf("\n----------------------------\n"); 

}//end printCurrent() 
+0

は、ここでは、コードに変更されます。すべての入力と出力を含めます。 – kaylum

+0

'getPcb'にパラメータを渡すポイントは何ですか? –

+0

@MichaelWalz公正なポイントが、本当に問題になる可能性がありますか? –

答えて

-1

M.Mが正しいです。 {scanf("%1s", &option);}は醜いです。 "%c"の場合に行います。あなたのテスト実行のログを表示してください

//function prototypes 
int addProcess(PCB* arr, int*); //you don't need newSt 
PCB getPcb();     
void printCurrent(PCB[], int); 

case '1': 
     // address of first element of the array is passed 
     // this way, you are letting addProcess know the location 
     // of pArray on main()'s stack so that it can fill it 
     addProcess(&pArray, &currentProcess); 
     break; 

int addProcess(PCB* ptr, int* currentProcess) 
{ 
     int i; 

     for(i = 0; i < SIZE; i++) 
     { 
       if(ptr[i].id == 0) 
       { 
         ptr[i] = getPcb(); 
         // getPcb() will initialize and return 
         // a new PCB which is assigned to the      
         // appropriate location of array 
         *currentProcess = i; 
         printf("%d", *currentProcess);//ERROR CHECK!!! 
         return 0; 
         // the function is anyway returning an int 
         // why not return "i"? 
       }//end if 
     }//end for 
     return 1; 
}//end addProcess() 
+0

フィードバックをいただき、ありがとうございました。この関数は、ブール値の代わりにintを返します。私は0と10の間の値を持つことができるので、私はそれを使用しません –

+0

これはスタイルの問題ではなく、正しいコードと間違ったコードの違いです。 – Olaf

+0

「醜い」とは、コードのスタイルや美意識を意味するものではありませんでした。私は深刻な結果につながる醜いコーディングの練習であることを意味しました。 – nachiketkulk

関連する問題