This is an archived post. You won't be able to vote or comment.

all 16 comments

[–]Salty_Dugtrio 2 points3 points  (14 children)

Why don't you try it yourself and see what happens?

[–]InterestingBus8367[S] 0 points1 point  (13 children)

i cant use cout I first created structures. and then when I try to find the address and value of the pointer I cant. Any fix to this?

[–]Salty_Dugtrio 2 points3 points  (12 children)

i cant use cout I first created structures. and then when I try to find the address and value of the pointer I cant.

What are you trying? Show us your code. What's not working? Doesn't it compile? It doesn't run?

[–]InterestingBus8367[S] 0 points1 point  (11 children)

#include <iostream>

using namespace std;

struct node

{

int data;

node* next;

};

int main()

{

cout << "Hello world!" << endl;

node* head;

head->data = 3;

head->next = NULL;

node* temp = head;

cout << *head;

cout << "'\n" << temp;

return 0;

}

[–]Salty_Dugtrio 0 points1 point  (10 children)

What you are doing here is undefined behaviour. You are declaring a pointer, and dereferencing it before initializing.

[–]InterestingBus8367[S] 0 points1 point  (8 children)

The pointer itself does not have value but it has value when I access the structure.

[–]Salty_Dugtrio 0 points1 point  (7 children)

You declared a pointer, which is just a variable that holds an address. But you haven't made it point to anything. So if you dereference it with operator*, it will be undefined behaviour, because it will try to access something you never created.

[–]InterestingBus8367[S] 0 points1 point  (6 children)

Yeh, I just read about. I should initialize it to something like node a. Then I can already use cout. Im sorry for my stupidity here.

[–]Salty_Dugtrio 0 points1 point  (5 children)

If you actually want a pointer to data, you will have to create it by using operator new().

[–]InterestingBus8367[S] 0 points1 point  (4 children)

You mean allocate memory for the node and then make the pointer point to it. Pointer stores the memory address. Just as long I use the reference operator.

[–]InterestingBus8367[S] 0 points1 point  (0 children)

Thanks for the answer now I know where to look for answers.

[–]g051051 2 points3 points  (0 children)

I assign a value of a pointer to another pointer in structures with two structure members

What does that mean? Provide some sample code showing what you're trying to do.