473,386 Members | 1,745 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,386 software developers and data experts.

pointers and array

How do you use a double pointer to access a two dimensional array.

ie int a[3][3];
int **p;

-
-
-
-
-

etc.
thanks
Anthony
Nov 14 '05 #1
11 1498
Anthony Moss wrote:

How do you use a double pointer to access a two dimensional array.

ie int a[3][3];
int **p;


int (*p)[sizeof *a / sizeof **a] = a;

--
pete
Nov 14 '05 #2


Anthony Moss wrote:
How do you use a double pointer to access a two dimensional array.

ie int a[3][3];
int **p;


int (*p)[3];

Example:
#include <stdio.h>

int main(void)
{
int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int (*p)[3];

p = a;
printf("a[1][1] = %d\np[1][1] = %d\n",
a[1][1],p[1][1]);
return 0;
}

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #3
"Anthony Moss" <a.*@baesystems.com> wrote:
How do you use a double pointer to access a two dimensional array.

ie int a[3][3];
int **p;


You don't. If you want to work with the sub-arrays of a (which is rare),
you need a pointer to an array: int (*p)[3]. If, OTOH, you want to work
with the individual int members, you need a simple int *.
The declaration int ** p means that p is a pointer _to a pointer_ to
int. You have no pointers to int to point at, so you should not be using
an int **.

Richard
Nov 14 '05 #4
In <40********@baen1673807.greenlnk.net> "Anthony Moss" <a.*@baesystems.com> writes:
How do you use a double pointer to access a two dimensional array.

ie int a[3][3];
int **p;


You don't. You may want to read the FAQ, BTW.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #5
> How do you use a double pointer to access a two dimensional array.

ie int a[3][3];
int **p;


Be careful not to confuse:

int a[][]

with:

int * a[]

These are completely different beasts.

While with:

int a[m][n]

a[i][j] is the same as: *(a + n*i + j)

with:

int * a[n]

a[i][j] is the same as: *(*(a + i) + j)

Nov 14 '05 #6
Dan Pop wrote:
In <40********@baen1673807.greenlnk.net> "Anthony Moss" <a.*@baesystems.com> writes:

How do you use a double pointer to access a two dimensional array.

ie int a[3][3];
int **p;

You don't. You may want to read the FAQ, BTW.

Dan


Specifically question 6.2. I was just helping someone with this last night, and
it is very confusing.

int a[1][1] = {{0}};
int (*p)[1] = a;
int **q = (int **)a;

printf("p=%p p[0]=%p\n", (void *)p, (void *)p[0]);
printf("q=%p q[0]=%p\n", (void *)q, (void *)q[0]);

produces (for example)

p=0x80605412 p[0]=0x80605412
q=0x80605412 q[0]=(nil)

So, even though p and q have the same value, p[0] and q[0] do not. This seems to
be entirely due to the fact that p[0] is array-of-int while q[0] is
pointer-to-int. p and p[0] have the same value for the same reason that &a and a
have the same value. The fact that the treatment and interpretation of p[0] and
q[0] are different is just an artifact of the type system.

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Nov 14 '05 #7
Guillaume wrote:
While with:

int a[m][n]

a[i][j] is the same as: *(a + n*i + j)

with:

int * a[n]

a[i][j] is the same as: *(*(a + i) + j)


That is a rather nice summary!

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Nov 14 '05 #8
[some editing for space below]
Guillaume wrote:
While with: int a[m][n]
a[i][j] is the same as: *(a + n*i + j)

with: int * a[n]
a[i][j] is the same as: *(*(a + i) + j)

In article <news:c1********@netnews.proxy.lucent.com>
David Rubin <no****@nowhere.net> writes:That is a rather nice summary!


Yes, except for one technical flaw -- the first expression is not
legal C -- and one more serious one: the second expression works
for the array of arrays case as well.

I have a pictorial explanation of what a pointer-to-array "means"
on my web pages; at some point I should add an "array of pointers"
to contrast with "array of arrays" and "pointer to array".
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #9
>>>While with: int a[m][n]
a[i][j] is the same as: *(a + n*i + j)

with: int * a[n]
a[i][j] is the same as: *(*(a + i) + j)
That is a rather nice summary!

Yes, except for one technical flaw -- the first expression is not
legal C


How is it not legal, please?

Nov 14 '05 #10
Guillaume <gr*******@no-spammail.com> scribbled the following:
While with: int a[m][n]
a[i][j] is the same as: *(a + n*i + j)

with: int * a[n]
a[i][j] is the same as: *(*(a + i) + j) That is a rather nice summary!
Yes, except for one technical flaw -- the first expression is not
legal C

How is it not legal, please?


Well, the expressions aren't equivalent. a[i][j] has type "int" but
*(a + n*i + j) has type "int (*)[n]". It's legal C, all right - but
if the value of i is too high, you end up reading unallocated memory,
because *(a + n*i + j) is the same as a[n*i + j].

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"We're women. We've got double standards to live up to."
- Ally McBeal
Nov 14 '05 #11
Joona I Palaste <pa*****@cc.helsinki.fi> scribbled the following:
Guillaume <gr*******@no-spammail.com> scribbled the following:
>While with: int a[m][n]
>a[i][j] is the same as: *(a + n*i + j)
>
>with: int * a[n]
>a[i][j] is the same as: *(*(a + i) + j) That is a rather nice summary!

Yes, except for one technical flaw -- the first expression is not
legal C
How is it not legal, please?
Well, the expressions aren't equivalent. a[i][j] has type "int" but
*(a + n*i + j) has type "int (*)[n]". It's legal C, all right - but
if the value of i is too high, you end up reading unallocated memory,
because *(a + n*i + j) is the same as a[n*i + j].


What am I babbling about? *(a + n*i + j) has type "int[n]" which decays
into type "int *" when used in a value context. a itself has type
"int[m][n]" which decays into "int (*)[n]" when used in a value context.
I must be too drunk. =)

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"This is a personnel commuter."
- Train driver in Scientific American
Nov 14 '05 #12

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

Similar topics

388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
20
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
19
by: gaga | last post by:
I can't seem to get this to work: #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *names; char **np;
3
by: ozbear | last post by:
This is probably an obvious question. I know that pointer comparisons are only defined if the two pointers point somewhere "into" the storage allocated to the same object, or if they are NULL,...
1
by: ketema | last post by:
Hello, I was wondering if someone could help me with a function I am trying to write. The purpose of the function is to read in text from a file in the following format: FIRSTNAME LASTNAME...
5
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;
36
by: raphfrk | last post by:
I have the following code: char buf; printf("%lp\n", buf); printf("%lp\n", &buf); printf("%lp\n", buf); printf("%lp\n", buf); printf("%d\n", buf-buf);
64
by: Zytan | last post by:
I know there are no pointers in C#, but if you do: a = b; and a and b are both arrays, they now both point to the same memory (changing one changes the other). So, it makes them seem like...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.