大神们可以帮我看看这个strcpy出了什么问题吗

毛闪闪icon 2017-11-12 01:00:02
void CodingHuffmanTree(HuffmanTree HT,HuffmanCode HC,int n)
{
int C,P,i;
int start;
char cd[100];
cd[n]='\0';
for(i=1;i<=n;i++)
{
start=n;
C=i;
P=HT[C].Parent;
while(P>0) //????打括号为什么会错呢
{
if(HT[P].Lchild==C)
cd[--start]='0';
else
cd[--start]='1';
C=P;
P=HT[C].Parent;
} //????在之前没有编出来的区域变成了什么
strcpy(HC[i],&cd[start]); //复制不出来
}
printf("\n");
}

是一个给编好的哈夫曼树附加前缀码的函数,这里的问题就出现在strcpy这里,我不知道怎么改了
...全文
178 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
heronism 2017-11-13
  • 打赏
  • 举报
回复
非字符串还是用memcpy或结构体赋值吧
自信男孩 2017-11-13
  • 打赏
  • 举报
回复
strcpy是对字符串进行拷贝的,第二个参数必须是字符串,即以'\0'结束的字符串; memcpy可以对任何数据进行拷贝;
paschen 2017-11-12
  • 打赏
  • 举报
回复
出错了中断下来,分析原因,看是不是访问了无效指针等
老马何以识途 2017-11-12
  • 打赏
  • 举报
回复
字符数组等同于字符串
毛闪闪icon 2017-11-12
  • 打赏
  • 举报
回复
引用 2 楼 hzy_76 的回复:
HuffmanCode HC到底是个什么结构,你要搞清楚。strcpy要求两个参数都是char *。
那我cd[start]只是一个普通的数组怎么使用strcpy呢,那个……我的结构体定义在楼下
毛闪闪icon 2017-11-12
  • 打赏
  • 举报
回复
typedef struct
{
	int weight;
	int Parent,Lchild,Rchild;
}HTNode;
typedef HTNode HuffmanTree[100];         
typedef char *HuffmanCode[100];
这是定义的结构体emmmm……
老马何以识途 2017-11-12
  • 打赏
  • 举报
回复
HuffmanCode HC到底是个什么结构,你要搞清楚。strcpy要求两个参数都是char *。
destory27 2017-11-12
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <windows.h>
#define MaxValue 512
#define Maxbit 16

typedef struct {
char chara;
int weight;
int flag;
int parent;
int leftchild;
int rightchild;
}haffnode;

typedef struct{
int bit[Maxbit];
int weight;
int start;
}haffcode;

int my_stdin(void)
{
lb1:fprintf(stderr, "%s", "input num :");
char str[1024];
fgets(str, 1024, stdin);

int len = strlen(str);
for(len-1; isspace(str[len-1]) && len >= 0; str[(len--)-1] = '\0')
;

char *p[1024];
int i = 0;
int j = 0;
while(str[i]){
while(isspace(str[i]) && str[i])
++i;

p[j++] = &str[i++];
while(isalnum(str[i]) && str[i])
++i;

str[i] = '\0';
++i;

}

if(j != 1){
fprintf(stderr, "%s\n", "Input error!");
fflush(stdin);
goto lb1;
}

i = 0;
int flag = 1;
while(p[0][i]){
if(isdigit(p[0][i]))
++i;
else {
flag = 0;
break;
}

}

if(flag == 0){
fprintf(stderr, "%s\n", "Input error!");
fflush(stdin);
goto lb1;
}

int num = atoi(p[0]);
return num;

}



void creat_hafftree(int n, haffnode *hafftree)
{
int i = 0;
for(; i < 2*n-1; ++i){
if(i >= n)
hafftree[i].weight = 0;
hafftree[i].flag = 0;
hafftree[i].parent = -1;
hafftree[i].leftchild = -1;
hafftree[i].rightchild = -1;
}

for(i = 0; i < n-1; ++i){
int m1 = MaxValue;
int m2 = MaxValue;
int x1 = 0;
int x2 = 0;
for(int j = 0; j < n+i; ++j){
if(hafftree[j].weight < m1 && hafftree[j].flag == 0){
m2 = m1;
x2 = x1;
m1 = hafftree[j].weight;
x1 = j;
}
else if(hafftree[j].weight < m2 && hafftree[j].flag == 0){
m2 = hafftree[j].weight;
x2 = j;
}
}

hafftree[x1].parent = n+i;
hafftree[x2].parent = n+i;
hafftree[x1].flag = 1;
hafftree[x2].flag = 1;
hafftree[n+i].weight = hafftree[x1].weight + hafftree[x2].weight;
hafftree[n+i].leftchild = x1;
hafftree[n+i].rightchild = x2;

}

for(i = 0; i < 2*n-1; ++i){
printf("%d %2d %2d %2d %2d %2d \n", i, hafftree[i].weight,\
hafftree[i].flag, hafftree[i].parent, hafftree[i].leftchild, hafftree[i].rightchild);
}


return ;
}

void haff_code(int n, haffnode *hafftree, haffcode ** code)
{
int i= 0;
for(; i < n; ++i){
code[i] = (haffcode *)malloc(sizeof(haffcode));
(haffcode *)memset(code[i], 0x00, sizeof(haffcode));
code[i]->weight = hafftree[i].weight;
code[i]->start = n-1;
int child = i;
int parent = hafftree[child].parent;

while(parent != -1){
if(hafftree[parent].leftchild == child)
code[i]->bit[code[i]->start--] = 0;
else
code[i]->bit[code[i]->start--] = 1;

child = parent;
parent = hafftree[child].parent;
}

}


return;
}

int main(void)
{
system("cls");
fprintf(stderr, "%s\n", "To the number of coding.");
system("color 04");
Sleep(1000);
system("color 0A");
int n;
n = my_stdin();
system("cls");
haffnode hafftree[2*n-1];
int i = 0;
for(; i < n; ++i){
fprintf(stderr, "%s%d%s", "hafftree[", i, "].chara:");
fflush(stdin);

hafftree[i].chara = getchar();

fprintf(stderr, "%s%d%s\n", "hafftree[", i, "].weight.");
system("color 04");
Sleep(1000);
system("color 0A");
fflush(stdin);

hafftree[i].weight = my_stdin();

}

creat_hafftree(n, hafftree);
haffcode *code[n];
haff_code(n, hafftree, code);

for(i = 0; i < n; ++i){
printf(" chara = %c weight = %d ", hafftree[i].chara, code[i]->weight);
printf("code = ");
int j;
for(j = code[i]->start+1; j < n; ++j)
printf("%d", code[i]->bit[j]);
printf("\n");
}



return 0;
}

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