C编码:我是否从一个节点错误地打印了这个数组?

C编码:我是否从一个节点错误地打印了这个数组?,c,pointers,linked-list,nodes,C,Pointers,Linked List,Nodes,我正在用给定的int值、char字符串和数组输入制作一个链表。出于某种原因,尽管从打印函数打印时数组的地址相同,但数组中的值发生了变化,但直接从函数打印时,我得到了正确的值等。第一组代码是我的主代码,下面一组是输入代码 #include <stdio.h> #include <stdlib.h> #include <string.h> #include "a03input.h" // Define the struct struct st

我正在用给定的int值、char字符串和数组输入制作一个链表。出于某种原因,尽管从打印函数打印时数组的地址相同,但数组中的值发生了变化,但直接从函数打印时,我得到了正确的值等。第一组代码是我的主代码,下面一组是输入代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "a03input.h"

// Define the struct
struct stNode {

    double avg;
    char* name;
    int* prodID;
    struct stNode *next;

};//stNode

typedef struct stNode Node;

// Implement the following function
void  createList(Node **head, Node **tail){

    Node* oTmp;
    Node* tmp;
    int index = 0;
    int shiftTo;

    for(int i = 0;i < INPUTSIZE; i++){

        oTmp = malloc(sizeof(Node));
        oTmp -> avg = a03input_avg[i];
        oTmp -> name = a03input_name[i];
        int arr[a03input_num[i]];
        oTmp -> prodID= arr;
        shiftTo = index;

            for(int j = 0; j < a03input_num[i]; j++){

                arr[j]=a03input_prodIDs[shiftTo];
                shiftTo++;

        }

        index = index+a03input_num[i];
        oTmp -> next = NULL;

        if( *head == NULL){

            *head  = oTmp;
                }

                else{
                    tmp = *head;
                    while(tmp -> next != NULL){
                    tmp = tmp -> next;
            }

            tmp -> next = oTmp;
        }
    }
} // createList

// Implement the following function
void printList(Node *head){

    Node *cur;
    cur = head;
    int i = 0;

    while(cur != NULL){

        printf("%-12s", cur -> name);
        printf("%.2f ", cur -> avg);
        printf("[ ");
            for(int j = 0;j < a03input_num[i]; j++){

                printf(" %d", (cur -> prodID)[j]);

        }

        printf("] \n");
        cur = cur -> next;
        i++;

    }
} // printList

// Implement the following function
void freeList(Node **head, Node **tail) {

    Node *cur = *head;

        while(cur != NULL){

        free(cur);
        cur = cur -> next;

    }

} // freeList


// DO NOT change the main()
int main() {
    Node        *head = NULL;
    Node        *tail = NULL;

    createList(&head, &tail);
    printList(head);
    freeList(&head, &tail);

    return 0;
}

错误在这里:

int arr[a03input_num[i]];
oTmp -> prodID= arr;
数组
arr
createList
函数中是local。一旦函数返回
arr
的生存期结束,它就不再存在,任何指向它的指针都无效

您需要为每个节点动态分配它

The following is the output generated from the main code:

Alfred      99.12 [  1 0 1243544112]
Ben         48.00 [  1 0]
Catherine   13.00 [  1]
Dana        88.67 [  1 0]
Ether       19.00 [  8 0 1243544112 32766 1]
Frank       43.00 [  1]
Gab         99.34 [  1 0 1243544112 32766]
Harry       21.80 [  8 0 1243544112 32766 1]
Ian         5.12 [  1 0]
Johnny      88.01 [  1 0 1243544112 32766]
Kerry       0.00 [  8 0 1243544112 32766 1 0 1243544112 32766]
Leo         27.23 [  1 0]
Mike        98.45 [  1]
Nathan      65.12 [  8 0 1243544112 32766 1 0]
Olivia      0.00 [ ]

And this is the expected output:

Alfred     99.12 -> [ 1012 1031 1001 ]
Ben        48.00 -> [ 1002 1005 ]
Catherine  13.00 -> [ 1006 ]
Dana       88.67 -> [ 1008 9012 ]
Ether      19.00 -> [ 9016 10024 7085 7012 7016 ]
Frank      43.00 -> [ 7080 ]
Gab        99.34 -> [ 7068 7001 7007 7021 ]
Harry      21.80 -> [ 7018 7088 3085 3012 3016 ]
Ian         5.12 -> [ 3080 3068 ]
Johnny     88.01 -> [ 3001 3007 3021 3018 ]
Kerry       0.00 -> [ 3088 8085 8012 8016 8080 8068 8001 8007 ]
Leo        27.23 -> [ 8021 8018 ]
Mike       98.45 -> [ 8088 ]
Nathan     65.12 -> [ 6085 6012 6016 6080 6068 6001 ]
Olivia      0.00 -> [ ]
int arr[a03input_num[i]];
oTmp -> prodID= arr;