user-0 提问: 大神们可以帮我看看这个strcpy出了什么问题吗

回复:
user: user-1
content: 非字符串还是用memcpy或结构体赋值吧

user: user-2
content: strcpy是对字符串进行拷贝的，第二个参数必须是字符串，即以'\0'结束的字符串； memcpy可以对任何数据进行拷贝；

user: user-3
content: 出错了中断下来，分析原因，看是不是访问了无效指针等

user: user-4
content: 字符数组等同于字符串

user: user-0
content: 引用 2 楼 hzy_76 的回复:HuffmanCode HC到底是个什么结构，你要搞清楚。strcpy要求两个参数都是char *。那我cd[start]只是一个普通的数组怎么使用strcpy呢，那个……我的结构体定义在楼下

user: user-0
content: typedef struct { 	int weight; 	int Parent,Lchild,Rchild; }HTNode; typedef HTNode HuffmanTree[100];          typedef char *HuffmanCode[100]; 这是定义的结构体emmmm……

user: user-4
content: HuffmanCode HC到底是个什么结构，你要搞清楚。strcpy要求两个参数都是char *。

user: user-5
content: #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; }