explain the following declaration

A

Abhishek

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.
 
J

jacob navia

Abhishek said:
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
 
V

Vladimir S. Oka

Abhishek said:
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
 
L

Lucien Kennedy-Lamb

Abhishek said:
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
 
C

Christopher Benson-Manica

Lucien Kennedy-Lamb said:
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.
 
C

Christopher Benson-Manica

(a repost)

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

Jordan Abel

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.
 
A

Abhishek

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 said:
Abhishek said:
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
 
V

Vladimir S. Oka

Abhishek said:
jacob said:
Abhishek said:
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.
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.

Cheers

Vladimir
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top