Description
I'm trying to understand pointers, linked lists, structures and such in C. As a learning experience, I have written this small program, which:
- defines some basic structures and creates links between them, using structure pointers.
- iterates over the whole linked lists and prints out all variables.
- creates another structure, but doesn't insert it into the linked list.
- calls a function
insertEntrywhich inserts a given struct between one element of the linked list and his direct follower.
What I've done so far:
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."
- I've checked for typos in the function definition, declaration and its call.
- I've checked the amount and type of the parameters. Both parameters of
insertEntryare 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;
}
entryin your fileex1_insertStructure_linkedList.h, i.e. before the function declaration:void insertEntry(struct entry, struct entry);, that is put the followingstruct entry;before that function declaration.In file included from chkhdr-2684.c:1:0:—xyz.h:1:25: warning: ‘struct entry’ declared inside parameter list will not be visible outside of this definition or declaration—void insertEntry(struct entry, struct entry);. If your compiler didn't generate such warnings, you need to get a better compiler, or turn on more warnings. If you saw such warnings but didn't mention them in the question, then you most certainly should have done.