TechSpott - Computer and technology forums

Go Back   TechSpott - Computer and technology forums > Webmasters > Programming
Programming Anything related to programming... PHP, PERL, ASP, ColdFusion, C++, etc.

Reply
 
Thread Tools Display Modes
Old February 20th, 2010, 10:49 PM   #1 (permalink)
Jessica
TechSpott Newbie
 
Join Date: Feb 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Jessica is an unknown quantity at this point
Question C++ multidimensional arrays?

Ok, two short questions. This is not homework, if you are wondering.

The first question:

consider this line:

int multi[30][80];

Which of the following is 'multi'

- An array of 30 arrays, each containing 80 ints
- An array of 80 arrays, each containing 30 ints


Second part of the question:

If I did these three lines:

int a = multi[5][15];
int** ptr = &multi[0][0];
int b = ptr[5][15];

is (a == b) for all cases?
Jessica is offline   Reply With Quote Share with Facebook
Old February 23rd, 2010, 12:47 AM   #2 (permalink)
Vivek
Junior Member
 
Join Date: Feb 2010
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Vivek is an unknown quantity at this point
Default Re: C++ multidimensional arrays?

Quote:
Originally Posted by Jessica View Post
Ok, two short questions. This is not homework, if you are wondering.

The first question:

consider this line:

int multi[30][80];

Which of the following is 'multi'

- An array of 30 arrays, each containing 80 ints
- An array of 80 arrays, each containing 30 ints


Second part of the question:

If I did these three lines:

int a = multi[5][15];
int** ptr = &multi[0][0];
int b = ptr[5][15];

is (a == b) for all cases?
Because, in C++, multiple array indices increment right-to-left (in other words, multi[0][0] is next to multi[0][1] in memory and multi[1][0] is eighty positions past multi[0][0]), the second index indicates the column and the right, the row. This makes it 30 rows of 80 columns. (The 80, being the nominal size of an input line (holdover from the ancient punch cards), is also a subtle tip-off to an old geezer like me.)

Pointers-to-pointer (**) are usually used in two situations: 1. A pointer argument to a function that's expected to return a new value OF THE POINTER or 2. A pointer to an array of pointers. This is neither. In fact, int* ptr = &multi[0][0]; is the correct syntax. (Which is identical to int* ptr = multi; by the way.) So if the compiler permits subscript notation with a pointer (I don't remember if that's syntactically legal), yes, a would always have the same value as b.

Hope that helps.
Vivek is offline   Reply With Quote Share with Facebook
Reply

Bookmarks

Tags
arrays, multidimensional


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
[IMG] code is On
HTML code is Off


 
Forum Stats
Members: 14,320
Threads: 20,326
Posts: 27,667
Total Online: 37

Newest Member: VanoNat

Latest Threads

Advertisements

Support our advertisers!

Amazon
Sears
Macys
1800flowers.com
Drugstore.com


Carbonite Online-Backups
Tiger Direct
Tech Depot
Buy.com
Office Depot
Fatcow
HostGator


TripsNow.us
Marriott
Expedia
CheapTickets.com
OneTravel
Hotwire

Are you a Fan?

Share this on Facebook

Tag Cloud

Partner Links


Contact Us - Home - Archive - Privacy Statement - Top - Copyright © 2009-2010, TechSpott - All times are GMT -4. Time is 05:01 AM

Powered by vBulletin® Copyright © Jelsoft Enterprises Ltd. :: SEO by vBSEO