# Array of pointers to linked lists

This is a discussion on Array of pointers to linked lists within the C Programming forums, part of the General Programming Boards category; Dear programmers, i have the following problem. The homework ask to create 3 linked lists and an array of pointers ...

1. ## Array of pointers to linked lists

Dear programmers,
i have the following problem.
The homework ask to create 3 linked lists and an array of pointers to them which is NULL at the beginning.
I m stuck when it asks to enter values to the lists until the user enters a number >=1000.
Also we have to make sure that every value in each list is distinct(no duplicate numbers are allowed).
This is my code
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

typedef struct node
{
int temperature;
struct node *next;
}set;

int main(void)
{
set *ap[]={NULL,NULL,NULL}; //array of pointers to linked lists
set *p;
int n;
while(scanf("%d",&n)==1 && n<1000)

{

p=(set*)malloc(sizeof(set));
p->temperature=n;
p->next=ap[0];
ap[0]=p;
}

for (p=ap[0];p;p=p->next)
printf("%d ",p->temperature);
return 0;
}```
That code is without preventing duplicate values...
i've made an function in order to search for duplicate values but i can't attach it to the code.. i call it like that exist(ap[0],n) but i think its wrong..

Code:
```int exists(set *t , int value)
{
if(t==NULL)return 0;
while (t!=NULL)
{
if(t->temperature==value)return 1;
t=t->next;
}
return 0;
}```

2. First things you should fix:

1) n is undeclared

2) main() should be int main(void) and should return 0

3) the return value of malloc should not be cast

What do you mean "you think it's wrong"? It either is or isn't. What is your compiler telling you? What is happening when you run it? What test data are you feeding into it?

3. ## corrections

Originally Posted by claudiu
First things you should fix:

1) n is undeclared FIXED

2) main() should be int main(void) and should return 0 FIXED

3) the return value of malloc should not be cast what do you mean ?

What do you mean "you think it's wrong"? It either is or isn't. What is your compiler telling you? What is happening when you run it? crashes
What test data are you feeding into it? i want to pass the first element of the array of pointers(ap[0]) to the function exist

4. He means:
Code:
```type *t = NULL;
t = malloc( HOWMANY * sizeof *t );```
Notice how there is nothing between the word malloc and the = sign.

Quzah.