473,396 Members | 2,121 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,396 software developers and data experts.

explain the following declaration

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
9 1735
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
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

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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ranyart Olias | last post by:
Can anyone explain why the <root@hiddenworld.net> portion of this html doesn't render in a browser, but the <20031010152346.GA15353@server.hiddenworld.net> portion does? Here is the html...
3
by: Sid | last post by:
Hi folks, I wrote this code to test if I get the same address for all the variables in a union but for some reason the address I got when I used a char variable in a union seems bizarre- can...
21
by: Gactimus | last post by:
Can anyone explain what the lines with the '*' by them do? ----------- #ifndef _COUNTER_H #define _COUNTER_H #include <iostream> using namespace std; class Counter
11
by: Kalle Rutanen | last post by:
Hello Here is a short code snippet which does not compile. Could someone explain why this is ? class A { public: void set(int a) {
8
by: huhu | last post by:
Hi, I meet a sentence: ------------------------------------------------ struct intNode *(*fpt)(void)= {createList, inNode, delNode,revNode,NULL};...
12
by: Andrew Ducker | last post by:
And no, this isn't a complaint about break - I'm very happy to make things explicit. However, why isn't the format something like: switch(myVariable) { case 1: { //Do Something
10
by: Jeff Boes | last post by:
I'm hoping there's someone here with experience in building the Visual Explain tool from Red Hat. I downloaded it and the J2 SDK, but when I attempt to follow the build instructions, I get messages...
6
by: blue | last post by:
The following is from http://developer.yahoo.com/yui/examples/dragdrop/dd-ontop.html What does the (function(){ })(); syntax actually do? I'm going to guess that is creates an unnamed function?!...
11
by: Pranav | last post by:
The Code is compiling without Error/Bug/Exception.., What are the possibilities for this behaviour? //*************************************************************** #include<stdio.h> typedef...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...
0
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,...

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.