How do I make pointer A to point to the list

I want to execute these statements:
[/code]
cout << list->info;
cout << A->info;
cout << B->link->info;
cout <<list->link->link->info;
[/code]
I keep getting errors there.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include<iostream>
#include<list>
using namespace std;

struct nodeType
{
    int info;
    nodeType *link;
};
int main()
{
    int num, *A, *B ;
    nodeType list ;
     
    cout<<"Enter numbers ending with -999" <<endl;
    cin>> num;
    while(num != -999)
    {
        cin >> num;
    }
//     A = list;
    cout <<list->info;

}
int num, *A

A is an int-pointer. It can point to an int value.

nodeType list ;

list is an object of type nodeType.

You cannot make an int-pointer point at a nodeType.

If you want to point at a nodeType, you need a nodeType-pointer.
Last edited on
I have declared nodeType pointer C as:
 
   nodeType *C;


How cqn I make C to point to the list?
C = &list;

Even better would be to initialise it directly:

nodeType *C = &list;

I don't mean to be rude, but - this is simple textbook stuff. This is one of the very first things any textbook or tutorial will tell you about pointers. If you don't know this, then I suspect you havern't really learned the very basics of pointers.

If so, then you're probably better off going to your textbook or a tutorial and getting a basic understanding of the subject, rather than trying to get your code working by asking piecemeal questions here.

If I've misunderstood your situation, then apologies.
Last edited on
I have correctly edited my program now, but when I run it I get:
1
2
3
4
5
Enter numbers ending with -999
45 34 12 -999
1967914073
Process returned 0 (0x0)   execution time : 62.767 s
Press any key to continue.

I expect to get the first num which is 45
1
2
nodeType *C = &list;
cout << C->info;
Your code doesn't do anything with the input:

1
2
3
4
5
6
    cout<<"Enter numbers ending with -999" <<endl;
    cin>> num;
    while(num != -999)
    {
        cin >> num;
    }
Topic archived. No new replies allowed.