initialize dynamically allocated struct array

1
2
3
4
5
6
weapons *myWeapons = new weapons[3];
*myWeapons = {"Weapon 1" , 500 , 2};
++myWeapons;
*myWeapons = {"Weapon 2" , 1500 , 2};
++myWeapons;
*myWeapons = {"Weapon 3" , 4500 , 2};


i have to initialize this dynamically allocated array of struct using pointer arithmetic. but its not working
Do you get an error message or what?
closed account (SECMoG1T)
Hi , this should 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
29
30
#include <iostream>
#include <string>

using namespace std;

struct weapons
{
    weapons()=default;
    weapons(string Name,int Value, int Qty):name(Name),value(Value),quantity(Qty){}
    string name;
    int value;
    int quantity;
};

int main()
{
    weapons* myWeapons =new weapons[3];
    *myWeapons={"Weapon 1" , 500 , 2};
    *(++myWeapons)={"Weapon 2" , 1500 , 2};
    *(++myWeapons)={"Weapon 3" , 4500 , 2};

    myWeapons-=2;

    for(int i=0;i<3;i++)
    {
        cout<<"NAME : "<<myWeapons->name<<"    VALUE : "<<myWeapons->value<<" Quantity : "<<myWeapons->quantity<<endl;
        ++myWeapons;
    }
}


i just tried to figure out your code.
Last edited on
1
2
3
4
5
*myWeapons={"Weapon 1" , 500 , 2};
    *(++myWeapons)={"Weapon 2" , 1500 , 2};
    *(++myWeapons)={"Weapon 3" , 4500 , 2};

    myWeapons-=2;
What type of witchery is this?

1
2
3
    myWeapons[0]={"Weapon 1" , 500 , 2};
    myWeapons[1]={"Weapon 2" , 1500 , 2};
    myWeapons[2]={"Weapon 3" , 4500 , 2};
Is much better.
Last edited on
closed account (SECMoG1T)
@Avilius initialize this dynamically allocated array of struct using pointer arithmetic
1. myWeapons[2] == *(myWeapons+2) == *(2+myWeapons) == 2[myWeapons]
Pointers, arithmetic, syntactic sugar, ...

Most of all, the "have to" makes one ask: Why?


2. That is not initialization. That is assignment to already initialized objects.

C++11 does allow initialization:
1
2
3
4
weapons* myWeapons = new weapons[3] {
    {"Weapon 1" ,  500 , 2},
    {"Weapon 2" , 1500 , 2},
    {"Weapon 3" , 4500 , 2} };


3. If the ++pointer style is necessary, then create a temporary pointer for the job and keep the myWeapons firmly at the beginning of the block at all times.

4. A new without delete is (1) bad habit and (2) the inspiration for many things in the standard library.
Last edited on
C++11 does allow initialization: 

i have tried it like this and scared to try again.
1
2
3
4
weapons* myWeapons = new weapons[3] = {
    {"Weapon 1" ,  500 , 2},
    {"Weapon 2" , 1500 , 2},
    {"Weapon 3" , 4500 , 2} };


3. If the ++pointer style is necessary, then create a temporary pointer for the job and keep the myWeapons firmly at the beginning of the block at all times.

what do you mean? this makes me think
@Peter87
Do you get an error message or what? 

i get like syntax error semicolon before '{' something like that
@andy1992
can you tell me what is this?
i havent yet in this
1
2
 weapons()=default;
    weapons(string Name,int Value, int Qty):name(Name),value(Value),quantity(Qty){}
is this a good idea also?

1
2
3
4
5
6
7
weapons myWeapons( weapons &myWeapon[3] ) {
*myWeapons = {"Weapon 1" , 500 , 2};
++myWeapons;
*myWeapons = {"Weapon 2" , 1500 , 2};
++myWeapons;
*myWeapons = {"Weapon 3" , 4500 , 2};
}


can you tell me how this works i mean the weapons myWeapons(); is this a function of struct? or object?
closed account (SECMoG1T)
Hi

1
2
weapons()=default;
 weapons(string Name,int Value, int Qty):name(Name),value(Value),quantity(Qty){}

The first one is the struct default constructor , the keyword =default request the compiler to synthesis the constructor instead of you providing an explicit definition, The second one is an explicitly defined constructor that takes the parameters specified within the braces,

I added them for the sake of formality because your struct possibly would do without them, am sure an aggregate class will accept implicit conversion from your initi-list during intialization.
Last edited on
closed account (SECMoG1T)
++11 does allow initialization: sure it does allow but you should realize that there is difference between assignment and intialization
1
2
3
4
5
6
7
8
9
10
11
weapons* myWeapons = new weapons[3] = {
    {"Weapon 1" ,  500 , 2},
    {"Weapon 2" , 1500 , 2},
    {"Weapon 3" , 4500 , 2} };///your are assigning an intializer list to an array


weapons* myWeapons = new weapons[3] {
    {"Weapon 1" ,  500 , 2},
    {"Weapon 2" , 1500 , 2},
    {"Weapon 3" , 4500 , 2} };/// intialization

1
2
 If the ++pointer style is necessary, then create a temporary pointer for the job and keep the 
myWeapons firmly at the beginning of the block at


sure you don't want to do like i did in the example i gave, preserving the initial pointer value is important
1
2
3
4
5
6
7
8
9
10
11
12
13
14
weapons* myWeapons =new weapons[3];
    *myWeapons={"Weapon 1" , 500 , 2};
    *(++myWeapons)={"Weapon 2" , 1500 , 2};
    *(++myWeapons)={"Weapon 3" , 4500 , 2};

///this can be equally be written as:-
weapons* myWeapons =new weapons[3];
weapons* newPtr=myWeapos;

    *newPtr={"Weapon 1" , 500 , 2};
    *(++newPtr)={"Weapon 2" , 1500 , 2};
    *(++newPtr)={"Weapon 3" , 4500 , 2};
///here the intial pointer value is preserved.



is this a good idea also?

1
2
3
4
5
6
7
weapons myWeapons( weapons &myWeapon[3] ) {
*myWeapons = {"Weapon 1" , 500 , 2};
++myWeapons;
*myWeapons = {"Weapon 2" , 1500 , 2};
++myWeapons;
*myWeapons = {"Weapon 3" , 4500 , 2};
}


Probably not because both your array and function identifiers have a similar signature, both of them can be converted to pointers am sure that might be quite problematic to a person reading your code or even possiblly to the compiler itself

Also your function should be returning a value.


how this works i mean the weapons myWeapons();

here myWeapons() is a function that returns an object of type weapons.

hope that helps
Last edited on
there is difference between assignment and intialization

Indeed. However,
1
2
3
4
5
6
7
8
9
10
int main()
{
    int bar [] = { 4, 7, 8 }; // Ok, old initialization syntax

    int foo [] { 4, 7, 8 }; // Ok. C++11 brace initialization

    int * x = new int[3] { 4, 7, 8 }; // Ok. C++11 brace initialization

    int * y = new int[3] = { 4, 7, 8 }; // Syntax error
}

The last one is what Lorence30 claims to have tried.
See http://www.informit.com/articles/article.aspx?p=1852519
closed account (SECMoG1T)
+1 @keskiverto thanks, hadn't thought of that ..
thanks guys
Topic archived. No new replies allowed.