2

I need to know if i want to make an array that every element of the array is a pointer to a linked list and pass the array to a function, the function is void because I need to change the array

typedef struct n{
  char *S;
  int num;
}list;

int (main){
list *Array[50];
return 0;
}

should the function be void changeArray(list A[]); or void changeArray(list *A[]); or void changeArray(list **A[]);

5
  • void changeArray(list *A[]); Commented Jun 6, 2015 at 10:49
  • why ? the Array when i pass it to the function take the pointer with it .. the element of the array is an address to an linked list .. if change the element of the array (address ) the element changes not the copy Commented Jun 6, 2015 at 10:51
  • you can write test code. Commented Jun 6, 2015 at 10:53
  • it works but i need a answer O.o Commented Jun 6, 2015 at 10:57
  • Other's does not fit the type. Commented Jun 6, 2015 at 10:59

2 Answers 2

2

The function could be either void changeArray(list *A[]) or void changeArray(list **A). Both signatures would accept an array of pointers, and let you change elements of that array:

void changeArray(list *A[]) {
    ...
    A[0] = malloc(list);
}
Sign up to request clarification or add additional context in comments.

4 Comments

why ? the Array when i pass it to the function take the pointer with it .. the element of the array is an address to an linked list .. if change the element of the array (address ) the element changes not the copy
@cip Arrays passed to functions are not copied. Instead, they "decay" to pointers. When you modify A inside your function, the array that you pass will be modified. If you would like to pass a copy of the array, either wrap it in a struct, or make a copy explicitly with memcpy.
that the point ! the element of the array is an address if i change it in the function it changes in real why i need a pointer then? why the function will not be just void change(list A[]);
@cip Because your Array is an array of pointers. Each element of the array is a pointer to list, but the array itself becomes a two-star-pointer, i.e. a pointer to a pointer. Had Array been declared as list Array[50], then void change(list A[]); would be the right signature. However, since you declared it with an asterisk, i.e. list *Array[50], you need a second asterisk in the signature of the function that receives your array.
1

The array is defined like

list *Array[50];

So if you want to pass this array to a function then it should be declared like

void changeArray( list *Array[50], size_t n );

or

void changeArray( list *Array[], size_t n );

or

void changeArray( list **Array, size_t n );

and called like

changeArray( Array, 50 );

In any case a declaration of an array as a function parameter is adjusted to a pointer to object of the type of elements of the array.

3 Comments

why ? the Array when i pass it to the function take the pointer with it .. the element of the array is an address to an linked list .. if change the element of the array (address ) the element changes not the copy
@cip The array is converted to a pointer to its first element that is the parameter will have actual type list **.The whole array is not copied into the function.
@cip So if you will change elements pointed to by the pointer you will change the original array not its copy.

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.