user-0 提问: C语言malloc与结构体调用

回复:
user: user-0
content: 引用 8 楼 真相重于对错的回复:printf("\n%d\n", ZA1->data);//这里还没有给data赋值，就要打印它，会出错 ZA1->data = ZA; 谢谢回复，已解决

user: user-0
content: 引用 7 楼 赵4老师的回复:#include<stdio.h> #include<stdlib.h> #include<string.h>  typedef struct MyString {     unsigned char* data;     int size; } MyString;  char* ConvertStringAsHex(MyString*);  //将字符串转换为16进制数串 char* ConvertStringAsHex(MyString* s) {     char* result = (char*)malloc(sizeof(char) * (s->size * 2 + 1));     for (int i = 0; i < s->size; i++) {         sprintf(&result[i * 2], "%02X", s->data[i]);     }     result[s->size * 2] = '\0';     return result; }  int main() {     char *ID = "aaabbb";     char *ENTLA = "123";     unsigned char *ZA = (unsigned char*)malloc(sizeof(unsigned char)*strlen(ENTLA) + strlen(ID) + 1);     memcpy(ZA, ENTLA,strlen(ENTLA));     memcpy(ZA+strlen(ENTLA), ID, strlen(ID));     ZA[strlen(ENTLA) + strlen(ID)] = '\0';     printf("ZA:[%s]\n",ZA);     MyString *ZA1 = (MyString*)malloc(sizeof(MyString));     ZA1->size = strlen((char *)ZA);     printf("strlen(ENTLA) + strlen(ID):%d\n", strlen(ENTLA) + strlen(ID));     ZA1->data = ZA;     printf("ZA1->data:[%s]\n",ZA1->data);     char *ZAstring = ConvertStringAsHex(ZA1);     printf("ZAstring:[%s]\n", ZAstring);     free(ZAstring);     free(ZA1);     free(ZA);     printf("End\n");     return 0; } //ZA:[123aaabbb] //strlen(ENTLA) + strlen(ID):9 //ZA1->data:[123aaabbb] //ZAstring:[313233616161626262] //End //  谢谢您，可算是懂了

user: user-1
content: printf("\n%d\n", ZA1->data);//这里还没有给data赋值，就要打印它，会出错 ZA1->data = ZA;

user: user-2
content: #include<stdio.h> #include<stdlib.h> #include<string.h>  typedef struct MyString {     unsigned char* data;     int size; } MyString;  char* ConvertStringAsHex(MyString*);  //将字符串转换为16进制数串 char* ConvertStringAsHex(MyString* s) {     char* result = (char*)malloc(sizeof(char) * (s->size * 2 + 1));     for (int i = 0; i < s->size; i++) {         sprintf(&result[i * 2], "%02X", s->data[i]);     }     result[s->size * 2] = '\0';     return result; }  int main() {     char *ID = "aaabbb";     char *ENTLA = "123";     unsigned char *ZA = (unsigned char*)malloc(sizeof(unsigned char)*strlen(ENTLA) + strlen(ID) + 1);     memcpy(ZA, ENTLA,strlen(ENTLA));     memcpy(ZA+strlen(ENTLA), ID, strlen(ID));     ZA[strlen(ENTLA) + strlen(ID)] = '\0';     printf("ZA:[%s]\n",ZA);     MyString *ZA1 = (MyString*)malloc(sizeof(MyString));     ZA1->size = strlen((char *)ZA);     printf("strlen(ENTLA) + strlen(ID):%d\n", strlen(ENTLA) + strlen(ID));     ZA1->data = ZA;     printf("ZA1->data:[%s]\n",ZA1->data);     char *ZAstring = ConvertStringAsHex(ZA1);     printf("ZAstring:[%s]\n", ZAstring);     free(ZAstring);     free(ZA1);     free(ZA);     printf("End\n");     return 0; } //ZA:[123aaabbb] //strlen(ENTLA) + strlen(ID):9 //ZA1->data:[123aaabbb] //ZAstring:[313233616161626262] //End //

user: user-0
content: 引用 3 楼 真相重于对错的回复:贴代码，不要贴图 代码贴评论区了，拜托指点

user: user-0
content: 这是源代码 #ifndef MYSTRING_H #define MYSTRING_H  typedef struct MyString { 	unsigned char* data; 	int size; }MyString;  char* ConvertStringAsHex(MyString*);  #endif  #include<stdio.h> #include<stdlib.h> #include "MyString.h"  //将字符串转换为16进制数串 char* ConvertStringAsHex(MyString* s) { 	char* result = (char*)malloc(sizeof(char) * (s->size * 2 + 1)); 	for (int i = 0; i < s->size; i++) { 		sprintf_s(&result[i * 2], "%02X", s->data[i]); 	} 	result[s->size * 2] = '\0'; 	return result; }   void main() { char *ID = "aaabbb"; char *ENTLA = "123"; unsigned char *ZA = (unsigned char*)malloc(sizeof(unsigned char)*strlen(ENTLA) + strlen(ID) + 1); memcpy(ZA, ENTLA,strlen(ENTLA)); memcpy(ZA+strlen(ENTLA), ID, strlen(ID)); ZA[strlen(ENTLA) + strlen(ID)] = '\0'; printf("\n%s",ZA); Mystring *ZA1 = (Mystring*)malloc(sizeof(Mystring)); ZA1-> = strlen(ZA); printf("\n%d\n", strlen(ENTLA) + strlen(ID)); printf("\n%d\n", ZA1->data); ZA1->data = ZA; printf("%s\n",ZA1->data); char *ZAstring = ConvertStringAsHex(ZA1); printf("%s\n", ZAstring); free(ZA1->data); free(ZA1); free(ZA); }

user: user-0
content: 引用 1 楼 赵4老师的回复:没有printf输出语句，当然没有输出结果。 在 free(ZA1->data); 前加一句 printf("%s\n",ZAstring); 抱歉，截图误导了，添加过printf，运行不出来

user: user-1
content: 贴代码，不要贴图

user: user-0
content: 引用 楼主 weixin_45710571的回复:求助， 调用结构体后为什么无法输出ZAstring字符串结果？编译通过的     抱歉，截图误导了，添加过printf，运行不出来

user: user-2
content: 没有printf输出语句，当然没有输出结果。 在 free(ZA1->data); 前加一句 printf("%s\n",ZAstring);