455,116 Members | 1,285 Online
Need help? Post your question and get tips & solutions from a community of 455,116 IT Pros & Developers. It's quick & easy.

# Array of pointers to arrays of Fixed Size

 P: n/a 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 Jul 25 '05 #1
11 Replies

 P: n/a Linny wrote: 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]; Jul 25 '05 #2

 P: n/a "Linny" schrieb im Newsbeitrag news:11**********************@f14g2000cwb.googlegr oups.com... 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 Jul 25 '05 #3

 P: n/a 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. Jul 25 '05 #4

 P: n/a Linny wrote: 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) Jul 25 '05 #5

 P: n/a "ve*********@hotmail.com" wrote: Linny wrote: and so on... But in this case a user can also point it to an array of a different size... say 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. It's altogether better not to require a fixed size in the first place. Richard Jul 25 '05 #6

 P: n/a 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. Jul 25 '05 #7

 P: n/a Linny wrote: 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 Jul 25 '05 #8

 P: n/a Linny wrote: 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[i])[j] i.e., to get to arr1[5] use (*arrp[1])[5] Jul 25 '05 #9

 P: n/a On 25 Jul 2005 02:09:42 -0700, "vindhya" wrote in comp.lang.c: 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. -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html Jul 26 '05 #10