I am trying to create a linked list of contacts in C, and sort them by person length, as an assignment for school. I kinda wanted to use a constructor to simulate object oriented programming. Only problem is, that when i try to enter the name as a string in my constructor function, i get a compile error.
This is how my code looks:
#include "stdafx.h"
#include "stdlib.h"
#include "string.h"
#define MAXL 32
typedef struct post {
char enamn[MAXL];
char fnamn[MAXL];
int langd;
post *prev;
post *next;
} POSTTYP;
//API
void sortlist(post *first);
post * newPost(post *first, const char en, const char fn, int ln);
void printlist(post *first);
int _tmain(int argc, _TCHAR* argv[])
{
post *first;
newPost(first, "Kalle", "Halvarsson", 185);
newPost(first, "Johan", "Lovgren", 180);
printlist(first);
return 0;
}
post * newPost(post *first, const char *fn, const char *en, int ln) {
post *p;
//Reservera minne till posten:
p = (post*)malloc(sizeof(post));
strcpy(p->fnamn, fn);
strcpy(p->enamn, en);
p->langd = ln;
first->prev = p;
p->next = first;
*first = *p;
return p;
}
void removePost(post *first, post* whichpost) {
post *temp = NULL;
whichpost->next->prev = whichpost->prev;
whichpost->prev->next = whichpost->next;
free(whichpost);
}
void swap(post *a, post *b) {
post *tnext = NULL;
post *tprev = NULL;
tnext = a->next;
tprev = a->prev;
a->next = b->next;
a->prev = b->prev;
b->next = tnext;
b->prev = tprev;
}
void sortlist(post *first) {
post *curr = first;
while(curr->next != NULL) {
//This function is incomplete
curr = curr->next;
}
}
void printlist(post *first) {
post *curr = first;
while (curr != NULL) {
printf("Förnamn: %s\n",curr->fnamn);
printf("Efternamn: %s\n",curr->enamn);
printf("Längd: %s\n",curr->langd);
curr = curr->next;
}
}
The error message i get is:
Error 1 : 'newPost' : cannot convert parameter 2 from 'const char [6]' to 'const char'
What am i doing wrong? Overall feedback on the code is very welcome, although note that it is still very unfinished.
Thanks in advance.