不确定这段代码发生了什么
我有一些 C 代码,但我不太确定发生了什么。
#include <stdio.h>
#include <stdlib.h>
#define DIM1 7
#define DIM2 5
#define RES_SIZE 1000
typedef double stackElementT;
typedef struct {
stackElementT *contents;
int maxSize;
int top;
int min2;
} stackT;
void StackInit(stackT *stackP, int maxSize) {
stackElementT *newContents;
newContents = (stackElementT *)malloc(sizeof(stackElementT)*maxSize);
if (newContents == NULL) {
fprintf(stderr, "Not enough memory.\n");
exit(1);
}
stackP->contents = newContents;
stackP->maxSize = maxSize;
stackP->top = -1;
}
void StackDestroy(stackT *stackP) {
free(stackP->contents);
stackP->contents = NULL;
stackP->maxSize = 0;
stackP->top = -1;
}
int StackIsEmpty(stackT *stackP) { return stackP->top < 0; }
int StackIsFull(stackT *stackP) { return stackP->top >= stackP->maxSize-1; }
void StackPush(stackT *stackP, stackElementT element) {
if(StackIsFull(stackP)) {
fprintf(stderr, "Can't push element: stack is full.\n");
exit(1);
}
stackP->contents[++stackP->top] = element;
}
stackElementT StackPop(stackT *stackP) {
if(StackIsEmpty(stackP)) {
fprintf(stderr, "Can't pop element: stack is empty.\n");
exit(1);
}
return stackP->contents[stackP->top--];
}
int shell(char* s1, int arg) {
printf("> ");
scanf("%s %d%*c", &s1, &arg);
return arg;
}
int main() {
char cmds[DIM1][DIM2] = {{"push"}, {"pop"}, {"add"}, {"ifeq"}, {"jump"}, {"print"}, {"dup"}};
char* s1; int arg;
arg = shell(s1, arg);
printf("%s\n", &s1);
}
输入:push 4
。它打印 J+
而不是“push”,但正常打印 4
。
它还在编译时给出这些警告:
stack.c: In function ‘shell’:
stack.c:60: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’
stack.c: In function ‘main’:
stack.c:71: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’
stack.c:65: warning: unused variable ‘cmds’
stack.c:69: warning: ‘arg’ is used uninitialized in this function
有人可以解释一下吗?
I have some C code, and I'm not quite sure what's going on.
#include <stdio.h>
#include <stdlib.h>
#define DIM1 7
#define DIM2 5
#define RES_SIZE 1000
typedef double stackElementT;
typedef struct {
stackElementT *contents;
int maxSize;
int top;
int min2;
} stackT;
void StackInit(stackT *stackP, int maxSize) {
stackElementT *newContents;
newContents = (stackElementT *)malloc(sizeof(stackElementT)*maxSize);
if (newContents == NULL) {
fprintf(stderr, "Not enough memory.\n");
exit(1);
}
stackP->contents = newContents;
stackP->maxSize = maxSize;
stackP->top = -1;
}
void StackDestroy(stackT *stackP) {
free(stackP->contents);
stackP->contents = NULL;
stackP->maxSize = 0;
stackP->top = -1;
}
int StackIsEmpty(stackT *stackP) { return stackP->top < 0; }
int StackIsFull(stackT *stackP) { return stackP->top >= stackP->maxSize-1; }
void StackPush(stackT *stackP, stackElementT element) {
if(StackIsFull(stackP)) {
fprintf(stderr, "Can't push element: stack is full.\n");
exit(1);
}
stackP->contents[++stackP->top] = element;
}
stackElementT StackPop(stackT *stackP) {
if(StackIsEmpty(stackP)) {
fprintf(stderr, "Can't pop element: stack is empty.\n");
exit(1);
}
return stackP->contents[stackP->top--];
}
int shell(char* s1, int arg) {
printf("> ");
scanf("%s %d%*c", &s1, &arg);
return arg;
}
int main() {
char cmds[DIM1][DIM2] = {{"push"}, {"pop"}, {"add"}, {"ifeq"}, {"jump"}, {"print"}, {"dup"}};
char* s1; int arg;
arg = shell(s1, arg);
printf("%s\n", &s1);
}
Input: push 4
. It prints J+
instead of "push" but prints 4
normally.
It also gives these warnings on compile:
stack.c: In function ‘shell’:
stack.c:60: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’
stack.c: In function ‘main’:
stack.c:71: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’
stack.c:65: warning: unused variable ‘cmds’
stack.c:69: warning: ‘arg’ is used uninitialized in this function
Can someone please explain?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使用
%s
格式说明符时,它期望一个值,该值是指向字符串开头的指针。在 C 中,此类型为char *
。使用您的
main
函数,您的变量s1
的类型为char *
。因此,s1
是printf
的有效参数,因此这一行是有效的:请注意
& 缺少 > 在
s1
前面。在您的代码中,您使用了&
,它采用s1
的地址,其结果将是char **
类型。这是错误类型,因此不要使用&
。问题是, printf 实际上无法判断其参数是什么类型,因为它是一个可变参数函数。它只是根据格式字符串中指定的类型使用存在的任何参数。
scanf
也是如此,但有一个陷阱:您必须确保分配足够的内存来考虑用户输入,否则您将遇到缓冲区溢出并产生不可预测的结果。除此之外,printf
和scanf
是完美互补的。无论如何,除了未使用的 cmds 变量(在提供的代码中没有必要)之外,这会处理编译器警告。另外,还有 args 部分 - 它确实应该是在 shell 内部声明的变量,而不是作为参数传递,因为它的值甚至没有在
shell 内部使用。代码>外壳。
不知道其余代码怎么样。考虑到您的
main
函数仅调用shell
,这是多余的。When you use the
%s
format specifier, it expect a value which is a pointer to the start of a string. In C, this type ischar *
.Taking your
main
function, your variables1
is of typechar *
. Therefore,s1
is a valid parameter toprintf
, so this line is valid:Note the absence of an
&
in front ofs1
. In your code, you used the&
, which takes the address ofs1
, the result of which will be of typechar **
. This is the wrong type, so don't use the&
.The thing is,
printf
can't actually tell what type its arguments are, since it is a variadic function. It simply uses whatever arguments are there, according to the types specified in the format string.The same thing goes for
scanf
, but there is a pitfall: you must make sure that enough memory is allocated to account for the user input, else you will experience a buffer overflow with unpredictable results. Aside from this,printf
andscanf
are perfectly complementary.Anyhoo, this takes care of the compiler warnings, aside from the unused
cmds
variable (it's unnecessary in the provided code). Also, there is the part ofargs
- it really should be a variable declared inside ofshell
, and not passed as a parameter, since its value is not even used insideshell
.Don't know what's up with the rest of the code. It's superfluous considering your
main
function only calls onshell
.