0

I am working on a waiting list that utilizes an array of linked lists. I have used linked lists before but can't figure out how to pass the array into functions. I have started out with declaring arrays of head pointers and tail pointers. I am getting this error:

warning: incompatible pointer types passing 'struct node *(*)[4]'
      to parameter of type 'struct node ***' [-Wincompatible-pointer-types]
                                add(&head,&tail);
                                          ^~~~~
lab6.c:10:31: note: passing argument to parameter 'tail' here
void add(NODE ***head,NODE ***tail);

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NODE struct node
struct node {
    char name[20];
    int number;
    NODE *next;
};
void add(NODE ***head,NODE ***tail);
void delete(NODE ***head,NODE ***tail);
void show(NODE ***head);

int main() {
    //Initialize
    NODE *head[4]={NULL,NULL,NULL,NULL};
    NODE *tail[4]={NULL,NULL,NULL,NULL};
    int whileLoop=0;
        while(whileLoop==0) {
                int selection;
                printf("Choose an Option: 1)Add new party 2)Remove table 3)Show list 4)Quit: ");
                scanf("%d",&selection);

                switch(selection) {
                        case 1:
                                add(&head,&tail);
                                break;
                        case 2:
                                delete(&head,&tail);
                                break;
                        case 3:
                                show(&head);
                                break;
                        case 4:
                whileLoop=1;
                                break;
                        default:
                                add(&head,&tail);
                }
        }
        return 0;
}
void add(NODE ***head,NODE ***tail) {
    char tempName[20];
    printf("enter a name for the party: ");
    scanf("%s",tempName);
    printf("enter a party size: ");
    int tempSize;
    scanf("%d",&tempSize);
    if (tempSize>0) {
        NODE *ptr=(NODE *)malloc(sizeof(NODE));
        strcpy(ptr->name,tempName);
        ptr->number=tempSize;
        ptr->next=NULL;
        int i;
        if (tempSize>=1 && tempSize<=2) {
            i=0;
        } else if (tempSize>=3 && tempSize<=4) {
            i=1;
        } else if (tempSize>=5 && tempSize<=6) {
            i=2;
        } else {
            i=3;
        }
        if (NULL==*head[i]) {
            *head[i]=*tail[i]=ptr;
        } else {
            (*tail[i])->next=ptr;
            (*tail[i])=ptr;
        }
    } else {
        printf("Valid size not entered");
    }



}
void delete(NODE ***head,NODE ***tail) {


}
void show(NODE ***head) {


}   
4
  • 2
    instead of using #define you can use typedef on structure. Increases readibility. Commented Oct 29, 2014 at 9:32
  • 1
    As you don't actually doesn't assign to head or tail in the add function, you don't need to pass "by reference". I suggest you drop an indirection. Commented Oct 29, 2014 at 9:34
  • 1
    As for your problem, a pointer to an array of pointers is not the same as a pointer to a pointer to a pointer. By following my recommendation above, you will actually solve your compiler error as well. Commented Oct 29, 2014 at 9:35
  • Thanks!Can you give an example? It still won't compile. Commented Oct 29, 2014 at 9:43

1 Answer 1

1

You have arrays of pointers to node, which will decay into pointers to pointers to node when passed as argument. Changes you make in the arrays' entries (to the heads and tails of the lists) will be permanent.

So the signature for your function is:

void add(NODE **head, NODE **tail);

for this client code:

NODE *head[4] = {NULL, NULL, NULL, NULL};
NODE *tail[4] = {NULL, NULL, NULL, NULL};

add(head, tail);

And inside add, you address the lists' heads as head[i].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.