By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,381 Members | 1,561 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,381 IT Pros & Developers. It's quick & easy.

arrays and pointers question

P: n/a
I have structs that looks like this:

struct StructA
{
//members ..
};
struct StructB
{
//members ..
struct StructA * ptr_
};
ptr_ is actually a ptr to an array of StuctA types. My question is, how
may I index into the array (pointed to by ptr_) given an index - (I
intentionally ignored array bounds checking to keep the question simple)
May 25 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Bart Simpson <12**********@terrace.comwrites:
I have structs that looks like this:

struct StructA
{
//members ..
};
struct StructB
{
//members ..
struct StructA * ptr_
};
ptr_ is actually a ptr to an array of StuctA types. My question is,
how may I index into the array (pointed to by ptr_) given an index -
Given:
struct StructB b;
you may access the element with index `i' with:
b.ptr_[i]
--
"The fact that there is a holy war doesn't mean that one of the sides
doesn't suck - usually both do..."
--Alexander Viro
May 25 '07 #2

P: n/a
Given:
struct StructB b;
you may access the element with index `i' with:
b.ptr_[i]

But you have to allocate the memory because * ptr_ is pointed random
memory.
May 25 '07 #3

P: n/a
quasimodo wrote:
>Given:
struct StructB b;
you may access the element with index `i' with:
b.ptr_[i]


But you have to allocate the memory because * ptr_ is pointed random
memory.
struct StructA a[2];

b.ptr_ = a;

--
Tor <torust [at] online [dot] no>

May 25 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.