Reputation: 561
I'm trying to understand pointers, linked lists, structures and such in C. As a learning experience, I have written this small program, which:
insertEntry
which inserts a given struct between one element of the linked list and his direct follower.This Stack Overflow answer says: "That actually means that there is another function/declaration [..] elsewhere in your source code structure that has a different function signature."
insertEntry
are always two structs of the same type.This different Stack Overflow answer says: "You have forgotten to #include "client.h"
, so the definition", but I've also checked that. Both the actual filename on the filesystem and the #include
.
===> I don't get, where my error is.
ex1_insertStructure_linkedList.h:
void insertEntry(struct entry, struct entry);
ex1_insertStructure_linkedList.c:
#include <stdio.h>
#include "ex1_insertStructure_linkedList.h"
struct entry {
int value;
struct entry *next;
};
// clangtidy: conflicting types for 'insertEntry' [clang-diagnostic-error]
void insertEntry(struct entry given_entry, struct entry entry_to_insert) {
printf("Print inside insertEntry method: %i\n", given_entry.value);
struct entry *second_pointer = (given_entry).next;
// entry_to_insert is now the element in the middle
given_entry.next = &entry_to_insert;
// the third element
entry_to_insert.next = second_pointer;
return;
}
int main(int argc, char *argv[]) {
struct entry n1, n2, n3;
n1.value = 1;
n1.next = &n2;
n2.value = 32;
n2.next = &n3;
n3.value = 34242;
n3.next = (struct entry *)0;
struct entry *list_pointer = &n1;
while (list_pointer != (struct entry *)0) {
int printValue = (*list_pointer).value;
list_pointer = (*list_pointer).next;
printf("%i\n", printValue);
}
printf("--------------------\n");
list_pointer = &n1;
struct entry a;
a.value = 999999;
a.next = (struct entry *)0;
// clangtidy: argument type 'struct entry' is incomplete [clang-diagnostic-error]
insertEntry(n1, a);
while (list_pointer != (struct entry *)0) {
int printValue = list_pointer->value;
list_pointer = list_pointer->next;
printf("%i\n", printValue);
}
return 0;
}
Upvotes: 0
Views: 720
Reputation: 1175
You should place struct entry
declaration in ex1_insertStructure_linkedList.h :
struct entry {
int value;
struct entry *next;
};
void insertEntry(struct entry, struct entry);
Upvotes: 3
Reputation: 15837
You need to "forward-declare" the struct entry
in your file ex1_insertStructure_linkedList.h
, i.e. before the function declaration: void insertEntry(struct entry, struct entry);
, that is put the following struct entry;
before that function declaration.
The reason for this is because when the compiler encounters insertEntry(struct entry, struct entry);
, it doesn't know anything about struct entry
. By forward declaring struct entry
, you "ensure" to the compiler that the struct entry
is defined somewhere in a source file.
Upvotes: 2