0

このコードがセグメンテーション違反を返す理由を理解しようとしています。strcpy(newNode -> data -> ticket_name, name);Gdb は、 function で言う障害を私に与える行を言いますstrcat()。何か案は?また、私は C に非常に慣れていないことを指摘したいと思います。

void add_ticket(tm_type * tm)
{
    char name[TICKET_NAME_LEN+1], type, zone[TICKET_ZONE_LEN], buffer[5], *ptr, *pos;
    int price;
    BOOLEAN check = TRUE;
    printf("Add Ticket\n");
    printf("----------\n");
    printf("\n");
    printf("Ticket name (1-40 characters): ");
    if(fgets(name, sizeof(name), stdin) != NULL){
        if(name[0] != '\n'){
            if((pos=strchr(name, '\n')) != NULL){
                *pos = '\0';
            }

            printf("\nTicket type (1 character): ");
            type = fgetc(stdin);
            if(type != '\n'){

                read_rest_of_line();
                printf("\nTicket zone (1-3 characters): ");
                if(fgets(zone, sizeof(zone), stdin) != NULL){
                    if(zone[0] != '\n'){
                        if((pos=strchr(zone, '\n')) != NULL){
                            *pos = '\0';
                        }

                        printf("\n\n");
                        printf("Price (in cents): ");
                        if(fgets(buffer, sizeof(buffer), stdin) != NULL){
                            price = strtol(buffer, &ptr, 10);
                            if(buffer[0] != '\n'){

                                //create new node
                                stock_node *newNode = (stock_node*)malloc(sizeof(stock_node));

                                if(newNode == NULL){
                                    fprintf(stderr, "Unable to allocate memory for new ticket.\n");
                                    exit(EXIT_FAILURE);
                                }

                                strcpy(newNode -> data -> ticket_name, name);
                                newNode -> data -> ticket_type = type;
                                strcpy(newNode -> data -> ticket_zone, zone);
                                newNode -> data -> ticket_price = price;
                                newNode -> data -> stock_level = DEFAULT_STOCK_LEVEL;
                                newNode -> next_node = NULL;

                                //check for first insertion
                                if(tm -> stock -> head_stock -> next_node == NULL){
                                    tm -> stock -> head_stock -> next_node = newNode;
                                    printf("First ticket added.\n");
                                }

                                else{
                                    //else loop through the list and find the last
                                    //node, insert next to it
                                    stock_node *stream = tm -> stock -> head_stock;
                                    while(check == TRUE){

                                        if(stream -> next_node == NULL){
                                            stream -> next_node = newNode;
                                            printf("Ticket added.\n");
                                            check = FALSE;
                                        }
                                        stream = stream -> next_node;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
4

1 に答える 1