Array of pointers to arrays of Fixed Size

L

Linny

Hi,
I need some help in declaring an array of pointers to array of a
certain fixed size. I want the pointers to point to arrays of fixed
size only (should not work for variable sized arrays of the same type).

eg:
int arr1[20];//Array of 20 ints
int arr2[20];
int arr3[20];
............
int arrn[20];


int *arrofptr[10];//Array of 10 pointers to int


arrofptr[0] = arr1;
arrofptr[1] = arr2;


and so on...
But in this case a user can also point it to an array of a different
size... say
int arr4[30];// Size 30
arrofptr[3]=arr4;


So is there a way to declare the array of pointers so that they point
to arrays of a FIXED size only,without any runtime check on the size of

arrays being pointed to ?


-Linvin
 
V

velthuijsen

Linny said:
Hi,
I need some help in declaring an array of pointers to array of a
certain fixed size. I want the pointers to point to arrays of fixed
size only (should not work for variable sized arrays of the same type).

eg:
int arr1[20];//Array of 20 ints
int arr2[20];
int arr3[20];
...........
int arrn[20];


int *arrofptr[10];//Array of 10 pointers to int


arrofptr[0] = arr1;
arrofptr[1] = arr2;


and so on...
But in this case a user can also point it to an array of a different
size... say
int arr4[30];// Size 30
arrofptr[3]=arr4;


So is there a way to declare the array of pointers so that they point
to arrays of a FIXED size only,without any runtime check on the size of
arrays being pointed to ?

Easy don't give the user an option to fill in the size of the array.

If you mean another programmer instead of user keep in mind that
another programmer can just ignore anything you want and do whatever
(s)he wants.
That said you can put in comments stating that the arrays have to be a
certain fixed size and then using asserts / exceptions to enforce that
size.

Oh and please do not use magic numbers like that get in a habit of
doing the following:
const unsigned int ARRAYSIZE = 20;
int arr1[ARRAYSIZE];
int arr2[ARRAYSIZE];
.....
int arrn[ARRAYSIZE];
 
C

Christian Meier

Linny said:
Hi,
I need some help in declaring an array of pointers to array of a
certain fixed size. I want the pointers to point to arrays of fixed
size only (should not work for variable sized arrays of the same type).

eg:
int arr1[20];//Array of 20 ints
int arr2[20];
int arr3[20];
...........
int arrn[20];


int *arrofptr[10];//Array of 10 pointers to int


arrofptr[0] = arr1;
arrofptr[1] = arr2;


and so on...
But in this case a user can also point it to an array of a different
size... say
int arr4[30];// Size 30
arrofptr[3]=arr4;


So is there a way to declare the array of pointers so that they point
to arrays of a FIXED size only,without any runtime check on the size of

arrays being pointed to ?


-Linvin

AFAIK it is not possible.
Try a workarround. For example you could create a struct with your fixed
array in it:

const unsigned int C_uiFixedSize = 20;

struct FixedArray
{
int arr[20];
};

int main()
{
FixedArray arr1;
FixedArray *arrofptr[10];
arrofptr[0] = &arr1;
}



Greets Chris
 
V

vindhya

May be you can use a class for that.....

class my_array {

public:
int arr1[20];//Array of 20 ints
int arr2[20];
int arr3[20];
...........
int arrn[20];

int *arrofptr[10];//Array of 10 pointers to int

arrofptr[0] = arr1;
arrofptr[1] = arr2;
.............

//define the method for copy self copy constructor
int * operator =(int *) {
//place the method to perform the check that user is not trying
to disrupt the logic
}
}

In this case if you do not permit then user will be not able to copy
the array to different size.
 
J

junky_fellow

Linny said:
Hi,
I need some help in declaring an array of pointers to array of a
certain fixed size. I want the pointers to point to arrays of fixed
size only (should not work for variable sized arrays of the same type).

eg:
int arr1[20];//Array of 20 ints
int arr2[20];
int arr3[20];
...........
int arrn[20];


int *arrofptr[10];//Array of 10 pointers to int


arrofptr[0] = arr1;
arrofptr[1] = arr2;


and so on...
But in this case a user can also point it to an array of a different
size... say
int arr4[30];// Size 30
arrofptr[3]=arr4;


So is there a way to declare the array of pointers so that they point
to arrays of a FIXED size only,without any runtime check on the size of

arrays being pointed to ?


-Linvin

You may try:
int (*arrofptr[10])[20];
int arr1[20];
arr1[5] = 100;
ptr[0] = &arr1;

Now arr1[5] may be accessed by *(**ptr + 5)
 
L

Linny

