1

I am currently implementing a linked list that will store an array of strings in each node. Currently I am trying to return those array of strings to main. Code for the structure is

#define MAX_LINE 80

typedef struct node 
{
    char things[MAX_LINE/2][MAX_LINE];
    struct node *link;
}Node;

and the method in question is

char **getThings(Node *head, int position){
    if(position == 1){
        return head->things;
    }
    int i = 1;
    while(head->link != NULL){
        head = head->link;
        i++;
        if(i == position){
            return head->things;
        }
    }

{

However, the error is a "warning: return from incompatible pointer type. I have looked over returning arrays in C and dynamic allocation but have not been able to understand it in relation to linked list access. Should each of the elements in things be taken out and put into a dynamically allocated array and if so how?

  • 1
    `char**` and `char[][]` have a different memory layout, you can't directly substitute one for another. You have to convert your `things` to fit properly in a `char**`. – Nbr44 Feb 18 '14 at 12:47
  • `getThings` should return `char [][MAX_LINE]` instead – Hayri Uğur Koltuk Feb 18 '14 at 12:49
  • 1
    @Nbr44 you can't convert a `char[][]` type to a `char **` type. Doesn't make sense. They are incompatible types and typecasting will only suppress the warning. – ajay Feb 18 '14 at 13:01
  • Please see this- http://stackoverflow.com/a/21822964/1809377 – ajay Feb 18 '14 at 13:06

3 Answers3

1

You should return correct type from getThings

try

#define MAX_LINE 80

typedef char (*thing_t)[MAX_LINE];

thing_t getThings(Node *head, int position){
    if(position == 1){
        return head->things;
    }
    int i = 1;
    while(head->link != NULL){
        head = head->link;
        i++;
        if(i == position){
            return head->things;
        }
    }
Hayri Uğur Koltuk
  • 2,970
  • 4
  • 31
  • 60
1

Pointers and arrays are different types. In certain situations, an array evaluates to a pointer to its first element. This does not mean that arrays and pointers are the same. They have different pointer arithmetic and different sizeof values.

things in your struct node is of type array of 80 char[80] types where char[80] type is array of 80 int type. The function getThings returns a value of char ** type which is different and incompatible with int ** type.

When you return an array from a function, you actually return a pointer to the first element of the array. Therefore head->things evaluates to type pointer to int[MAX_LINE]. And before returning this value, you are changing its type by implicit typecasting because it doesn't match with the function's return type. (hence the warning).

// in getThings 

return head->things 

So basically it's equivalent to essentially doing this

return (int **)head->things;

I have written here in detail why int ** and int *[MAX_LINE] are incompatible types and typecasting the latter to the former type is plain wrong. Explicit typecasting won't help. It will only suppress the warning which is worse.

The only way to make this work is to change your function signature to:

typedef char (*arr_ptr)[MAX_LINE];
arr_ptr getThings(Node *head, int position);
Community
  • 1
  • 1
ajay
  • 9,402
  • 8
  • 44
  • 71
0
char (*getThings(Node *head, int position))[MAX_LINE]{
...

char (*p)[MAX_LINE];
p=getThings(&head, 1);
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70