c语言中 结构体中数组赋值问题

来源:百度知道 编辑:UC知道 时间:2024/06/14 08:07:52
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct hui{
char *data;
int top;
} *Hui;

int IsHui(char *s);

void main() {
char c[100];
printf("Enter:\n");
gets(c);
if(IsHui(c))
printf("Is\n");
else
printf("NO\n");
}

int IsHui(char *s) {
int n = strlen(s);
Hui S = (Hui)malloc((n/2)*sizeof(char)+sizeof(int));
S->top = -1;

for(int i = 0; i < n/2; i++) {
S->top++;
S->data[i] = s[i];//这一步使程序停止????为什么???

}
char temp;
i = i - 1;
while(S->top>=0) {
temp = S->data[i];
if(temp != s[n-i-1])
return 0;
i--;S->top--;
}
return 1;
}

这是我的回文游戏.为什么S->data[i] = s[i];这一步对结构体中的数组赋值运行不了???晕~~~

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct hui{
char data[20]; //你写的是指针,要开数组的话必须用malloc开空间,我是
//用直接改为数组了。
int top;
} *Hui;

int IsHui(char *s);

void main() {
char c[100];
printf("Enter:\n");
gets(c);
if(IsHui(c))
printf("Is\n");
else
printf("NO\n");
}

int IsHui(char *s) {
int n = strlen(s);
Hui S = (Hui)malloc((n/2)*sizeof(char)+sizeof(int));
S->top = -1;

for(int i = 0; i < n/2; i++) {
S->top++;
S->data[i] = s[i];//这一步使程序停止????为什么???

}
char temp;
i = i - 1;
while(S->top>=0) {
temp = S->data[i];
if(temp != s[n-i-1])
return 0;
i--;S->top--;
}
return 1;
}

typedef struct hui{
char data[10];
int top;
} *Hui;