C-字符推送和弹出操作 [英] C- Character Push and Pop operation

查看:55
本文介绍了C-字符推送和弹出操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Stack create(int c)
{
    Stack S=(Stack)malloc(sizeof(struct stack));

    S->size=c;
    S->top=-1;
    S->array=(char *)malloc(sizeof(char)*c);

    return S;
}

Stack makeEmpty(void)
{
    Stack *S1=create(100);
    S1[0]->top=-1;
    return S1;
}

char pop(Stack S)
{
    return S->array[S->top--];
};

int main(void)
{
    Stack *S1;
    S1=makeEmpty();
    int j;
    int k=0;
    char result[30];
    for(j=0; j<2; j++)
    {
        char result1=pop(S1);
        strcat(result, result1);
        k++;
    }
}

我跳过了某些部分,例如 typedef struct stack Stack ;
我想做的是在for循环工作时从堆栈中弹出元素。然后,将这些元素存储在一个新数组中,该数组为 result 。为了检查它是否有效,我打印了出来,但是遇到运行时错误。如何存储元素以及如何将其打印出来?

I skipped some parts, like typedef struct stack Stack; What I wanted to do was pop out elements from the stack while for-loop works. Then, store those elements in a new array which is result. To check whether it works or not, I printed out but I had a runtime error. How to store the element and how to print it out?

推荐答案

我已经复制并粘贴了代码,并将其粘贴没有被编译。我认为
您要么不发布您的实际代码,要么不费心编译和阅读编译器警告。帮助您相当困难。在这里,我注意到了一些事情:

I've made copy&paste of your code, and it doesn't get compiled. I think that you are either not posting your actually code nor you don't bother to compile and read the compiler warnings. It's rather difficult to help you. Here some things I noticed:

1。
create 必须返回指向 Stack 而不是对象的指针。

1. create must return a pointer to Stack, not the object.

Stack *create(int c)
{
    Stack *S = malloc(sizeof *S);

    S->size=c;
    S->top=-1;
    S->array = malloc(c);

    return S;
}

2。
makeEmpty

Stack *makeEmpty(void)
{
    Stack *S1=create(100);
    S1->top=-1;
    return S1;
}

3。
pop 应该获得指向 Stack 而不是对象的指针

3. pop should get a pointer to Stack, not the object

char pop(Stack *S)
{
    return S->array[S->top--];
};

在这里,您应该检查堆栈中是否有元素。 int pop(Stack * S,char * val)返回1并在$ b $上的 * val 上写入b成功,然后返回0,否则会更好。

Here you should check whether there are elements on your stack. int pop(Stack *S, char *val) where it returns 1 and writes on *val on success, and returns 0 otherwise would be better.

4。
从您的流行音乐来看,您仅在推送 char 。我不明白您
正在尝试使用 strcat 做什么。无论哪种方式,您都在执行 strcat 错误。您
声明了一个包含100个空格的堆栈,但您只声明了30个空格
表示结果。如果堆栈中有31个以上的元素怎么办?我知道
您仅检查2个元素,但是很容易忽略,并且
对其进行扩展以遍历所有堆栈,而不会更改 result <的内存要求
/ code>。

4. Judging from your pop you are pushing char only. I don't get what you are trying to do with strcat. Either way, you are doing strcat wrong. You are declaring a stack with 100 spaces, but you are only declaring 30 spaces for result. What if you have more than 31 elements on your stack? I know that you are only inspecting 2 elements but it's easy to overlook that and expand it to go through all the stack without changing the memory requirements for result.

strcat 是可与C字符串一起使用的函数,这意味着它期望
C字符串。 C字符串必须必须 \0 终止,而不是。您有
类似C字符串的东西,但实际上不是。如果您坚持使用
strcat ,则应这样做:

Also strcat is a function that works with C-Strings, that means it expects C-Strings. A C-String must be \0 terminated, yours are not. You have something that looks like a C-String but it's not. If you insist on using strcat, the you should do it like this:

for(j=0; j<2; j++)
{
    char result1[] = { pop(S1), 0 };
    strcat(result, result1);
}

这篇关于C-字符推送和弹出操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