GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Nov-2004, 14:38
MAX_POWER MAX_POWER is offline
New Member
 
Join Date: Nov 2004
Posts: 4
MAX_POWER is on a distinguished road
Question

Crazy Linked List Problem


Hello :-o
I am new to this forum and this is my first post so excuse me for any errors in my posting. I have been learning c for a few months now and in the 3rd or so rewrite of my first program. I am starting to get into doing more with structs and linked lists and using pointers to hopefully limit the amount of memory used since not all of them need everything. I am at a point where I need to add a struct that hasn't been defined at the point where I need it because it has to be below since it uses the other structs. I have tried to use a void pointer to the struct as a member but it doesn't work and I even tried to type cast it but that didn't help anything other than adding more errors. I'm not sure if I was doing it right since my book I'm learning from never did a cast to a struct so I thought it may have just been outside the scope of the book.

My question is:
Is it possible to have a void pointer to a struct or union so I don't have to nest them all in the base linked list so I'm not wasting memory and if not is there a way to do what I'm trying to do?

ps.
Keep in mind that I don't know much yet and my terminology is kinda weak so try not to confuse me past the point I am at now.

A BIG THANX in advance for any help you can provide.
  #2  
Old 06-Nov-2004, 14:52
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hello MAX_POWER. Welcome to GIDForums™. Did you get your name off a hair dryer

I don't follow your question exactly. Do you have any code snippet that you could post that would explain what you mean?

If you post code, keep in mind to use the c-code posting tags
  #3  
Old 06-Nov-2004, 15:16
MAX_POWER MAX_POWER is offline
New Member
 
Join Date: Nov 2004
Posts: 4
MAX_POWER is on a distinguished road
hehehe
nope
got the name from a simpsons episode ;-)
my code is kinda messed up so I would have to do some tweaking since I only wrote it for testing it out and not for use in the program I needed it in since that program has a long way before I can compile it...
I'll go and tweak my code and post it
  #4  
Old 06-Nov-2004, 15:19
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by MAX_POWER
Hello :-o
I am new to this forum and this is my first post so excuse me for any errors in my posting. I have been learning c for a few months now and in the 3rd or so rewrite of my first program. I am starting to get into doing more with structs and linked lists and using pointers to hopefully limit the amount of memory used since not all of them need everything. I am at a point where I need to add a struct that hasn't been defined at the point where I need it because it has to be below since it uses the other structs. I have tried to use a void pointer to the struct as a member but it doesn't work and I even tried to type cast it but that didn't help anything other than adding more errors. I'm not sure if I was doing it right since my book I'm learning from never did a cast to a struct so I thought it may have just been outside the scope of the book.

My question is:
Is it possible to have a void pointer to a struct or union so I don't have to nest them all in the base linked list so I'm not wasting memory and if not is there a way to do what I'm trying to do?

ps.
Keep in mind that I don't know much yet and my terminology is kinda weak so try not to confuse me past the point I am at now.

A BIG THANX in advance for any help you can provide.


Instead of boring you with termonilogy, here's an example with a couple of things you might be interested in.

The bottom line is that you can refer to a pointer to a struct even before the struct has been defined. You can have a struct with members that are pointers to the type of struct being defined. That's how linked lists are implemented.

Have fun with this:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct tnode {
  char *word;
  int count;
  struct tnode *left;
  struct tnode *right;
};

struct t {
  int i1;
  int i2;
  struct s *p;
};

struct s {
  int xx;
  struct t *tpointer;
  struct tnode *tnode_pt;
};

int main()
{

  struct tnode st1;
  struct t     stt;
  struct s     sts;
  struct tnode tar[10];

  st1.word = malloc(100);
  if (st1.word == NULL) {
    printf("Can't allocate memory\n");
    return 0;
  }
  else {
    printf("Allocated memory\n");
  }
  strcpy(st1.word, "Hello, world!");
  printf("Here's the scoop: <%s>\n", st1.word);

  sts.tnode_pt = &st1;

  printf("Here's some more of the same:<%s>\n", sts.tnode_pt->word);
  
  free(st1.word);

  return 0;
}


Any questions? (This stuff is kind of fun when it works.)

Regards,

Dave
  #5  
Old 06-Nov-2004, 15:35
MAX_POWER MAX_POWER is offline
New Member
 
Join Date: Nov 2004
Posts: 4
MAX_POWER is on a distinguished road
thanx for that info dave
I'm going to try making it work using that info before I try to fix my unfixable test code and post it
but if I understand it properly as long as it is a pointer to the struct then it doesn't need to be defined at the time the pointer to it is used in another struct
but that is if I understand it properly
  #6  
Old 06-Nov-2004, 15:40
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by MAX_POWER
thanx for that info dave
... if I understand it properly as long as it is a pointer to the struct then it doesn't need to be defined at the time the pointer to it is used in another struct
but that is if I understand it properly

I think you've got it! (Lots of people do this for a lot longer than you've been at it and never seem to get it.)

Enjoy!

Regards,

Dave
  #7  
Old 06-Nov-2004, 17:07
MAX_POWER MAX_POWER is offline
New Member
 
Join Date: Nov 2004
Posts: 4
MAX_POWER is on a distinguished road
I would just like to say THANX again for the help
I rewrote the test code and it worked...
at least after I fixed all my other errors ;-)
at least now I know where to go if I have any other questions
and thanks for the quickness of the help
I been sitting on my code(not literally) for weeks trying to think of a way to get past my problem
all I can say is thanks(but not enough) and woohoo
I can now continue with the real code
and a big
THANK YOU
to dave for setting me straight
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with the function creating updating a linked list nkhambal C Programming Language 3 28-Oct-2004 21:45
bus error in linked list picknicker187 C Programming Language 2 08-Oct-2004 11:44
Insert problem in Linked list Kay Chan C Programming Language 1 03-Sep-2004 18:06
Insert problem in linked list with two function code Kay Chan C++ Forum 1 03-Sep-2004 10:52
help on linked lists any1????? nick4 C Programming Language 1 17-May-2004 10:32

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:18.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.