By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,569 Members | 1,394 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,569 IT Pros & Developers. It's quick & easy.

explain the following declaration

P: n/a
Yet another option is to use pointers to arrays:

int (*array4)[NCOLUMNS] = malloc(nrows * sizeof(*array4));

or even

int (*array5)[NROWS][NCOLUMNS] = malloc(sizeof(*array5));

Please explain the declaration of the multidimension array as shown
above.
how will u access the elements? Please explain with the help of a
diagram if possible.
Thank you for your patience.

Jan 23 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Abhishek wrote:
Yet another option is to use pointers to arrays:

int (*array4)[NCOLUMNS] = malloc(nrows * sizeof(*array4));

or even

int (*array5)[NROWS][NCOLUMNS] = malloc(sizeof(*array5));

Please explain the declaration of the multidimension array as shown
above.
how will u access the elements? Please explain with the help of a
diagram if possible.
Thank you for your patience.

This looks like homework.

Please tell us the address of your teacher then we will mail
him/her the answer directly.

jacob
Jan 23 '06 #2

P: n/a
Abhishek wrote:
Yet another option is to use pointers to arrays:

int (*array4)[NCOLUMNS] = malloc(nrows * sizeof(*array4));

or even

int (*array5)[NROWS][NCOLUMNS] = malloc(sizeof(*array5));

Please explain the declaration of the multidimension array as shown
above.
how will u access the elements? Please explain with the help of a
diagram if possible.
"u" cannot access any elements at all, even if it tried to use a
diagram...
Thank you for your patience.


You're welcome. We love doing other people's homeworks.

Cheers

Vladimir

--
There are three things I always forget. Names, faces -- the third I
can't remember.
-- Italo Svevo

Jan 23 '06 #3

P: n/a

Abhishek wrote:
Yet another option is to use pointers to arrays:

int (*array4)[NCOLUMNS] = malloc(nrows * sizeof(*array4));

or even

int (*array5)[NROWS][NCOLUMNS] = malloc(sizeof(*array5));

Please explain the declaration of the multidimension array as shown
above.
how will u access the elements? Please explain with the help of a
diagram if possible.
Thank you for your patience.


This is a fair question and quite an uncommon syntax found in C.

C'mon you other guys, this is a forum to _help_ people with C
questions. Even if it is homework, let's provide some pointers (excuse
the pun).

OK, consider the following variable declarations:

int i;
int *pi; /* pointer to int */
int **ppi; /* pointer to pointer to int */
int ***pppi; /* pointer to pointer to pointer to int */

i = 1;
pi = &i;
ppi = π
pppi = &ppi;

Many people get confused by the levels of indirection you need to
access data in this kind of scenario.

Consider that:
printf("%d", i);
printf("%d", *pi);
printf("%d", **ppi);
printf("%d", ***pppi);

all will produce the same output. And so will:

printf("%d", pi[0]);
printf("%d", ppi[0][0]);
printf("%d", pppi[0][0][0]);

If you don't understand these concepts, then the answer to your
question will be very difficult to understand.

Now to follow your first example, consider an array:

int array3[NCOLUMNS];
Now I want a pointer to 'array3' called 'array4' please. It provides
one more level of indirection to the array.

int (*array4)[NCOLUMNS] = &array3;

To access the second element of the array you could use either:
array3[1]
or
(*array4)[1]

Now can you tell us how the 2D array (array5 in your example) would be
accessed the same way?

Feel free to ask more questions, but tell us how much you DO
understand. Which part is confusing you?

Lucien Kennedy-Lamb

Jan 24 '06 #4

P: n/a
Lucien Kennedy-Lamb <lu*****@gmail.com> wrote:
This is a fair question and quite an uncommon syntax found in C.
Yet not so uncommon that it isn't at least partially addressed by the
FAQ, which OP (as usual) seems not to have read.

