ฉันจะเรียกสมาชิกของ struct ที่ได้รับการเติมโดยใช้ฟังก์ชันตัวชี้ struct ได้อย่างไร

ฉันต้องการทราบวิธีพิมพ์ค่า char* wordfrom โครงสร้างนี้

  struct word_count_struct {
  char *word;
  int   count;
};

โครงสร้างนี้ได้รับค่าที่คิดว่าเป็นฟังก์ชัน

struct word_count_struct *new_word_count( char *word )
{
    struct word_count_struct *new_word_count = (struct word_count_struct *)malloc(sizeof(struct word_count_struct));

    new_word_count->word = "Hello";
    new_word_count->count = 1; 

    return new_word_count;    
}

เมื่อฉันใช้ word_count_struct new_word_count( &word ); หรือ new_word_count( &word ); เพื่อเรียกใช้ฟังก์ชันและ puts(new_word_count->word); เพื่อพิมพ์สมาชิกจาก main.c

ฉันได้รับข้อความแสดงข้อผิดพลาด

error: called object ‘new_word_count’ is not a function or function pointer

และ

note: declared here
     struct word_count_struct *new_word_count;

สิ่งที่ฉันไม่แน่ใจเป็นพิเศษคือการเรียกใช้ฟังก์ชัน ฉันรู้ในฟังก์ชันอื่นๆ โดยทั่วไปแล้วฉันจะใช้โค้ด function_name(passing arguments); แต่ฟังก์ชันนี้ส่งคืนตัวชี้ struct และดูเหมือนว่าจะมีสองชื่อภายในลายเซ็นฟังก์ชัน

ถ้าฉันไม่ใช้ฟังก์ชันเพื่อกำหนดค่าให้กับสมาชิก struct ฉันสามารถพิมพ์ค่าของ new_word_count->word ภายใน main.c ได้ไม่มีปัญหา ดังนั้นฉันเชื่อว่าปัญหาของฉันต้องมาจากการเรียกใช้ฟังก์ชัน ขอบคุณล่วงหน้า.

ตัวอย่างขั้นต่ำ ครบถ้วน และตรวจสอบได้:

#include <stdio.h>
#include <stdlib.h>

struct word_count_struct {
  char *word;
  int count;
};

struct word_count_struct *new_word_count( char *word );

int main()
{
    struct word_count_struct *wordcountptr;
    char *word = "Hello";

    wordcountptr = new_word_count(word);
    puts(wordcountptr->word); /* print out the string "Hello" which was assigned to this struct member though the word_count_struct function */
    free(wordcountptr);
    return 0;
}

/* this function should take the value of the passing argument and assign it to the word member of struct word_count_struct and then return a pointer to that struct */
struct word_count_struct *new_word_count( char *word )
{
    struct word_count_struct *new_word_count;
    new_word_count = (struct word_count_struct *)malloc(sizeof(struct word_count_struct)); /* allocate memory for struct */

    new_word_count->word = &word; /* assign the memory address of passing argument to this member */
    new_word_count->count = 1;

    return new_word_count;

}

person Tyler Green    schedule 11.02.2018    source แหล่งที่มา
comment
word_count_struct new_word_count( &word ); ไม่ใช่รหัส C คุณตั้งใจจะเขียน word_count_struct *mystruct = new_word_count( &word ); หรือไม่ โปรดแสดงตัวอย่างที่ทำซ้ำได้น้อยที่สุด คำถามของคุณค่อนข้างไม่ชัดเจน   -  person Jabberwocky    schedule 11.02.2018


คำตอบ (1)


คุณอาจต้องการสิ่งนี้:

struct word_count_struct *wordcountptr;
char *word;
wordcountptr = new_word_count(word);
puts(wordcountptr->word);

โค้ดนี้คอมไพล์และรันแต่ก็ไร้ประโยชน์ไม่มากก็น้อยเนื่องจากฟังก์ชัน new_word_count ยังไม่เสร็จสิ้น

คำถามมีการเปลี่ยนแปลง...

ปัญหาอยู่ที่นี่แล้ว:

คุณต้องการสิ่งนี้:

new_word_count->word = word;

แทน

new_word_count->word = &word;

BTW คอมไพเลอร์ของคุณควรเตือนคุณ

ฟังก์ชั่นที่สมบูรณ์:

struct word_count_struct *new_word_count(char *word)
{
  struct word_count_struct *new_word_count;
  new_word_count = (struct word_count_struct *)malloc(sizeof(struct word_count_struct));

  new_word_count->word = word;
  new_word_count->count = 1;

  return new_word_count;
}

แต่ด้วยวิธีนี้ คุณมักจะประสบปัญหาอื่นๆ เนื่องจากคุณเก็บเฉพาะ ที่อยู่ ของคำที่จะนับ แต่คุณอาจต้องการเก็บคำนั้นเองมากกว่า แทน:

new_word_count->word = word;

คุณอาจต้องการ:

new_word_count->word = malloc(strlen(word) + 1);  // allocate memory for word
strcpy(new_word_count->word, word);               // copy the word
person Jabberwocky    schedule 11.02.2018
comment
ฉันได้ทำการเปลี่ยนแปลงและเปลี่ยนโค้ดของฉันและดำเนินการแล้ว ฉันได้รับข้อผิดพลาดในการแบ่งส่วน คุณช่วยชี้ให้ฉันดูหน่อยได้ไหมว่าทำไมรหัสถึงยังไม่เสร็จ? ฉันคิดว่าฉันมีฟังก์ชั่นที่สมบูรณ์แล้ว - person Tyler Green; 11.02.2018
comment
@TylerGreen 1. new_word_count ไม่ได้ทำอะไรที่เป็นประโยชน์เลย โดยเฉพาะคุณไม่ได้ใช้ wordparameter เลย "Hello"2 คืออะไร คุณต้องแสดงตัวอย่างที่ทำซ้ำได้น้อยที่สุด ฉันไม่สามารถเดาได้ว่าคุณใช้โค้ดใดกันแน่ คุณแสดงเฉพาะส่วนของโค้ดในคำถามของคุณ และบ่อยครั้งที่ปัญหาจริงอยู่ที่ส่วนอื่นของโค้ด - person Jabberwocky; 11.02.2018
comment
ฉันได้เพิ่มความคิดเห็นเล็กน้อยในตัวอย่างขั้นต่ำเพื่ออธิบายเป้าหมายสุดท้ายได้ดีที่สุด - person Tyler Green; 11.02.2018
comment
@ไมเคิล วอลซ์. ขอขอบคุณสำหรับความช่วยเหลือ! คำถาม: ฉันคิดว่าเมื่อเรียกใช้ฟังก์ชันที่ต้องใช้ตัวชี้เป็นพารามิเตอร์ ฟังก์ชันจะต้องได้รับที่อยู่หน่วยความจำเป็นอาร์กิวเมนต์ที่ส่งผ่าน ในไฟล์นี้ ฉันได้ใช้ฟังก์ชันที่ส่งผ่านตัวชี้คู่และ &word ทำงาน เหตุใด &word จึงไม่ทำงานกับพารามิเตอร์ตัวชี้ตัวเดียว - person Tyler Green; 11.02.2018
comment
@TylerGreen ฉันคิดว่านั่นเป็นผู้สมัครสำหรับคำถามอื่น - person Jabberwocky; 11.02.2018