|
|
arr
is not declaredsavigArray
is misspelled
|
|
|
|
|
|
|
|
|
|
int i; int i = 0; // doesn't compile!
, vs. int i; i = 0; // just fine!
.
|
|
arr.array
is a pointer which has not yet been initialised. The new []
operator returns an address which will be stored in the pointer variable. It would not make sense to try to access an element of the dynamic array using the square bracket syntax before the pointer actually pointed to anything. In addition, each element of the array is an int
, and it would be a type mismatch if you tried to store an address in an int.
|
|
|
|
arr
is a local variable of type x
. arr.array
is an uninitialised pointer. It is an error to dereference an uninitialised pointer like this:
|
|
|
|
|
|
0 2 4 6 8 10 12 14 16 18 0 4 8 12 16 20 24 28 32 36 0 -901 0 0 0 0 -901 0 0 9 8 7 18 16 14 Program ended with exit code: 0 |
You shouldn't write the type of an identifier unless you want to declare an identifier. |
|
|
the variable arr.array is an uninitialised pointer. It is an error to dereference an uninitialised pointer like this: arr.array[i] = savingArray; |
std::ostream or std::cout , is it called Mapping ? |
using namespace std;
common in learning environments. std:: etc reduces the likelihood of function name (namespace) clashes by explicit reference to the function namespace eg std:: It is good practice to adopt the explicit form. It is a little harder to get used to and read but it pays off.Ok, so if I need to declare an array |
arr
by putting empty brackets after the name, you're doing it wrong. It won't compile. arr
's value, then you'd put brackets with some integral expression in them, like you're doing array[i]
. using namespace whatever;
using namespace
opens the door to name collisions which in the worst case may silently change the behavior of your program.
|
|
main arr = 0 main arr = 3 test1 arr = 0 test1 arr = 10 main arr = 3 |
arr
inside function test1()
is a completely separate and independent variable from arr
inside main()
. Changes made inside main do not affect the value inside test1 and vice versa.
|
|
test1 5 main 4567 |
new []
/ delete []
Function test1 creates a local variable, allocates some memory, uses that memory. Then the memory is released before the variable goes out of scope and is destroyed.the variable arr inside function test1() is a completely separate and independent variable from arr inside main(). . |
Kourosh23 wrote: | ||
---|---|---|
Thank your for your response, I understand arr.array is a pointer, and we can initialize it using the struct
Question: Does the element 10 store in the first free array location (e.g. index 0) ? Because I believe the pointer gets the value and transfers it to address that is given to the array. |
delete [] arr.array;
because the address of the memory has been lost.Does the element 10 store in the first free array location (e.g. index 0) ? |
|
|
|
|
Kourosh23 wrote: | |||
---|---|---|---|
Also, I don't understand why it is an error ? I am simply passing value to the pointer which is accessing it from the struct. Later after I am done with it, I delete it. If you could maybe show me the correct code , in that case I can relate stuff because I am new to C++ :D
|
|
|
|
|
|
|
|
|
index: 0 value: 88 index: 1 value: -207 index: 2 value: 3521 That's it Program ended with exit code: 0 |
|
|
index: 1 value: 88 index: 2 value: -207 index out of range: 3 That's it |
At line 2, the address of the newly allocated block of memory is stored in the pointer. Then at line 3, the address is overwritten by some completely different value |
|
|
|