How to dereference an array element (C++) -
I wanted to create an array of a specific size using a variable, but the only way to do this is by pointers To use
integer size = 5; String * myArray = new string [size];
Now I want to assign another variable to this array, which I believe will only work through the definitions. The following line of code does not work How do I fix it?
string line; My array [0] = "hello"; Line = * Maya array [0]; Edit: I just want to clarify something: Using the normal "myArray [0]" code does not work either. It's compiled, but causes an accident. Here are some more specific codes that I want to do.
Zero MyClass :: setLine {string line} {myLine = line; / * This is a private variable in MyClass * /} ///////////// MyClass * element = New MyClass [size]; Elements [0] .setLine (myArray [0]);
I want to assign an array element to a class in a private variable, but the program crashes when I try to assign values to a personal variable.
If you know the size in time collection, you can use the normal array. If you know the size only on the runtime, you can still use std :: vector
, which is easier to use than manual allocation.
However, if you really want to know about pointers for array management, keep in mind that the index operator is equal to this addition and that is, ar [i]
Is similar, as * (ar + i)
. In other words, Indexing is just defying the offset.
In this way, no extra dereference is required. Just leave the star in the unsuccessful line.
Comments
Post a Comment