• Hi Guest: Welcome to TRIBE, the online home of TRIBE MAGAZINE. If you'd like to post here, or reply to existing posts on TRIBE, you first have to register. Join us!

simple C programming question

the_fornicator

TRIBE Member
okay, maybe I'm stupid and I can't see past a glaring error but for the life of me, it's killing me. It's been a while since I played with C but I'm trying to implement a simply single linked list.

I have a struct:

struct node{
struct node *next_node;
char *name;
};

In my main, when I attempt to malloc some space for the fucker:

struct node *current = (struct node *)malloc(sizeof(struct node));

I get an error saying: "sizeof applied to an incomplete type."

wtf? the struct looks pretty simple to me. Nothing major.

any ideas???

thanks in advance Wilson
 

Ditto Much

TRIBE Member
Originally posted by the_fornicator
okay, maybe I'm stupid and I can't see past a glaring error but for the life of me, it's killing me. It's been a while since I played with C but I'm trying to implement a simply single linked list.

I have a struct:

struct node{
struct node *next_node;
char *name;
};

In my main, when I attempt to malloc some space for the fucker:

struct node *current = (struct node *)malloc(sizeof(struct node));

I get an error saying: "sizeof applied to an incomplete type."

wtf? the struct looks pretty simple to me. Nothing major.

any ideas???

thanks in advance Wilson

I didn't realize how out of whack I was with C until I looked at this. I can check when I get home if you don't find the answer by then.
 
tribe cannabis accessories silver grinders

~atp~

TRIBE Member
Originally posted by dj Red Turtle
Haven't used malloc in a while. But try a typedef on the struct and malloc that new name.


This is correct.

You need to typedef the structure or else the compiler gets a bit pissy.
 

the_fornicator

TRIBE Member
yeah, I just type-def'd the struct like so:

typedef struct node{
Node next_node;
char *name

} *Node;

and I still get the same error.

bastardo!
 

Dirty Girl

TRIBE Member
Originally posted by dj Red Turtle
Haven't used malloc in a while. But try a typedef on the struct and malloc that new name.

dont listen to him, he cant even connect to the internet!!:rolleyes:

carry on geeks!! :p
 
tribe cannabis accessories silver grinders

dj Red Turtle

TRIBE Promoter
Originally posted by the_fornicator
yeah, I just type-def'd the struct like so:

typedef struct node{
Node next_node;
char *name

} *Node;

and I still get the same error.

bastardo!

Did you change the malloc to sizeof (Node)?
 

dj Red Turtle

TRIBE Promoter
Originally posted by Indian Girl
what's this malloc?? some newfangled chemical enhanced drug??

hook a girl up!

Malloc is the new drug you find at parties these days. A combination of E, Weed and Beer. With no come downs !! :eek:
 

IgStar

TRIBE Member
Originally posted by dj Red Turtle
Malloc is the new drug you find at parties these days. A combination of E, Weed and Beer. With no come downs !! :eek:

we're livin' in a dream world babe! NO COME DOWNS?
 

Ditto Much

TRIBE Member
Originally posted by Indian Girl
wow.

it's like you guys are speaking japanese to me.

Computer langauges are very similar to regular languages in many respects. They have vocabularies, they have syntax, they have punctuation and they have rules. Unlike normal languages they also have what amounts to blank spaces to fill in later.

Going to school for computer science is largely about learning the rules of boolean logic and the concepts behind the syntaxes of language. It transfers from language to language as its conceptual rather than being specific.


typedef struct node{
Node next_node;
char *name

} *Node;


What this say is create a structure, the structure contains to elements, the first is a pointer to another structure and the second is a name. Think of it as saying that you have a keychain ring and a key. The key is a name and the king ring is the "node". Now each key sits on a ring, and each ring connects to another ring.

(struct node *)malloc(sizeof(struct node))

What this is really saying is allocate the space in a persons pocket for the next key ring and key. In this case I believe that it should create a space that is 4 bytes wide, two for the pointer to the next ring, and two for the name (or key).

The beauty of these comnputer languages is that they are "exact" languages. Thus if myself and two other people both understand the language we can all come to the same conclusions about what it does even though we don't know.


Its like reading algebra after a while. You just know what your looking at.
 
tribe cannabis accessories silver grinders

~atp~

TRIBE Member
I'll have a chance to look at it in a sec...but, tell me, what compiler version are you using? ...also, what platform?
 

~atp~

TRIBE Member
k, i had a sec to recreate your problem:

#define givebirthto(x) malloc(sizeof(struct x))
#define aKid Human

struct Human
{
struct Human * parent;
char * name;
};

int main()
{
struct Human * parent = (struct Human *) givebirthto(aKid);

return 0;
}

I was bored, so I changed it a bit, but it's the same thing as yours...mine worked fine. I'm using gcc 3.3 and i'm on a linux box kernel 2.4.
 

the_fornicator

TRIBE Member
oops... nevermind.

I figured it out.

Apparently I declared a struct in my header file but defined it in my source file.

heh. so when I went to malloc(sizeof(blah)), blah (my struct) wasn't properly defined. Hence my invalid error.

dammit.

(in case anyone cares, compiling with gcc on an openBSD box)
 

Shug

TRIBE Member
I thought linked list arrays were already implemented in STL.

Just use that, the whole point of libraries is that all the work is done for you already.

Anyways, I'm C++, not C, so we stay away from shit like malloc... too dangerous.
 
tribe cannabis accessories silver grinders

dj Red Turtle

TRIBE Promoter
Originally posted by Shug
I thought linked list arrays were already implemented in STL.

Just use that, the whole point of libraries is that all the work is done for you already.

Anyways, I'm C++, not C, so we stay away from shit like malloc... too dangerous.

Same here.
 

~atp~

TRIBE Member
Originally posted by Shug
I thought linked list arrays were already implemented in STL.

Just use that, the whole point of libraries is that all the work is done for you already.

Anyways, I'm C++, not C, so we stay away from shit like malloc... too dangerous.

too dangerous for the weak-minded. ;)


*poke poke*
 

erikwh

TRIBE Member
Originally posted by Shug
I thought linked list arrays were already implemented in STL.

Just use that, the whole point of libraries is that all the work is done for you already.

Anyways, I'm C++, not C, so we stay away from shit like malloc... too dangerous.

SEGMENTATION FAULT

pointers will cause one endless grief...
 
tribe cannabis accessories silver grinders
Top