Link to home
Start Free TrialLog in
Avatar of klopter
klopter

asked on

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
Avatar of nadt
nadt

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
ASKER CERTIFIED SOLUTION
Avatar of KangaRoo
KangaRoo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
nadt, I don't think that was the question.
Avatar of klopter

ASKER

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

Ken
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?
Avatar of klopter

ASKER

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

Ken
Avatar of klopter

ASKER

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
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?
Avatar of klopter

ASKER

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

 
>> I trust your advice
Kagaroo is equally trustworthy--except when we dissagree. : -)
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.
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.
Avatar of klopter

ASKER


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
>> 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 [].
   
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];}
};
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.

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