C为什么在按值传递此int时,每次都错误地传递0

时间:2016-10-09 05:40:56

标签: c arrays struct pass-by-reference

很抱歉代码块的长度,但我很茫然。变量currentProcess保存添加到数组的最后一个结构的索引。我已经包含了print语句来证明currentProcess的值在添加元素时正在递增。然而,当我将此变量传递给函数printCurrent()时,它传递值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()

1 个答案:

答案 0 :(得分:-1)

M.M是对的。 {scanf("%1s", &option);}很难看。如果&#34;%c&#34;。

以下是更改的代码:

//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()