Help me with struct

Hello, help with this program, because i dont know where be error.
Look my code:

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
29
30
#include <iostream>


using namespace std;

struct Tipo_Lista
 {
     int cod;
     int id;
     int num;
     int *vet;
     int n;
 };

 int main()
 {
    Tipo_Lista *Prox;

    Prox.cod = 69;
    Prox.id = 666;
    Prox.num = 6969;
    Prox.vet[] = {13, 4, 5, 6, 7};

    cout << "cod = " << Prox.cod << endl;
    cout << "id = " << Prox.id << endl;
    cout << "num = " << Prox.num << endl;
    cout << "vet = " << Prox.vet[2] << endl;

    return 0;
 }
Hi,

Prox is a pointer, so you need to use the -> operator:

Prox->cod = 69;

Hope this helps :+)
but, in cout << "cod = " << Prox.cod << endl; for example. Gave error.
Everywhere you have Prox. you need to replace it with Prox->.

That should fix the syntax errors, but the code still won't run correctly. You have defined the pointer, but you need to point it at a valid Prox object. You should either:

Replace line 17 with Tipo_Lista *Prox = new Tipo_Lista;
and add delete Prox; at line 28

or

Replace line 17 with:
1
2
Tipo_Lista tmp;
Tipo_Lista *Prox = &tmp;
with Help from dhayden, I went ahead modified your code and following gives proper results:
#include <iostream>


using namespace std;

struct Tipo_Lista
{
int cod;
int id;
int num;
int *vet;
int n;
};

int main()
{
Tipo_Lista *Prox;
Prox = new Tipo_Lista;

Prox ->cod = 69;
Prox ->id = 666;
Prox ->num = 6969;
int as[] = {13, 4, 5, 6, 7};
Prox ->vet = new int(1);
Prox->vet = as;

cout << "cod = " << Prox->cod << endl;
cout << "id = " << Prox->id << endl;
cout << "num = " << Prox->num << endl;
cout << "vet = " << Prox->vet[2] << endl;
delete Prox;
return 0;
}

output:
PS C:\Users\###\Desktop\New folder> g++ .\struct-code.cpp -o test
PS C:\Users\###\Desktop\New folder> .\test
cod = 69
id = 666
num = 6969
vet = 5
Topic archived. No new replies allowed.