Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 25th, 2005, 09:55 AM
Linny
Guest
 
Posts: n/a
Default 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

  #2  
Old July 25th, 2005, 10:15 AM
velthuijsen@hotmail.com
Guest
 
Posts: n/a
Default Re: Array of pointers to arrays of Fixed Size



Linny wrote:[color=blue]
> 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 ?[/color]

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];

  #3  
Old July 25th, 2005, 10:15 AM
Christian Meier
Guest
 
Posts: n/a
Default Re: Array of pointers to arrays of Fixed Size

"Linny" <linvin333@gmail.com> schrieb im Newsbeitrag
news:1122281109.468349.114640@f14g2000cwb.googlegr oups.com...[color=blue]
> 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
>[/color]

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


  #4  
Old July 25th, 2005, 10:15 AM
vindhya
Guest
 
Posts: n/a
Default Re: Array of pointers to arrays of Fixed Size

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.

  #5  
Old July 25th, 2005, 10:35 AM
junky_fellow@yahoo.co.in
Guest
 
Posts: n/a
Default Re: Array of pointers to arrays of Fixed Size



Linny wrote:[color=blue]
> 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[/color]

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)

  #6  
Old July 25th, 2005, 10:45 AM
Richard Bos
Guest
 
Posts: n/a
Default Re: Array of pointers to arrays of Fixed Size

"velthuijsen@hotmail.com" <velthuijsen@hotmail.com> wrote:
[color=blue]
> Linny wrote:[color=green]
> > and so on...
> > But in this case a user can also point it to an array of a different
> > size... say[/color][/color]
[color=blue][color=green]
> > 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 ?[/color]
>
> Easy don't give the user an option to fill in the size of the array.[/color]

<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
  #7  
Old July 25th, 2005, 11:35 AM
Linny
Guest
 
Posts: n/a
Default Re: Array of pointers to arrays of Fixed Size

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.

  #8  
Old July 25th, 2005, 07:55 PM
Default User
Guest
 
Posts: n/a
Default Re: Array of pointers to arrays of Fixed Size

Linny wrote:
[color=blue]
> 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.[/color]

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
  #9  
Old July 25th, 2005, 08:45 PM
John Bode
Guest
 
Posts: n/a
Default Re: Array of pointers to arrays of Fixed Size



Linny wrote:[color=blue]
> 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[/color]

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

  #10  
Old July 26th, 2005, 03:55 AM
Jack Klein
Guest
 
Posts: n/a
Default Re: Array of pointers to arrays of Fixed Size

On 25 Jul 2005 02:09:42 -0700, "vindhya" <manish.sing@gmail.com> wrote
in comp.lang.c:
[color=blue]
> May be you can use a class for that.....[/color]

Why are you crossposting to comp.lang.c++? The OP posted his question
to comp.lang.c ONLY.
[color=blue]
> class my_array {[/color]

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
  #11  
Old July 26th, 2005, 04:15 AM
Jack Klein
Guest
 
Posts: n/a
Default Re: Array of pointers to arrays of Fixed Size

On Mon, 25 Jul 2005 21:45:53 -0500, Jack Klein <jackklein@spamcop.net>
wrote in comp.lang.c++:
[color=blue]
> On 25 Jul 2005 02:09:42 -0700, "vindhya" <manish.sing@gmail.com> wrote
> in comp.lang.c:
>[color=green]
> > May be you can use a class for that.....[/color]
>
> Why are you crossposting to comp.lang.c++? The OP posted his question
> to comp.lang.c ONLY.
>[color=green]
> > class my_array {[/color]
>
> And why are you answering a question about C, asked only in
> comp.lang.c, with code that is not valid C?[/color]

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.
[color=blue]
> 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.[/color]

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
  #12  
Old July 26th, 2005, 09:35 AM
Linny
Guest
 
Posts: n/a
Default Re: Array of pointers to arrays of Fixed Size


Default User wrote:[color=blue]
> Linny wrote:
>[color=green]
> > 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.[/color]
>
> 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[/color]

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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles