user3362107
user3362107

Reputation: 279

Declare a `char` to pass to a function

Sorry, this is a really simple question but I can't find the answer...I have implemented a char stack with a push function that takes in a char to push into the stack. However, I don't know how to actually declare a char to push into the stack.

I've tried doing this, but I get an error saying that I'm trying to convert from a const char* to a char:

int main(){
    char_stack stack;
    char c = "x";
    s1.push(c);
}

I've also tried doing this, but I think that's making c into a char*, and since my push function takes in only chars, I get another error.

int main(){
    char_stack s1;
    char c[] = "p";
    s1.push(c);
}

Thanks!!

Upvotes: 0

Views: 143

Answers (2)

tisma
tisma

Reputation: 56

Try with char c = 'x';. What you are trying is defining a string (char array) and assigning to char variable.

Upvotes: 1

bialpio
bialpio

Reputation: 1024

The proper way to go is:

char c = 'p';

Upvotes: 4

Related Questions