473,657 Members | 2,409 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6746


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*******@gmai l.com> schrieb im Newsbeitrag
news:11******** **************@ f14g2000cwb.goo glegroups.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*********@ho tmail.com" <ve*********@ho tmail.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*********@gm ail.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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 26 '05 #10

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

Similar topics

2
6447
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 "*?<>/\|\\" If one of the chars are found, I'd like to replace it with a "-" and then commit the change to the actual file system. I know that a scripting language may be better suited for this, but I'm experimenting with C and I'm trying to...
4
157988
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
7402
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 example, the manner in which I update element 1 depends on several other (randomly numbered) elements in the array. So, I can't change an element until I've figured out how every element changes.
12
472
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 same type). eg: int arr1;//Array of 20 ints int arr2; int arr3; ............
12
1746
by: junky_fellow | last post by:
How can I declare a function that returns a pointer to one dimensional array ?
5
3123
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
7395
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 in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
17
7239
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 to show the array data to the end user. Can I do that? How?
33
7162
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 array, so im inputting the dimensions of those myself. The problem is that the output array (C=A*B) has as many rows as A and as many columns as B. I would think of initialising C with:
0
8826
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7330
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.