http://www.c-faq.com/aryptr/ptrtoarray.html
C'mon you other guys, this is a forum to _help_ people with C
questions. Even if it is homework, let's provide some pointers (excuse
the pun).


Pointers are rewards for effort, a quality lacking in the original
post.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jan 24 '06 #5

P: n/a
Lucien Kennedy-Lamb <lu*****@gmail.com> wrote:
This is a fair question and quite an uncommon syntax found in C.
Yet not so uncommon that the FAQ does not at least in part address it.

http://www.c-faq.com/aryptr/ptrtoarray.html
C'mon you other guys, this is a forum to _help_ people with C
questions. Even if it is homework, let's provide some pointers (excuse
the pun).


Why reward OP's clear lack of effort?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jan 24 '06 #6

P: n/a
Christopher Benson-Manica <at***@ukato.freeshell.org> wrote:

(a repost)

Sorry for the weird semi-double post. News software was acting up.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jan 24 '06 #7

P: n/a
On 2006-01-23, Abhishek <ab*************@gmail.com> wrote:
Yet another option is to use pointers to arrays:

int (*array4)[NCOLUMNS] = malloc(nrows * sizeof(*array4));

or even

int (*array5)[NROWS][NCOLUMNS] = malloc(sizeof(*array5));

Please explain the declaration of the multidimension array as shown
above.
The cdecl program says

declare array4 as pointer to array NCOLUMNS of int
declare array5 as pointer to array NROWS of array NCOLUMNS of int
how will u access the elements? Please explain with the help of a
diagram if possible.


For the second example, you've got a pointer to a multidimensional
array, so dereference and then add the array subscripts

say, (*array5)[0][0]=42.
Jan 24 '06 #8

P: n/a
Hey Guys,
This is not a homework and I infact know a lot of C.
Was just going through the FAQs in C over the web and came across the
above declarations. I thought why not I put it over the group and
trigger a small intelectual discussion.
Happy to see you guys wanting me to to put in an effort before querying
you people.
And that is what I call as guidance.
GREAT!!! JOB keep that attitude going.
Bye

jacob navia wrote:
Abhishek wrote:
Yet another option is to use pointers to arrays:

int (*array4)[NCOLUMNS] = malloc(nrows * sizeof(*array4));

or even

int (*array5)[NROWS][NCOLUMNS] = malloc(sizeof(*array5));

Please explain the declaration of the multidimension array as shown
above.
how will u access the elements? Please explain with the help of a
diagram if possible.
Thank you for your patience.

This looks like homework.

Please tell us the address of your teacher then we will mail
him/her the answer directly.

jacob


Jan 24 '06 #9

P: n/a
Abhishek wrote:
jacob navia wrote:
Abhishek wrote:
Yet another option is to use pointers to arrays:

int (*array4)[NCOLUMNS] = malloc(nrows * sizeof(*array4));

or even

int (*array5)[NROWS][NCOLUMNS] = malloc(sizeof(*array5));

Please explain the declaration of the multidimension array as shown
above.
how will u access the elements? Please explain with the help of a
diagram if possible.
If this does not sound like homework assignment, I don't know what
does.
Thank you for your patience.
This looks like homework.

Please tell us the address of your teacher then we will mail
him/her the answer directly.

jacob


Hey Guys,


Please don't top post.
This is not a homework and I infact know a lot of C.
In which case you probably wouldn't need to ask these questions in the
first place. The way you put the original question implied only one
thing: homework.
Was just going through the FAQs in C over the web and came across the
above declarations. I thought why not I put it over the group and
trigger a small intelectual discussion.
If you wanted to trigger an intellectual discussion you could have
volunteered your opinion as well to start it off. That's how it usually
works, especially if you know a lot about subject matter.
Happy to see you guys wanting me to to put in an effort before querying
you people.
And that is what I call as guidance.
There's no better guidance than encouraging people to think before they
act.
GREAT!!! JOB keep that attitude going.
I will, thank you very much.
Bye


Cheers

Vladimir

Jan 24 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.