## Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of posts: 16951

This Forum Only

Confused about pointer to array Posted by hajimeml on 21 Jun 2009 at 6:45 PM
Hello. Could anybody please let me know what the following means? I am uncertain the meaning of (*KM)[i]. I am not sure but it seems that Vec3dArray is an array where each element is a vector of 3 doubles.
So, KM may be a 2-d array of 3 columns and a run-time determined number of rows. Am I right? If so, what (*KM)[i] in the loop means? Thanks.

Vec3d is a vector of 3 doubles.
typedef vector<Vec3d> Vec3dArray
KM = new Vec3dArray(num) where num is an integer

for (i = 0; i < num; i++) {
(*KM)[i] = Vec3d::Zero();
}
Re: Confused about pointer to array Posted by Lundin on 21 Jun 2009 at 11:48 PM
: Vec3d is a vector of 3 doubles.
: typedef vector<Vec3d> Vec3dArray
: KM = new Vec3dArray(num) where num is an integer

Judging by the above, KM must be declared as

Vec3dArray* KM;

Which means that *KM simply accesses the Vec3dArray.
[i] will call the index operator inside the vector class to get the element.

So... no multiple dimensions anywhere outside the vector.
Re: Confused about pointer to array Posted by hajimeml on 22 Jun 2009 at 12:19 AM
Thanks for the reply but I am still a bit confused. Do you mean the following?

KM->[ [1.0 2.0 3.0] ]
[ [4.0 5.0 6.0] ]
[ [7.0 8.0 9.0] ]
[ [1.1 2.2 3.3] ]

(*KM)[0] is [1.0 2.0 3.0]

(*KM)[1] is [4.0 5.0 6.0]

(*KM)[3] is [1.1 2.2 3.3]

where num in the original post is set to four.
I am not sure but maybe num is the number of rows. Any idea?
Re: Confused about pointer to array Posted by Lundin on 22 Jun 2009 at 5:38 AM
: Thanks for the reply but I am still a bit confused. Do you mean the
: following?
:
: KM->[ [1.0 2.0 3.0] ]
: [ [4.0 5.0 6.0] ]
: [ [7.0 8.0 9.0] ]
: [ [1.1 2.2 3.3] ]
:
: (*KM)[0] is [1.0 2.0 3.0]
:
: (*KM)[1] is [4.0 5.0 6.0]
:
: (*KM)[3] is [1.1 2.2 3.3]

Yes that is correct.

: where num in the original post is set to four.
: I am not sure but maybe num is the number of rows. Any idea?

"num" is the number of rows, which corresponds to the number of objects the programmer wants allocated inside the vector upon creation. It isn't necessary to specify it, as the vector class relies on dynamic memory allocation. The reason why you would specify the number of rows is only execution-time efficiency: they will be allocated at startup instead of during the push_back() to the vector.
Re: Confused about pointer to array Posted by hajimeml on 22 Jun 2009 at 5:55 AM
Lundin, Thank you very much for your nice explaination. It is now clear.

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic