I have come across this error i can not explain. Was working on a liked list:
main.c
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
int main(int argc, char *argv[]) {
Container* c = initList((void*)0x1);
void* value1 = list_get_content(c,0); //1.
void* value2 = list_get(c,0)->content; //2.
printf("%p",value1);
printf("%p",value2);
return 0;
}
1.
Works fine, but 2.
doesn't compile, eventhough they should do the same thing.
Why does the function call make a diffrence here?
list_get_content
is just a wrapper for list_get()->content
Compiled using MinGW GCC 4.7.2 in Dev-C++.
No parameters or settings
list.h:
struct _Node;
typedef struct _Node Node;
struct _Container;
typedef struct _Container Container;
Node* createNode(Node* last, Node* next, void* content);
Container* initList(void* firstValue);
void list_add(Container* list,void* e);
void* list_get_content(Container* list,int index);
Node* list_get(Container* list,int index);
void list_remove(Container* list,int index);
list.c:
#include <stdlib.h>
#include <stdio.h>
#include "list.h"
struct _Node{
void* content;
struct Node* last;
struct Node* next;
};
struct _Container{
Node* start;
Node* end;
};
Node* createNode(Node* last, Node* next, void* content){
Node *n = (Node *) malloc(sizeof(Node));
n->content = content;
n->last = last;
n->next = next;
return n;
}
Container* initList(void* firstValue){
Container *c = (Container *) malloc(sizeof(Container));
Node* n = createNode(NULL,NULL,firstValue);
c->start = n;
c->end = n;
return c;
}
Node* list_get(Container* list,int index){
Node* n = list->start;
for(int i=0;i<index;i++){
n = n->next;
}
return n;
}
void* list_get_content(Container* list,int index){
return list_get(list,index)->content;
}