list question

Why it doesn't work?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
using namespace std;

  struct elev {char nume[20], prenume[20]; float v[3]; };
  struct list {elev date; list *adru; };

list *adauga (list *p, list *q, list *u) {
  int i;
  q = new list;
  cin.getline (q->date.nume, 20);
  cin.getline (q->date.prenume, 20);
  cin >> q->date.v[0] >> q->date.v[1] >> q->date.v[2];
  if (p == NULL) {
    p = q; u = q;
  } else {
    u->adru = q;
    u = q;
  }
  //q->adru = NULL;
  return p;
}

int main () {
  list *p, *q, *u;
  for (q = adauga(p, q, u); q != NULL; q=q->adru)
   cout << p->date.nume << ' ' << p->date.prenume;
  return 0;
}


If I declare p, q, u to be global variables it works, but as function parameters no.

Thanks!
Last edited on
Setting the values of p, q, and u inside the function adauga() does not change the corresponding
values of p, q, and u inside main().
p and u are not initialized when you call the function.
Also, whatever q you pass to the function is ignored -- you clobber its value when you reuse the variable on line 9.

Hope this helps.
is there a way to modify the *p, *q, *u inside the function?

Thank you for replies.
You can pass them by reference, e.g.

1
2
adauga(list& p, list& q, list& u);
adauga(list*& p, list*& q, list*& u);
Last edited on
He's using pointers to lists.
 
adauga(list*& p, list*& q, list*& u);
Oh, d'oh, you're right.
Topic archived. No new replies allowed.