• C

How do I avoid pointer to integer casting errors in C programming?

I'm getting these compiler warnings and I would like to know how to deal with them. I don't know too much about casting so the simpler the explanation the better.

line 12:
incompatible pointer to integer conversion initializing 'char' with an expression of type void
line 35:
comparison between pointer and integer ('int' and 'void *')
line 37:
incompatible pointer to integer conversion assigning to 'char' from 'void'

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


int men_in_bathroom=0;
int women_in_bathroom=0;
int bathroom_capacity = 10;
int men_in_queue=0;
int women_in_queue=0;
int queue_capacity = 10;
int cycle=1;
char queueContents[10] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
char eventInput;



void StatusCheck(){
    printf("\nWomen in bathroom: %d \nMen in bathroom: %d\n", women_in_bathroom, men_in_bathroom);
    printf("Queue after Cycle: %d: ",cycle);
    for (int i = 0; i <= men_in_queue + women_in_queue; i++){
        if(queueContents[i] == 'M'){
            printf("Man ");
        }
        else if(queueContents[i] == 'W'){
            printf("Woman ");
        }
    }
    printf("\n");
}


void QueueReorganizer(){
    //removing the blank slots in the queue as a result of the flush
    for (int i = 0; i < men_in_queue + women_in_queue; i++){
        if(queueContents[i] == NULL && queueContents[i+1] !=NULL){
            queueContents[i] = queueContents[i+1];
            queueContents[i+1] = NULL;
            i = 0;
        }
    }
}

void QueueFlusher(){
    if(queueContents[0] == 'W'){ //if a woman is first in line upon the bathroom emptying out
        for (int i = 0; i < 10; i++){
            if(queueContents[i] == 'W'){ //replace all the female in the queue with NULLS
                queueContents[i] = NULL;
                women_in_queue--; //decrement the females-in-queue count as many times as needed until no more females remain
                women_in_bathroom++; //increment females in bathroom
            }
        }
        printf(" One or more women have entered the bathroom.");
    }
    else if(queueContents[0] == 'M'){ //if a man is first in line upon the bathroom emptying out
        for (int i = 0; i < 10; i++){
            if(queueContents[i] == 'M'){
                queueContents[i] = NULL;
                men_in_queue--;
                men_in_bathroom++;
            }
        }
        printf(" One or more men have entered the bathroom.");
    }
    QueueReorganizer();
}


void WomanWantsToEnter()
{
    if (women_in_bathroom + men_in_bathroom >= bathroom_capacity){ //if the bathroom is at full capacity
        if(men_in_queue + women_in_queue < queue_capacity){ //if the queue can accomodate more people
            printf("The bathroom is full. One woman has been added to the queue.");
            for (int i = 0; i < 10; i++){
                if(queueContents[i] == NULL){
                queueContents[i] = 'W';
                break;
                }
            }
            women_in_queue++;
        }
        else{ //if the queue is at full capacity
            printf("Sorry, the bathroom and the queue are both full. No one can be added at this time.");
        }
    }
    else if(men_in_bathroom != 0){ //if the bathroom can accomodate more people but there are men currently inside the bathroom
        if(men_in_queue + women_in_queue < queue_capacity){ //if the queue can accomodate more people
            printf("Men are currently inside the bathroom. One woman has been added to the queue.");
            women_in_queue++;
            for (int i = 0; i <= women_in_queue + men_in_queue; i++){
                if(queueContents[i] == NULL){
                    queueContents[i] = 'W';
                    break;
                }
            }
        }
        else{ //if the queue is at full capacity
            printf("Men are currently inside the bathroom, and the queue is full. Cannot accomodate any women at the moment.");
        }
    }
    else if(men_in_bathroom == 0){ //if the bathroom can accomodate more people and there are no men currently inside the bathroom
        printf("A woman has entered the bathroom");
        women_in_bathroom++;
    }
    StatusCheck();
}





void ManWantsToEnter()
{
    if (women_in_bathroom + men_in_bathroom >= bathroom_capacity){ //if the bathroom is at full capacity
        if(men_in_queue + women_in_queue < queue_capacity){ //if the queue can accomodate more people
            printf("The bathroom is full. One man has been added to the queue.");
            for (int i = 0; i < 10; i++){
                if(queueContents[i] == NULL){
                    queueContents[i] = 'M';
                    break;
                }
            }
            men_in_queue++;
        }
        else{ //if the queue is at full capacity
            printf("Sorry, the bathroom and the queue are both full. No one can be added at this time.");
        }
    }
    else if(women_in_bathroom != 0){ //if the bathroom can accomodate more people but there are women currently inside the bathroom
        if(men_in_queue + women_in_queue < queue_capacity){ //if the queue can accomodate more people
            printf("Women are currently inside the bathroom. One man has been added to the queue.");
            men_in_queue++;
            for (int i = 0; i <= women_in_queue + men_in_queue; i++){
                if(queueContents[i] == NULL){
                    queueContents[i] = 'M';
                    break;
                }
            }
        }
        else{ //if the queue is at full capacity
            printf("Women are currently inside the bathroom, and the queue is full. Cannot accomodate any men at the moment.");
        }
    }
    else if(women_in_bathroom == 0){ //if the bathroom can accomodate more people and there are no women currently inside the bathroom
        printf("A man has entered the bathroom");
        men_in_bathroom++;
    }
    StatusCheck();
}