Thanks but not relevant to my question
I wanted to know how can we restrict the user to point a fixed sized
string to, a pointer from an array of pointers, without runtime check
of array size.
 
D

Default User

Linny said:
Thanks but not relevant to my question
I wanted to know how can we restrict the user to point a fixed sized
string to, a pointer from an array of pointers, without runtime check
of array size.

1. Learn to post properly on usenet, this includes quoting a relevant
portion of the previous message. To do so from Google, click "show
options" and use the Reply shown in the expanded header.

2. Figure out which language you want. You posted this to both
comp.lang.c and comp.lang.c++. The answers may differ, especially when
people begin suggesting expanded scope.





Brian
 
J

John Bode

Linny said:
Hi,
I need some help in declaring an array of pointers to array of a
certain fixed size. I want the pointers to point to arrays of fixed
size only (should not work for variable sized arrays of the same type).

eg:
int arr1[20];//Array of 20 ints
int arr2[20];
int arr3[20];
...........
int arrn[20];


int *arrofptr[10];//Array of 10 pointers to int


arrofptr[0] = arr1;
arrofptr[1] = arr2;


and so on...
But in this case a user can also point it to an array of a different
size... say
int arr4[30];// Size 30
arrofptr[3]=arr4;


So is there a way to declare the array of pointers so that they point
to arrays of a FIXED size only,without any runtime check on the size of

arrays being pointed to ?


-Linvin

#define N ...
#define LEN 20

int arr0[LEN];
int arr1[LEN];
int arr2[LEN];
....
int arrn[LEN];

/*
** arrp is an array of pointers to 20-element arrays of int
*/
int (*arrp[N+1])[LEN];

arrp[0] = &arr0; /* note the '&' operator! */
arrp[1] = &arr1;
....
arrp[N] = &arrn;

You should get a warning if you attempt to assign a pointer to an array
that isn't 20 elements; i.e.,

int arr5[30];
arrp[5] = &arr5;

A warning's better than nothing, but it won't stop compilation. A
suitable runtime test would be

if (sizeof arr5 == sizeof *arrp[0])
{
arrp = &arr5;
}
else
{
/* arr5 isn't the right type */
}

To access elements of the pointed-to arrays, use

(*arrp)[j]

i.e., to get to arr1[5] use (*arrp[1])[5]
 
J

Jack Klein

May be you can use a class for that.....

Why are you crossposting to comp.lang.c++? The OP posted his question
to comp.lang.c ONLY.
class my_array {

And why are you answering a question about C, asked only in
comp.lang.c, with code that is not valid C?

And finally, if you are going to use the broken Google beta interface
instead of a real newsreader and server, read one of Chuck Falconer's
posts, mostly in comp.lang.c only, about how to quote properly. The
fact that you choose to use a broken posting mechanism does not excuse
bad manners, it just means you need to do the extra work to reply
correctly.
 
J

Jack Klein

Why are you crossposting to comp.lang.c++? The OP posted his question
to comp.lang.c ONLY.


And why are you answering a question about C, asked only in
comp.lang.c, with code that is not valid C?

Sorry for the above part of the reply. For some reason, the OP's post
appearing in comp.lang.c was posted only to comp.lang.c. But the one
here in comp.lang.c++ is crossposted to both groups. Either Google or
the OP did something strange.
And finally, if you are going to use the broken Google beta interface
instead of a real newsreader and server, read one of Chuck Falconer's
posts, mostly in comp.lang.c only, about how to quote properly. The
fact that you choose to use a broken posting mechanism does not excuse
bad manners, it just means you need to do the extra work to reply
correctly.

This part still applies, however.
 
L

Linny

Default said:
1. Learn to post properly on usenet, this includes quoting a relevant
portion of the previous message. To do so from Google, click "show
options" and use the Reply shown in the expanded header.

2. Figure out which language you want. You posted this to both
comp.lang.c and comp.lang.c++. The answers may differ, especially when
people begin suggesting expanded scope.

Brian

Opps Sorry !! I'll take that into account in my next post. Thanks
 
S

Steven Kobes

Linny said:
I need some help in declaring an array of pointers to array of a
certain fixed size. I want the pointers to point to arrays of
fixed size only (should not work for variable sized arrays of the
same type).

You may try:
int (*arrofptr[10])[20];
int arr1[20];
arr1[5] = 100;
ptr[0] = &arr1;

Now arr1[5] may be accessed by *(**ptr + 5)

Linny said:
Thanks but not relevant to my question

It seems relevant to me.
int (*p[10])[20];
declares an array of 10 pointers which will only point to arrays of a
fixed size (namely 20), which is exactly what you said you wanted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top