0_insomniac_0 0_insomniac_0 - 1 month ago 6
C Question

C Why when passing this int by value, it is incorrectly passing 0 every time

Ok so sorry for the length of the code block, but I am at a loss. The variable currentProcess holds the index of the last structue added to the array. I have included print statements to prove that the value of currentProcess is being incremented correctly as elements are being added. However, when I then pass this variable to the function printCurrent() it passes the value 0. I included the whole program as it stands, because I have no idea where this error could be coming from, any help is appreciated. thanks in advance. (Apologies for the blank switch case block, this is a work in progress)

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

Answer

M.M is right. {scanf("%1s", &option);} is ugly. Make if "%c".

Here is changed code:

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