0

My questions have to do with very early stages of learning lists in C.I haven't been able to find a clear answer to my questions anywhere whether it has to to with the simplest code there is.(I'm going to give you an example of what i don't clearly understand on a very simple code just for you to get where i lose my understanding.It's the same question i have with more complicated ones.)

   struct node{
       int data;
       struct node *next;
   };

i get what data and next are used for as variables but i don't get: what struct node *next really means.Do we create a new stuct named next that has new data and next members?What i also don't get is the * usage in next.

**Below follows a code that aims to create tabs and add site addresses to these tabs.Kind of what a borwser would do.InfoSession in an informational code.The list i'm creating is a double linked list.**But i don't understand two things: 1)What does really TabNodePtr LastTab mean for example?Is LastTab a new struct now that has all the above :

TabNodePtr PrevTab, NextTab;
SiteNodePtr FirstSite;
SiteNodePtr CurrSite;

as members?Same with the SiteNodePtr s. I don't get how they are all linked together it's all a ball of christmas garlands to me right now. 2)Why do we use pointers for the members?And don't just go with TabNode LastTab for instance?Where do pointers make a diffrence and we must add them?

   typedef struct TabNode *TabNodePtr;
    typedef struct SiteNode *SiteNodePtr;

    struct InfoSession {
        TabNodePtr FirstTab;
        TabNodePtr LastTab;
        TabNodePtr CurrTab;
    };

    struct TabNode {    
        TabNodePtr PrevTab, NextTab;
        SiteNodePtr FirstSite;
        SiteNodePtr CurrSite;
    };

    struct SiteNode{
        SiteNodePtr PrevSite, NextSite;
        TabNodePtr UpTab;
    };
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    It seems you could really need to read a couple of beginner books, as they should tell you all you seem to wonder about. – Some programmer dude May 15 '19 at 12:58
  • Possible duplicate of [Struct pointer versus actual struct](https://stackoverflow.com/questions/25800447/struct-pointer-versus-actual-struct) – Raymond Chen May 15 '19 at 13:13

1 Answers1

1

You have a long, long way to go....

Let me try, if I can help you a little in your journey of learning C language.

what struct node *next really means.Do we create a new stuct named next that has new data and next members?What i also don't get is the * usage in next.

struct node *next;

This statement declares a variable named next which is of type struct node * which means next is a pointer which can point to address of type struct node.
For e.g. in the statement int *p;, variable p is of type int * which means p is a pointer which can point to int. [For pointers, check this or this or google it or refer any good book].

Have you ever heard of self-referential structure in C?
A self-referential structure is a structure in which one of its members is a pointer to the structure itself.

So, in this structure declaration:

       -------------------------------------------------
       |                                               |
       v                                               |
  struct node{                                     -----------
      int data;                                    |         |
      struct node *next;   // next is a pointer to struct node, i.e. pointer to self
  };

the struct node is a self referential structure. [For more details on self-referential structure check this and for its usage check this].

But i don't understand two things: 1)What does really TabNodePtr LastTab mean for example?Is LastTab a new struct now that has all the above....

First of all, read thoroughly about pointers (more specifically pointer to a structure) in C.

typedef is used to create an alias name for other data type. [Read more about typedef here and about typedef a struct here or refer any good book.]

So, when you write

typedef struct TabNode *TabNodePtr;

that means, now, the TabNodePtr is an alias of struct TabNode * type and you can use TabNodePtr to declare variable of type struct TabNode *.

So, after the above typedef.... statement, when you write

TabNodePtr xyz;

is exactly same as this

struct TabNode *xyz;

In your code:

TabNodePtr LastTab;

this declares the variable LastTab of type struct TabNode * which means LastTab is a pointer to struct TabNode.

So, this is

    struct InfoSession {
        TabNodePtr FirstTab;
        TabNodePtr LastTab;
        TabNodePtr CurrTab;
    };

is exactly same as this

    struct InfoSession {
        struct TabNode *FirstTab;
        struct TabNode *LastTab;
        struct TabNode *CurrTab;
    };

Same is for SiteNodePtr as well which is an alias of struct SiteNode *.

2)Why do we use pointers for the members?And don't just go with TabNode LastTab for instance?Where do pointers make a diffrence and we must add them?

Why do we use pointers for the members? or Why do we use pointers? - This is a very natural question for someone who does not know or know very little about pointers in C and for most of the beginners it is a topic which confuses them a lot. But believe me once you gain proper knowledge about pointers and learn to use them, you will never go back. Pointers are an irresistibly very powerful programming construct. To understand - Why do we use pointers...., first you have gain knowledge about pointers. For starters, you can refer this and this.

Happy learning.... enjoy..

H.S.
  • 11,654
  • 2
  • 15
  • 32