不确定这段代码发生了什么

发布于 2024-11-27 14:51:50 字数 2197 浏览 1 评论 0原文

我有一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

時窥 2024-12-04 14:51:50

当您使用 %s 格式说明符时,它期望一个值,该值是指向字符串开头的指针。在 C 中,此类型为 char *

使用您的 main 函数,您的变量 s1 的类型为 char *。因此,s1printf 的有效参数,因此这一行是有效的:

printf("%s\n", s1);

请注意 & 缺少 > 在 s1 前面。在您的代码中,您使用了 &,它采用 s1 的地址,其结果将是 char ** 类型。这是错误类型,因此不要使用&

问题是, printf 实际上无法判断其参数是什么类型,因为它是一个可变参数函数。它只是根据格式字符串中指定的类型使用存在的任何参数。

scanf 也是如此,但有一个陷阱:您必须确保分配足够的内存来考虑用户输入,否则您将遇到缓冲区溢出并产生不可预测的结果。除此之外,printfscanf 是完美互补的。

无论如何,除了未使用的 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 is char *.

Taking your main function, your variable s1 is of type char *. Therefore, s1 is a valid parameter to printf, so this line is valid:

printf("%s\n", s1);

Note the absence of an & in front of s1. In your code, you used the &, which takes the address of s1, the result of which will be of type char **. 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 and scanf 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 of args - it really should be a variable declared inside of shell, and not passed as a parameter, since its value is not even used inside shell.

Don't know what's up with the rest of the code. It's superfluous considering your main function only calls on shell.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文