void WomanLeaves()
{
    if(women_in_bathroom == 0 ) //if there are no women in the bathroom, tell the user the problem with his request
    {
        printf("Invalid choice. There are already no women in the bathroom.\n");
    }
    else{ //if there are some women in the bathroom, decrement the women by 1, and find a woman in the queue and decrement queue
        women_in_bathroom--;
        printf("A woman has left the bathroom.");
        if(women_in_bathroom == 0){
            QueueFlusher();
        }
        else if(women_in_queue > 0){ //if there are women in the queue...
            while (women_in_bathroom < 10) { //and there is space in the bathroom...
                women_in_bathroom ++; //add a woman to the bathroom
                women_in_queue --; //decrement the women-in-queue count
                for (int i = 0; i < 10; i++){ //go into the queue and remove the first woman
                    if(queueContents[i] == 'W'){
                        queueContents[i] = NULL;
                        break;
                    }
                }
                QueueReorganizer(); //remove the NULL in between the people in the queue
            }
        }
        StatusCheck();
    }
}

void ManLeaves()
{
    if(men_in_bathroom == 0 )
    {
        printf("Invalid choice. There are already no men in the bathroom.\n");
    }
    else{
        men_in_bathroom--;
        printf("A man has left the bathroom.");
        if(men_in_bathroom == 0){
            QueueFlusher();
        }
        else if(men_in_queue > 0){
            while (men_in_bathroom < 10) { //does this have to execute at least once? that would be bad
                men_in_bathroom ++;
                men_in_queue --;
                for (int i = 0; i < 10; i++){
                    if(queueContents[i] == 'M'){
                        queueContents[i] = NULL;
                        break;
                    }
                }
                QueueReorganizer();
            }
        }
        StatusCheck();
    }
}





int main() {
    
    while (cycle <= 25) {
        printf("\nChoose one of the following events for cycle %d:\n", cycle);
        printf("A) woman wants to enter bathroom\nB) man wants to enter bathroom\nC) woman leaves bathroom\nD) man leaves bathroom\n");
        printf("Enter A,B,C or D here --> ");
        fgets(&eventInput, 3, stdin);
        printf("\n");
        if(eventInput == 'A'){
            WomanWantsToEnter();
        }
        else if(eventInput == 'B'){
            ManWantsToEnter();
        }
        else if(eventInput == 'C'){
            WomanLeaves();
        }
        else if(eventInput == 'D'){
            ManLeaves();
        }
        cycle++;
    }
    
    printf("\n\n%d cycles completed. Thanks for playing.", cycle);


    return 0;
}
//reusing i so often bad? pointer clearing?



//        for (int i = 0; i < 10; i++){
//            if(queueContents[i] == 'M'){
//                queueContents[i] = NULL;
//                men_in_queue--;
//                QueueReorganizer();
//                break;
//            }
//        }

Open in new window

shampouyaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
use '\0' instead of NULL

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Subrat (C++ windows/Linux)Software EngineerCommented:
Line 12:
char queueContents[10] = {}

and other places, as said by ozo, replace NULL with '\0'
NULL is a macro replaced by 0.
ozoCommented:
Some implementations may
#define NULL 0
others, like the one shampouya is evidently using, will
#define NULL ((void *)0)

note that
char queueContents[10] = {};
is guaranteed only when queueContents has static storage duration.
sarabandeCommented:
to add to above comments:

i personally prefer

char queueContents[10] = { '\0'};

Open in new window


what initializes the first element of the array with a binary zero character and the rest of the array with zeros (what is the same).

a statement like this always has static storage and hardly could be mixed-up with a dynamic storage at the heap (by malloc or calloc) or an extern storage by using the extern keyword (where an initialization would be invalid).

the queueContents array is supposed to take either 'M' or 'W'. the binary zero characters for the empty slots in the array are currently not used (and not needed). because of that you might think to initialize the array like the following:

char queueContents[11] = "EEEEEEEEEE";

Open in new window

or
char queueContents[10] = { 'E', 'E',  'E', 'E',  'E', 'E',  'E', 'E',  'E', 'E', };

Open in new window


where 'E' means empty.

in the first case the char array is treated as a string and needs one more element for terminating the string.

Sara
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.