簡體   English   中英

當我有指向它的指針時,如何在 C 中打印這個字符串?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM