You are not logged in.

#1 2009-09-12 20:32:44

Lexion
Member
Registered: 2008-03-23
Posts: 510

Linked List error [SOLVED]

I have written linked lists many times, but I'm stumped by this error.

header

#include <stdio.h>

typedef struct
{
char *str;
struct strstack *next;
} strstack;

void connect(struct strstack *a, struct strstack *b);

source

#include <stdio.h>
#include "strstack.h"

void connect(struct strstack *a, struct strstack *b)
{
a->next = b;
}

errors

gcc -g -O2 --std=c99 -c strstack.c
strstack.c: In function 'connect':
strstack.c:7: error: dereferencing pointer to incomplete type
make: *** [strstack] Error 1

Does anybody have any idea what causes this?

Last edited by Lexion (2009-09-12 23:48:49)


urxvtc / wmii / zsh / configs / onebluecat.net
Arch will not hold your hand

Offline

#2 2009-09-12 21:08:21

gnud
Member
Registered: 2005-11-27
Posts: 182

Re: Linked List error [SOLVED]

Yes.

Your typedef is silly.

Replace with

typedef struct stack_s
{
char *str;
struct stack_s *next;
} stack_t;

or similar.

You have to name the struct, and use the struct name when declaring the next pointer. You can typedef it too, if you want to, but that name can't be used in the struct declaration.

Offline

#3 2009-09-12 21:09:24

gnud
Member
Registered: 2005-11-27
Posts: 182

Re: Linked List error [SOLVED]

To clarify: You don't declare "struct strstack" anywhere. You declare an unnamed struct, and typedef that struct as strstack.

Offline

#4 2009-09-12 21:22:41

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,804

Re: Linked List error [SOLVED]

There is an article on this at http://www.netalive.org/codersguild/posts/1753.shtml

Try:

struct strstack
{
char *str;
struct strstack *next;
} ;

Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#5 2009-09-12 23:48:08

Lexion
Member
Registered: 2008-03-23
Posts: 510

Re: Linked List error [SOLVED]

Thanks all, it works great! smile


urxvtc / wmii / zsh / configs / onebluecat.net
Arch will not hold your hand

Offline

Board footer

Powered by FluxBB