What is the best way to declare an index into an array?

I have three arrays, call them A[], B[] and C[],
indices into A make no sense as indices into
B[] or C[].  How should I declare A[], B[], C[],
indexA, indexB, and indexC so that indexA
cannot be used as an index into B[] or C[] and
indexA cannot be compared or set to indexB
or indexC?

Thanks,
  Ken
klopterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nadtCommented:
klopter,
if i get it straight then u want to do following thing...
1) u want to have 3 variable size array, right?

if this is the case then following solution can be implemented..

1) don't use arrays use pointers.  the declaration goes as follows...

void main()
{
(data_type) *A;
(data_type) *B;
(data_type) *C; //u can def. any data type such as `int' over here;

A=(data_type *)malloc(xyz);
B=(data_type *)malloc(abc);
C=(data_type *)malloc(def);//where xyz,abc,def are user defined variables have to be assigned appropriate values as per the requirement.
..
..
..
}


regards
nadt
0
KangaRooCommented:
I don't think you can do anything about it with plain arrays and indices. It is build in that the index for an array is a plain integer.

You could write a (template) array class, overload operator[] to accept only objects of a special type:

template<class T> T& Array::operator[](Array<T>::index_type i);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KangaRooCommented:
nadt, I don't think that was the question.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

klopterAuthor Commented:
Kangaroo is right - Your answer was
to a different question than the one
that I asked.

Ken
0
nadtCommented:
All right then explain the question more clearly.  What do u mean by `indices into A don't make sense as indices into B and C'???  This was the sentese that confused me.

Although i still defend my answer as follows:-
1)u can allocate as much space as u want for the pointer by sizeof operator.
2)u can make a data type and increment the space allocated to the pointer by sizeof operator and still manage to index it by numbers right?

What else can u ask for?
0
klopterAuthor Commented:
Kangaroo is right - Your answer was
to a different question than the one
that I asked.

Ken
0
klopterAuthor Commented:
nadt,
I have three tables, one for kids names (A[]), one
for state names (B[]) and one for the height of
buildings (C[]).

Kids have no state names, so it does not make sense
to index the state names array (A[]) with an
index corresponding to a kid.  Likewise it
makes no sense to index either names array
with an index corresponding to a building.

Ken
0
nietodCommented:
In C/C++ array indexes are always positive integers that start at 0.  So unrelated arrays will use the same indexes, there is no way around that.  its like saying "How do I number the floors of a building.  I can't use 1,2,3..., because peoples ages use 1,2,3..."  So what?
0
klopterAuthor Commented:
Fair enough.  

I understand that I can't have
everything that I think that I
would like.  Now I can continue
to use integers as indices without
thinking that I am perhaps underutilizing
the power of C++.

nietod,
  Thanks for your comments.  I was
pleased to get the e-mail saying that
you had responded because
I trust your advice.  I gave
Kanga the points for being the
first correct answer.

Ken

 
0
nietodCommented:
>> I trust your advice
Kagaroo is equally trustworthy--except when we dissagree. : -)
0
sumant032199Commented:
No,No,No,No,No.........

Remember C++ is godess of all languages.
You can do what are you asking for.
If I am not making a mistake then Kangaroo has done it. I am ready with a code which does exactly what you have described. I took nearly 3 hours to think and build that code(really great efforts). It is small but very much tricky. By that time I lost the chance of answering your question. Now I am here seeing that question is answered. But you please, please trust me and give me another  chance of 70 points again and I will prove my point.

Please klopter, do it for me.
0
nietodCommented:
C++ only accepts integers as indexes to arrays.  period.  You can make it appear like it accepts other types of indexes, kangaroo pointed out that you can do that by overloading operator [], but that is really only for appearances, in the end, if there is an array involved, you will end up using an integer index (or add an integer onto to the array pointer).  Another cosmetic option would be to use enums, like

enum ordinal
{
   first,
   second,
   third
};

int Array[3];

Array[first] = 1;

But again this is a cosmetic trick, the enum is converted to an integer that is used in the array.
0
klopterAuthor Commented:

My arrays may be arbitrarily large.
So, unless there is a way to make an enum
that of arbitrary size, each an integer that
can only be used in limited ways, I am -
as you pointed out earlier - stuck iwth using
integers for array indices.  Not a bad place
to be stuck though.

Ken
0
nietodCommented:
>> My arrays may be arbitrarily large.
Thsi sort of approach is usually used in cases where you have a well defined and finite set of options that are really not numerical in nature.  Like You may have an array based on car model and use an enum index like

enum Model
{
   Tercel,
   Corola,
   Camary,
};

For an arbitrarily large array numerical indexes are ideal.  One thing C/C++ is missing is the ability to control the range of the indexes, though, they must start at 0, which is not true in most other languages.  Again that can be "gotten around" by using classes and overloading operator [].
   
0
KangaRooCommented:
As a very brief example of an Array class implementation with type specific indexing. I just wonder if it's worth the trouble.

template<class T> class Array
{
  private:
     T* array;
 
  public:
     class index
     {
        friend class Array;
        private: int idx
        public:
          index& operator ++ () { return ++idx;}
          index& operator +(constindex&);        // etc... all necesary operators
     };

     T& operator[](const index& idx) { return array[idx.idx];}
};
0
sumant032199Commented:
Hi nietod and Kangaroo !
Will you please add a comment on this answer. I know you must spend your 10 points for that. But please.

http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=10287183 

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.