Initialize struct as a pointer

Hello everybody,

I have a little trouble initializing a struct which is declared as a pointer

while this


#include <iostream>

struct teststruct {
bool a, b, c, d;
int num;
}

int main()
{

teststruct myTestStruct = { 0, 1, 1, 1, 60 };

cout << myTestStruct.num << endl;

cout << endl;
return 0;

}



works fine for me I'm having trouble initializing a struct when it is declared as a pointer e.g.



teststruct *myTestStruct;
myTestStruct = { 0, 1, 1, 1, 60 };




Is there a way to do it or am I completly missing something?

Thanks in advance and have a nice day!
- MrBr
Last edited on
first, init the pointer (do a sizeof on myTestStruct now and you'll see that it's 4B(or8B on x64) of size), eg teststruct * myTestStruct = new teststruct;

Than, to acess the teststruct to which you have a pointer, you derefrence your pointer as this:

*myTestStruct

which means that from now on this: (*myTestStruct) from your second example will equal myTeststruct from your second.


*myTestStruct = { 0, 1, 1, 1, 60 }
Hi Gregor,

thx for your quick reply. Unfortunately I've still trouble getting it to work.

From what you wrote and what I understood my code now is:

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>
using namespace std;

struct teststruct {
	bool a;
	bool b;
	bool c;
	bool d;
	int zahl;
};

int main() 
{ 
	
	teststruct *myTestStruct = new teststruct;

	cout << sizeof(myTestStruct) << endl;

	*myTestStruct = { 0, 1, 1, 0, 60 };
	
	cout << endl;
	return 0; 

} 


But it doesn't work. There's a compiler error which says

error C2059: syntax error: '{'
at line 19

Maybe I'm gettig something wrong?
The {} syntax only works when initializing structs and arrays. You can't use it to assign.
It will also not work with new, so don't bother trying.
You are getting something wrong.

A structure object can only use the { ......} value list at initialisation.

This is initialization:
teststruct myStruct= {0, 1, 1, 0, 60 }; //this is initialization

This is NOT initialization:
1
2
3
4
5
teststruct myStruct;
myStruct = {0, 1, 1, 0, 60 }; //Error - myStruct already exist and has been default initialized - 
//this is assignment - and you can't assign to a struct using this method. 
//You must set the individual structure member values.
//or you can assign  one structure to another 
Last edited on
Thx. That's what I was guessing.

Anyway appreciate your help!
closed account (ypkiE3v7)
Pointers indicate the memory address where a variable is located, right?

Therefore, the pointer should be pointing to somewhere in memory first, in order to receive a value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

struct teststruct {
bool a, b, c, d;
int num;
};

int main()
{

teststruct myTestStruct = { 0, 1, 1, 1, 60 }, *myTestStruct2;
myTestStruct2 = &myTestStruct;

cout << (*myTestStruct2).num << endl; /*Here "MyTestStruct2.num" is pointing to 
"MyTestStruct.num" and will show your value.*/

cout << endl;
return 0;

}


I hope i have helped.
Last edited on
I don't understand what's the point you're trying to convey, but that doesn't compile. You're trying to use the dot operator on a non-object on line 14.
closed account (ypkiE3v7)
The dot is to indicate the variable "num" in the struct "myTestStruct2".
For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cstdlib>
using namespace std;

struct data {
    int day;
    int month;
    int year;
};

int main (void){
    data today;
    today.day = 28; //dot is indicating "day" in struct "today"
    today.month = 8; //month is indicating "day" in struct "today"
    today.year = 2009; //year is indicating "day" in struct "today"
    cout <<"Today is "<<today.day<<"/"<<today.month<<"/"<<today.year<<endl;
    system ("pause");
    return EXIT_SUCCESS;
}
Last edited on
Except 'myTestStruct2' is a pointer, not a structure.

EDIT: Oh, and on line 13 you're assigning a structure to a pointer, which is also illegal.
Last edited on
closed account (ypkiE3v7)
"myTestStruct2" Is a pointer of type "teststruct", then he only can point to a address memory of a variable of type "teststruct", and not other type of any differents primary(int, char, etc) or compound variables (structs).

EDIT: i think what i assigned on line 13 is legal, because the variable and the pointer are of same type("teststruct").
Last edited on
You've been writing C++ for less than a week, haven't you?
closed account (ypkiE3v7)
Well, i'm a beginner in C++.
I found and I fixed the syntax errors of the above codes, I'm sorry for the confusion I caused.

EDIT: Can you show and explain to me how do that with a code, helios ?

Last edited on
1
2
3
4
5
6
int main(){
    teststruct myTestStruct = { 0, 1, 1, 1, 60 },
        *myTestStruct2=&myTestStruct;
    cout << myTestStruct2->num << endl;
    return 0;
}
closed account (ypkiE3v7)
Thank you helios.

"myTestStruct2->num" has the sam effect of "(*myTestStruct2).num" has not ?
Yes. It's syntactic sugar. Just like v[i]==*(v+i)
Topic archived. No new replies allowed.