tags:

views:

145

answers:

5

hello i got a problem with returning an array of linklists in c lets say i got

arrlinklist = {linklist1 , linklist 2...,linklist5}

and i want my function to return arrlinklist. how do i do that... thx in advance.

+3  A: 

In C you cannot return an array, you can only return a pointer to the first element of the array.

doron
A: 

i wanted to know how to define the signature of my function i tried linkedlist* CreateArrayOfList();

Nadav Stern
In the first place, this should be an edit to the question, not an answer. Since the order of answers is variable, SO is not suited for conversational style. Second, how are we going to answer that without knowing more? You've already been asked for more code and declarations, and have not provided them.
David Thornley
That will allow you to return a pointer to a linkedlist object. So return arrlinklist[0];
Patrick
okey thx anyway
Nadav Stern
...or it could be a comment to the answer it is responding to.
T.E.D.
+1  A: 

Perhaps you could maintain a linked list of linked lists. I am assuming you have created a linked list successfully.

Praveen S
A: 

I assume you want something like this: (there are no checks on this program, you should check for malloc failures etc')

#include <stdio.h>
#include <stdlib.h>

typedef struct list1
{
    struct list1 *next;
    int data;
} list;

    list **
getArrayLists(int amount)
{
    list** arrayList  = malloc(sizeof(list[amount]));
    int i=0;
    for (i=0;i<amount;++i)
    {
        list *listPointer = malloc(sizeof(list));
        //let's start their data portion to be the index
        listPointer->data = i;
        arrayList[i] = listPointer;
    }
    return arrayList;
}

    int
main()
{
    list** arrayList = getArrayLists(10);
    int i=0;
    for(i=0;i<10;++i)
        printf("%d\n",arrayList[i]->data);

}
Liran Orevi
A: 

Couple of points:

First of all, C functions cannot return array types; they can return pointers to arrays, however:

T (*foo())[N] {...} // returns pointer to N-element array of type T

The signature breaks down as:

    foo                        -- foo
    foo()                      -- is a function
   *foo()                      -- returning a pointer
  (*foo())[N]                  -- to an N-element array
T (*foo())[N]                  -- of T

Pointers to arrays are not that convenient to use; you (usually) have to know the size of the array type you're dealing with (a pointer to a 10-element array of int is a different, incompatible type from a pointer to an 11-element array of int), and you have to dereference the array identifier before applying the subscript((*arr)[i] = ...). Secondly, you can't just return the address of an array from the function such as

int (*foo())[10]
{
  int arr[10] = {0,1,2,3,4,5,6,7,8,9};
  return &arr;                          // THIS DOESN'T WORK!!!!!!!!
}

because once the function exits arr technically no longer exists, and the value received by the caller now points to memory that's potentially been overwritten or no longer available.

The usual idiom is to have the function dynamically allocate a chunk of memory of the type you want, initialize it, and then return a pointer to that base type:

T *foo(size_t count) // you may pass other arguments if necessary for 
                     // for initializing the buffer
{
  T *bar = malloc(sizeof *bar * count);
  if (bar)
  {
    // initialize the contents of bar
  }
  return bar;
}

int main(void)
{
  T *blah = foo(10);
  if (blah)
  {
    int i;
    for (i = 0; i < 10; i++)
      do_something_with(blah[i]);
  }
  return 0;
}

Replace T above with your linked list type, and that should give you the basis of what you want to do.

If you don't want to mess with dynamic memory (the best way to avoid problems with memory management is to avoid memory management), you can pass your target array as a parameter to the function like so:

void foo(T *bar, size_t count) // along with any other parameters necessary
                               // to initialize  bar
{
  size_t i;
  for (i = 0; i < count; i++)
    do_something_with(bar[i]);
}

int main(void)
{
  T blah[10];
  foo(blah, sizeof blah / sizeof blah[0]);
  ...
}

Remember that in most contexts, an array expression will have its type implicitly converted ("decay") from "N-element array of T (T [N])" to "pointer to T (T *)", so when you call foo with an array argument, what the function will actually receive is a pointer type, not an array type. Since pointers don't know the size of the area they point to, you will have to pass the array size as a separate parameter.

John Bode