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?

4
  • 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**. Commented Feb 18, 2014 at 12:47
  • getThings should return char [][MAX_LINE] instead Commented Feb 18, 2014 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. Commented Feb 18, 2014 at 13:01
  • Please see this- stackoverflow.com/a/21822964/1809377 Commented Feb 18, 2014 at 13:06

3 Answers 3

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;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

0
char (*getThings(Node *head, int position))[MAX_LINE]{
...

char (*p)[MAX_LINE];
p=getThings(&head, 1);

2 Comments

typedef char (*parray)[MAX_LINE]; parray getThings(Node *head, int position); is more readable :)
@ajay I do not think it is easier to read necessarily be hiding.

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.