Reputation: 563
I have a code to implement a stack using array, here the complete code : here
The case is why I cannot to push more than one character, but
only one character?
but I've been clicking some variables are initialized using a struct
for her push
some characters in the form of an array:
struct stackMhs {
char nama[10];
char npm[10];
char telp[10];
int size;
};
struct stackMhs stackMhsbaru;
This is the push()
function with a parameter which will be the contents of data in the function main()
:
void push(char nm, char np, char tel) {
if(stackMhsbaru.size != 10) {
stackMhsbaru.nama[stackMhsbaru.size + 1] = nm;
stackMhsbaru.npm[stackMhsbaru.size + 1] = np;
stackMhsbaru.telp[stackMhsbaru.size + 1] = tel;
stackMhsbaru.size++;
}
else {
printf("stack is full!");
}
}
The problem is when I use '
to fill data is only one character at the push()
function like push('a','b','c');
when at compile no errors, but when I use "
such as push("aa","bb","cc");
when at compile error occurs:
main.c: In function 'main':
main.c:60:6: warning: passing argument 1 of 'push' makes integer from pointer without a cast [-Wint-conversion]
push("aa", "bb", "cc");
^
main.c:23:6: note: expected 'char' but argument is of type 'char *'
void push(char nm, char np, char tel) {
^
main.c:60:12: warning: passing argument 2 of 'push' makes integer from pointer without a cast [-Wint-conversion]
push("aa", "bb", "cc");
^
main.c:23:6: note: expected 'char' but argument is of type 'char *'
void push(char nm, char np, char tel) {
^
main.c:60:18: warning: passing argument 3 of 'push' makes integer from pointer without a cast [-Wint-conversion]
push("aa", "bb", "cc");
^
main.c:23:6: note: expected 'char' but argument is of type 'char *'
void push(char nm, char np, char tel) {
^
My question is : any solution ?
Upvotes: 0
Views: 321
Reputation: 6116
In C, anything inside a ''
is a character (declared using char
) and anything inside ""
represents a string which is a null terminated array of char
s.
You cannot assign an array of char
s to a single char
variable, hence you see the warnings.
Explanation of the warning:
warning: passing argument 1 of 'push' makes integer from pointer without a cast
For explanation sake, assume that the compiler means char
when it says int
, then basically it is complaining that you are trying to assign a char
array type to a char
without explicitly telling compiler that you want to do so.
Correct way of doing what you are trying:
Pass the string character by character to the push
function called inside a loop.
Upvotes: 2
Reputation: 734
First you should try to care more about your code indentation, it would be easier for us to read it.
Now, for your problem : there is a huge difference in C between 'c'
and "c"
. The first is just a character and the second is a null terminated array of characters, which is totally different.
When you call push("aa", "bb", "cc");
, you are giving 3 arrays of characters to a function needing 3 characters, this is why your code does not works.
To bypass this, you can call your function multiple time with all the different characters of your array of characters :
char *str = "foo";
while (*str)
push(*(str++));
This way, the while
will loop in your string and call the push()
function will all the characters available in str
.
Upvotes: 1