Array's again
-
What I was wondering is when i put. const size_t array = 3; int ca[array] = {0,1,2}; cout << ca << endl; The program prints 0x75dfc0 (or something like that, I'm just guessing) that output tells me nothing. Is it the memory address or what. it all seems a little confusing.
#hackC++ wrote:
const size_t array = 3; int ca[array];
1. you cannot do this. you must use either macros or dynamic memory allocation.
#hackC++ wrote:
cout << ca << endl;
this prints
ca
which is a pointer to the first int which compounds the array, so what you get is actually the address at which the array has been allocated... if you want to print each elements in the array, iterate over it. -
What I was wondering is when i put. const size_t array = 3; int ca[array] = {0,1,2}; cout << ca << endl; The program prints 0x75dfc0 (or something like that, I'm just guessing) that output tells me nothing. Is it the memory address or what. it all seems a little confusing.
-
#hackC++ wrote:
const size_t array = 3; int ca[array];
1. you cannot do this. you must use either macros or dynamic memory allocation.
#hackC++ wrote:
cout << ca << endl;
this prints
ca
which is a pointer to the first int which compounds the array, so what you get is actually the address at which the array has been allocated... if you want to print each elements in the array, iterate over it. -
#hackC++ wrote:
const size_t array = 3; int ca[array];
1. you cannot do this. you must use either macros or dynamic memory allocation.
#hackC++ wrote:
cout << ca << endl;
this prints
ca
which is a pointer to the first int which compounds the array, so what you get is actually the address at which the array has been allocated... if you want to print each elements in the array, iterate over it.#hackC++ wrote:
const size_t array = 3; int ca[array];
v2.0 wrote:
1. you cannot do this. you must use either macros or dynamic memory allocation.
Yes we can, because it is const. At least my GCC compiles this code.
Maxwell Chen
-
#hackC++ wrote:
Would it be easier to use vectors in most cases?
depends what you need to do with it. if you need an array which size need to change often, then yes, vector is certainly a good choice. please explain what you want to do with that array if you want me to advise you better.
-
#hackC++ wrote:
const size_t array = 3; int ca[array];
1. you cannot do this. you must use either macros or dynamic memory allocation.
#hackC++ wrote:
cout << ca << endl;
this prints
ca
which is a pointer to the first int which compounds the array, so what you get is actually the address at which the array has been allocated... if you want to print each elements in the array, iterate over it.v2.0 wrote:
#hackC++ wrote: const size_t array = 3; int ca[array]; 1. you cannot do this.
You can in C++ (not in C).
v2.0 wrote:
#hackC++ wrote: cout << ca << endl; this prints ca which is a pointer to the first int which compounds the array, so what you get is actually the address at which the array has been allocated... if you want to print each elements in the array, iterate over it.
'iterate' means a for-loop ;)
-
v2.0 wrote:
#hackC++ wrote: const size_t array = 3; int ca[array]; 1. you cannot do this.
You can in C++ (not in C).
v2.0 wrote:
#hackC++ wrote: cout << ca << endl; this prints ca which is a pointer to the first int which compounds the array, so what you get is actually the address at which the array has been allocated... if you want to print each elements in the array, iterate over it.
'iterate' means a for-loop ;)
-
Well i'm currently reading C++ Primer 4th Edition by Stanley Lippman. just got done reading about vectors and went into arrays which is confusing me to say the least. The complier I'm using is Bloodshed Dev C++. By the way, whats the best software for C++. i'm guessing Visual Basic C++ 2003 but i could be wrong. -- modified at 14:00 Tuesday 25th April, 2006
-
Well i'm currently reading C++ Primer 4th Edition by Stanley Lippman. just got done reading about vectors and went into arrays which is confusing me to say the least. The complier I'm using is Bloodshed Dev C++. By the way, whats the best software for C++. i'm guessing Visual Basic C++ 2003 but i could be wrong. -- modified at 14:00 Tuesday 25th April, 2006
#hackC++ wrote:
Well i'm currently reading C++ Primer 4th Edition by Stanley Lippman.
when you get stronger C++ programmer, i advise you to read The C++ Language by Bjarne Stroustrup (the C++ Creator).
#hackC++ wrote:
By the way, whats the best software for C++.
i'm not sure about what you're saying here, but if you ask for an IDE, then i think most people here will tell you that Microsoft Visual C++ 2003/2005 worth it (because Codeproject is dedicated to microsoft technologies). you can get Visual Studio 2005 Express edition for free on MS web site.
-
What I was wondering is when i put. const size_t array = 3; int ca[array] = {0,1,2}; cout << ca << endl; The program prints 0x75dfc0 (or something like that, I'm just guessing) that output tells me nothing. Is it the memory address or what. it all seems a little confusing.
You can't output a whole array like that. What's happening is an implicit conversion from
int[]
toint*
, and you're seeing the value of that pointer (the address ofarray[0]
)--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
-
Well i'm currently reading C++ Primer 4th Edition by Stanley Lippman. just got done reading about vectors and went into arrays which is confusing me to say the least. The complier I'm using is Bloodshed Dev C++. By the way, whats the best software for C++. i'm guessing Visual Basic C++ 2003 but i could be wrong. -- modified at 14:00 Tuesday 25th April, 2006
#hackC++ wrote:
just got done reading about vectors and went into arrays which is confusing me to say the least.
Technically, a vector is a one-dimensional array. Things get confusing because C++ has a
vector
type.#hackC++ wrote:
By the way, whats the best software for C++.
It depends on what you use to measure with. One person might like it for features A, B, and C, while the next person hates those features but likes X, Y, and Z instead.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"We will be known forever by the tracks we leave." - Native American Proverb