堆栈:无法将几个字符压入数组 [英] Stack : not able to push a couple of characters into array
问题描述
我有一个代码可以使用数组实现堆栈,这里是完整的代码:这里
I have a code to implement a stack using array, here the complete code : here
这种情况就是为什么我不能推多个字符,而
只推一个字符?
,但是我一直在单击一些变量,这些变量使用 struct
初始化,以便她将
的某些字符以数组的形式推送给
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;
这是 push()
函数参数,该参数将成为函数 main()
中的数据内容:
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!");
}
}
问题是我使用'
在 push()
函数中仅一个字符,例如 push('a','b ','c');
在编译时没有错误,但是当我使用
如 push( aa , bb, cc);
发生编译错误:
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 ?
推荐答案
在C语言中,''
内的任何字符都是(使用 char
声明,并且
内的任何内容都表示一个字符串,该字符串是 char
s。
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.
不能将 char
s数组分配给单个 char
变量,因此您会看到警告。
You cannot assign an array of char
s to a single char
variable, hence you see the warnings.
警告的解释:
警告:传递'push'的参数1会使指针的整数不进行强制转换
出于解释的原因,假定编译器在显示 int
时表示 char
,然后基本上是在抱怨您试图将 char
数组类型分配给 char
,而没有明确告诉编译器您想要这样做。
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.
正确的操作方式:
逐个字符地将字符串传递给 push
函数在循环内调用。
Pass the string character by character to the push
function called inside a loop.
这篇关于堆栈:无法将几个字符压入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!