-1

I'm beginner. So, I have trouble to understand linked lists and how pointers behave in creating first item or last and so on. Soon as struct of new_type (student) is created (with pointer to itself), and object of that struct, there are three pointers. "first", "new" and "temp". Are they always null pointers (including "link" pointer)? I mean soon as they are declared, I understand that later they have to change adress and/or value. If (!ptr) command says they are. If that's the case always I could better understand principle of further code. In all tutorials and lectures stand that pointer has to be declared as null pointer to become one. Tnx.

#include <iostream>
using namespace std;

typedef struct student { 
    char* first_name;
    char* last_name;
    char* smth;
    student* link; // is this null pointer??
}student;

student* first, *new;   //
student* temp;          // are these null pointers?  

// here is whole thing... pointers "translated"
#include <iostream>
#pragma GCC diagnostic ignored "-Wwrite-strings"

using namespace std;

typedef struct student {
    char* name;
    char* last_name;
    char* rnmb;
    student* next;
} student;

student *poc, *s;
student *temp;     

void make_new (char name[10], char last_name[10], char rnmb[5]){ 
    s = new student;
    s->name = name; 
    s->last_name = last_name;
    s->rnmb = rnmb;
    s->next = NULL; 
}

void add_at_b (char name[10], char last_name[10], char rnmb[5]) {
    make_new (name, last_name, rnmb);
    s->next = poc; 
    poc = s;
    }   

void add_at_end (char name[10], char last_name[10], char rnmb[5]) {
    make_new (name, last_name, rnmb);
    if (!poc) {
        poc = s;
    } else {
        temp = poc;
        while  (temp->next) temp = temp->next;
        temp->next = s;
    }
}

void stu_del (char name[10]) {
    temp = poc;
    while (temp->next) {
        if (temp->next->name == name) {
            delete temp->next;
            temp->next = temp->next->next;
        }
        temp = temp->next;
    }
}


void stu_del_all () {
    student *cpy;
    temp = poc;
    while (temp) {
        cpy=temp;
        temp=temp->next;
        delete cpy;
    }
    poc = NULL;
}

int main()
{
    add_at_b("John", "Doe", "4323");
    add_at_end("John jr.", "DoeII", "4323");
    add_at_b("Ma", "Mar", "4323");
    stu_del("John");
    //stu_del_all ();
    if (poc == NULL) cout << "List is empty" << endl;
   return 0;
}
  • 3
    Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Jan 14 '19 at 22:14
  • Are you indicating that they are declared outside of a function or in a function? It looks like these would be outside a function – Tas Jan 14 '19 at 22:14
  • 1
    I see no null pointers in provided snippet. However, I see `using namespace std` which is bad practice, and variable named `new`, which is a syntax error. However, `first`, `temp` and badly named `new` could be null pointers, if they are declared in a namespace scope. – SergeyA Jan 14 '19 at 22:15
  • You have only [uninitialized variables](https://en.cppreference.com/book/uninitialized). – zett42 Jan 14 '19 at 22:17
  • well, I don't use English in naming pointers, I'm aware of reserved names. I just translated them for this purpose. Yes, they are outside functions, and later used inside functions. (Yes I could use "good" book. I follow cplusplus and tutorialspoint. And some bad books, obviously). – user3427294 Jan 14 '19 at 22:29
  • 1
    Whoops, `new` is a keyword, so it can’t be the name of a pointer. – Pete Becker Jan 14 '19 at 23:07

3 Answers3

1

Objects of built in types that are defined in global scope are initialized to all zeros. So first, last, and temp are all null pointers.

link is part of a type definition. The code does not create any student objects, so there are no link pointers, so there are no null link pointers.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
1

It appears these variables are declared in the global namespace (i.e. outside a function), in which case yes: they are initialised to nullptr.

However, if they were declared within a function, that would not be the case, and you would need to explicitly write:

student* first = nullptr;

There's nothing stopping you from writing that where they are anyway.

link is not nullptr, because link is nothing. link doesn't exist yet, because all you have are pointers to nothing.

You have some bad practices here (using namespace std;, typedef struct), so you could simply refactor your code to the following:

struct student
{
    std::string first_name;
    std::string last_name;
    student* link = nullptr;
};
Tas
  • 7,023
  • 3
  • 36
  • 51
0
typedef struct student { 
    char* first_name;
    char* last_name;
    char* smth;
    student* link; // is this null pointer??
}student;

That's a declaration of a member variable. No link pointer exists until a student object is created.

student* first, *new;   //
student* temp;          // are these null pointers?  

Variables with static storage duration are zero initialised (before further initialisation, if any). Therefore these are null pointers initially. Except new which is keyword and therefore an ill-formed variable name.

there are three pointers. "first", "new" and "temp". Are they always null pointers

Depends on how you have initialised the student object. There are many different syntaxes that can be used to initialise an object.

If you have default initialised it, then they are not guaranteed to be null - default initialised pointers have an indeterminate value. Unless the object has static storage, in which case it is zero initialised, as I mentioned above. If you read an indeterminate value, the behaviour of the program will be undefined (some exceptions apply).

If you have value initialised the object, then those pointer will be null.

If you have list initialised the object, then those pointers will have the values that you've given in the initialiser expression. Those pointer members that lack an initialiser will be value initialised and therefore will be null.

eerorika
  • 232,697
  • 12
  • 197
  • 326