Re: [问题] 从struct 印出字串

楼主: MasterChang (我爱ASM)   2017-09-28 20:29:43
※ 引述《SKTP (Yi)》之铭言:
: 问题(Question):
: 我想印出结构中的一个字串,可是却一直跑出??,非预期的答案
: 程式码(Code):(请善用置底文网页, 记得排版)
其实不知道怎么跟你讲,因为满多低级错误的。
: 我预期的结果应该是%s的地方应该是ABC,可是显示的却是??
: 请问我的观念错在哪,请前辈们告知一下
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct A{
char *element;
struct QElement* next;
}QElement;
QElement* head = NULL;
QElement* tail = NULL;
void enq(char *data);
int main()
{
char *input ="ABC";
enq(input);
return 0;
}
void enq(char *data) {
QElement* temp = (QElement*)malloc(sizeof(QElement));
temp->element = (char*)malloc(sizeof(strlen(data)));
if(temp->element != NULL)
{
strcpy(temp->element,data);
printf("Element: %s is enqueued\n", temp->element);
}
temp->next = NULL;
free(temp->element);
free(temp);
}

Links booklink

Contact Us: admin [ a t ] ucptt.com