[英]How to print this string in C when I have the pointer to it?
我是 Java 開發人員,我正在學習 C,我想打印一個string
,但它不起作用。 我發現與 Java 相比,在 C 中打印string
非常困難。
這是代碼:
static struct t_node* create_tnode(char* n) {
// Assigning memory to struct variable temp
struct t_node *temp=(struct t_node * )malloc(sizeof(struct t_node ));
printf("name = %s\n", temp->name);
// Assigning value to name variable of temp using arrow operator
temp->name=n;
temp->next_dfile=NULL;
temp->next_file=NULL;
return temp;
}
打印這個temp->name
不起作用,然后我嘗試以這種方式打印*temp->name
或像這個&temp->name
並且不起作用。 還有一個問題,這個結構是返回 temp var 還是指向 temp 的指針? 先感謝您!
您應該在為其賦值后打印該變量。 這將起作用,
static struct t_node* create_tnode(char* n) {
// Assigning memory to struct variable temp
struct t_node *temp=(struct t_node * )malloc(sizeof(struct t_node ));
// Assigning value to name variable of temp using arrow operator
temp->name=n;
printf("name = %s\n", temp->name);
temp->next_dfile=NULL;
temp->next_file=NULL;
return temp;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.