C语言大小不匹配,为何sizeof出来大小不对

C/C++ code#include

#include

#include

int main()

{

struct jb{

char actor[25];

struct jb *next;

};

struct jb *bond;

int n;

/* Create the first structure in the list */

bond = (struct jb *)malloc(sizeof(struct jb));

printf("struct jb sizeof is %d\n", n = sizeof(struct jb));

printf("struct bond->actor sizeof is %d\n", n = sizeof(bond->actor));

printf("struct bond->next sizeof is %d\n", n = sizeof(bond->next));

/* Fill the structure */

strcpy(bond->actor, "Sean Connery");

bond->next = NULL; /* End of list */

/* Display the results */

printf("The first structure has been created:\n");

printf("\tbond->actor = %s\n", bond->actor);

printf("\tnext structure address = %p\n", bond->next);

return(0);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
栈是一种常用的数据结构,可以用于括号匹配的问题。下面是一个简单的C语言代码示例: ``` #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define STACK_INIT_SIZE 100 // 栈的初始大小 #define STACKINCREMENT 10 // 栈的增量 typedef char ElemType; typedef struct { ElemType *top; // 栈顶指针 ElemType *base; // 栈底指针 int stacksize; // 当前已分配的存储空间,以元素为单位 } SqStack; // 初始化栈 bool InitStack(SqStack *s) { s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType)); if (!s->base) { exit(1); // 存储分配失败 } s->top = s->base; s->stacksize = STACK_INIT_SIZE; return true; } // 获取栈顶元素 bool GetTop(SqStack s, ElemType *e) { if (s.top == s.base) { return false; // 栈为空 } *e = *(s.top - 1); return true; } // 压栈操作 bool Push(SqStack *s, ElemType e) { // 栈满,追加存储空间 if (s->top - s->base >= s->stacksize) { s->base = (ElemType *)realloc(s->base, (s->stacksize + STACKINCREMENT) * sizeof(ElemType)); if (!s->base) { exit(1); // 存储分配失败 } s->top = s->base + s->stacksize; s->stacksize += STACKINCREMENT; } *(s->top++) = e; return true; } // 弹栈操作 bool Pop(SqStack *s, ElemType *e) { if (s->top == s->base) { return false; // 栈为空 } *e = *(--s->top); return true; } // 判断是否匹配 bool Match(char a, char b) { if ((a == '(' && b == ')') || (a == '[' && b == ']') || (a == '{' && b == '}')) { return true; } else { return false; } } // 括号匹配函数 bool BracketMatch(char *str) { SqStack s; InitStack(&s); char ch; while (*str != '\0') { switch (*str) { case '(': case '[': case '{': Push(&s, *str); break; case ')': case ']': case '}': if (GetTop(s, &ch) && Match(ch, *str)) { // 匹配成功 Pop(&s, &ch); } else { // 匹配失败 return false; } break; default: break; } ++str; // 处理下一个字符 } if (s.top == s.base) { // 栈为空,匹配成功 return true; } else { // 栈非空,匹配失败 return false; } } int main() { char str[] = "{[(3+5)*2-1]+2}/2"; if (BracketMatch(str)) { printf("括号匹配成功\n"); } else { printf("括号匹配失败\n"); } return 0; } ``` 这段代码中定义了一个栈的结构体`SqStack`,并实现了栈的初始化、获取栈顶元素、压栈、弹栈等基本操作。在括号匹配函数`BracketMatch`中,遍历输入字符串中的每个字符,并将左括号压入栈中;当遇到右括号时,取出栈顶元素进行匹配,若匹配成功则弹出栈顶元素,否则返回匹配失败。最后判断栈是否为空,若为空则说明括号全部匹配成功,否则匹配失败。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值