0

int 型と char 型のデータを保持するリンク リストを作成しました。関数はデータをリストに追加し、もう一方はそれを出力します。int 型のみを印刷すると問題は発生しませんが、char 型も印刷しようとするとプログラムがクラッシュします。

したがって、印刷関数 print_list() で char* を定義する方法を実行する必要があります。

より具体的には、私の問題はここにあります print_list() :

printf("\n [%s] \n", ptr -> name);
printf("\n [%s] \n", ptr -> lastn);

したがって、私の実際のコードは次のとおりです(エラーと警告は0ですが、プログラムはクラッシュします):

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

// Creating structure for node 
struct test_struct 
{ 
    int val;         // val is member id number 
    char name; 
    char lastn; 
    int age; 
    struct test_struct *next; 
}; 

// declaring global head and curr pointers 
struct test_struct *head = NULL; 
struct test_struct *curr = NULL; 

// creating a list 
struct test_struct* create_list(int val, char* name, char* lastn, int age) 
{ 
    printf("\n creating list with head node as [%d] [%s] [%s] [%d] \n", val, name, lastn, age); 

    struct test_struct *ptr = malloc(sizeof(struct test_struct)); // creating list 
    if(NULL == ptr) { 
        printf("\n Node creation failed \n"); 
        return NULL; 
    } 

    ptr->val = val; 
    ptr->name = *name; 
    ptr->lastn = *lastn; 
    ptr->age = age; 
    ptr->next = NULL; 

    head = curr = ptr; 

    return ptr; 
}

// add member to list 
struct test_struct* add_to_list(int val, char *name, char *lastn, int age, bool add_to_end) 
{ 
    if(NULL == head) { 
        return (create_list(val, name, lastn, age)); 
    }      

    if(add_to_end) { 
        printf("\n Adding node to end of list with data [%d] [%s] [%s] [%d] \n",  val, name, lastn, age); 
    } else { 
        printf("\n Adding node to beginning of list with data [%d] [%s] [%s] [%d] \n", val, name, lastn, age); 
    } 

    struct test_struct *ptr = malloc(sizeof(struct test_struct)); 

    if(NULL == ptr) { 
        printf("\n Node creation failed \n"); 
        return NULL; 
    } 

    ptr->val = val; 
    ptr->name = *name; 
    ptr->lastn = *lastn; 
    ptr->age = age; 
    ptr->next = NULL; 

    if (add_to_end) { 
        curr-> next = ptr; 
        curr = ptr; 
    } else { 
        ptr -> next = head; 
        head = ptr; 
    } 

    return ptr; 
} 

//printing the list 
void print_list(void) 
{ 
    struct test_struct *ptr = head; 

    printf("\n -----Printing list Start----- \n"); 

    while(ptr != NULL) { 
        printf("\n [%d] \n", ptr -> val); 
        printf("\n [%s] \n", ptr -> name); 
        printf("\n [%s] \n", ptr -> lastn); 
        printf("\n [%d] \n", ptr -> age); 
        ptr = ptr->next; 
    } 

    printf("\n -----Printing list end---- \n"); 

    return; 
}   

// main function 
int main(void) 
{ 
    struct test_struct *ptr = NULL; 

    // for adding member to list 
    add_to_list(123, "william", "shakespeare", 30, true); 
    add_to_list(124, "william", "gibson", 35, true); 
    add_to_list(125, "chuck", "palahniuk", 40, true); 
    add_to_list(126, "mario", "puzio", 50, true); 
    add_to_list(127, "umberto", "eco", 60, true); 
    add_to_list(128, "ezra", "pound", 125, true); 

    print_list(); 

    return 0; 
}  
4

2 に答える 2

1

name と lastn を 1 文字として宣言しました

struct test_struct
{
int val;         // val is member id number
char name;
char lastn;
int age;
struct test_struct *next;
};

文字列を保持するために割り当てられたスペースを指す固定サイズの配列またはポインターとして宣言する必要があります。文字列は、\0 で終わる一連の文字です。

struct test_struct
{
int val;         // val is member id number
char name[MAXLEN];
char lastn[MAXLEN];
int age;
struct test_struct *next;
};

次に、関数への引数を構造体のフィールドにコピーします

例えば

strcpy(ptr->name,name);
strcpy(ptr->lastn,lastn);
于 2015-09-24T11:51:06.370 に答える