473,324 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Array of pointers to arrays of Fixed Size

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 6646


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
"Linny" <li*******@gmail.com> 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
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


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
"ve*********@hotmail.com" <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.


<http://www.catb.org/~esr/jargon/html/C/C-Programmers-Disease.html>

It's altogether better not to require a fixed size in the first place.

Richard
Jul 25 '05 #6
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
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


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
On 25 Jul 2005 02:09:42 -0700, "vindhya" <ma*********@gmail.com> 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
On Mon, 25 Jul 2005 21:45:53 -0500, Jack Klein <ja*******@spamcop.net>
wrote in comp.lang.c++:
On 25 Jul 2005 02:09:42 -0700, "vindhya" <ma*********@gmail.com> 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?


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.

--
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 #11

Default User wrote:
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


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

Jul 26 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: hokieghal99 | last post by:
I wish to place all files and directories that are within a user defined path (on a Linux x86 PC) into some type of array and then examine those items for the existence of certain charaters such as...
4
by: Isaac | last post by:
Hi mates I want to know a simple program of return array from function ? Do I need to use pointer to return the address of the first element in an array. Isaac
7
by: simkn | last post by:
Hello, I'm writing a function that updates an array. That is, given an array, change each element. The trick is this: I can't change any elements until I've processed the entire array. For...
12
by: Linny | last post by:
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...
12
by: junky_fellow | last post by:
How can I declare a function that returns a pointer to one dimensional array ?
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.