C将结构添加到具有常量值的结构数组中

时间:2016-09-30 03:04:32

标签: c arrays structure constants

我有一项任务,我们需要取一个未指定的数字列表,显示未排序的双向链表,快速排序并显示排序列表。

这是主要的:

void main()
{
    int i =0;
    int c = 0;
    int *j = malloc(sizeof(int));
    int temp;
    int *k = malloc (sizeof(int));
    struct _myNode *a = malloc (sizeof(struct _myNode));
    printf("\nEnter the list to be sorted : ");
    while(1)
    {   
        scanf("%d",j);
        if (*j == 0)
        {
            free(j);
            break;      
        }
        else
        {       
            temp = *j;  
            c++;
            printf("C = %d ", c);
            a = realloc(a, c*sizeof(struct _myNode));
            if(i > 0)
            {
                struct _myNode newNode = { temp, NULL, &a[i-1] };
                a[i] = newNode;
                a[i-1].next = &a[i];
            }
            else 
            {
                struct _myNode newNode = {  temp, NULL, NULL };
                a[i] = newNode;
            }
            ++i;

        }
    }

    printf("\nUnsorted list is : ");
    for(i = 0; i < c; i++)
        printf("%d ", a[i].value);


    //quickSort(a, 0, c-1);

    //printf("\nSorted array is: ");
    //for(i=0; i < c; i++)
    //  printf("%d ", a[i]);
    //printf("\n");
}

这是标题

#ifndef NODE_H_
#define NODE_H_

struct _myNode{
    int const value;
    struct _myNode *next;
    struct _myNode *prev;
};

struct _myNode* quicksort(struct _myNode *head);

void printlist(struct _myNode *head);

#endif

当我尝试编译时,我在assignment of read-only location ‘*(a + (sizetype)((long unsigned int)i * 24ul))’行收到错误a[i]=newNode;。从我能够找到的,错误是由结构的int const value元素引起的。这是任务的要求之一,因此无法更改。如何给阵列a [i]输入正确的值?我试过了 a[i] = { temp, NULL, NULL }我得到error: expected expression before '{' token

0 个答案:

没有答案