C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 28th, 2004, 03:38 AM
BLiND_LiMiT BLiND_LiMiT is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 11 BLiND_LiMiT User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to BLiND_LiMiT Send a message via AIM to BLiND_LiMiT
pointer to a multidimensional array

Hi,

How can I declare a pointer to a multi-dimensional array without losing the benefits granted from using a multi-dimensional array?

EG.

Code:
const char c_cMap[4][4] = {
	{1, 0, 1, 0},
	{1, 0, 1, 0},
	{1, 0, 1, 0},
	{1, 0, 1, 0},
};

//char* pcPointerToMap[4][4]...?
// I want to use the pointer to access the array in the same way as I could the constant.

Reply With Quote
  #2  
Old November 28th, 2004, 06:34 AM
grumpy's Avatar
grumpy grumpy is offline
Left due to despotic ad-min
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2003
Posts: 1,044 grumpy User rank is Corporal (100 - 500 Reputation Level)grumpy User rank is Corporal (100 - 500 Reputation Level)grumpy User rank is Corporal (100 - 500 Reputation Level)grumpy User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 53 m 47 sec
Reputation Power: 12
To declare a pointer to a multi-dimensional array, the most practical way is to use a typedef.

PHP Code:
 typedef char MyMap[4][5];
typedef MyMap *PointerToMyMap;

int main()
{
     
MyMap a_map;
     
char another_map[4][5];
     
MyMap *a_pointer;
     
PointerToMyMap another_pointer;

     
a_pointer = &a_map;
     
a_pointer = &another_map;
     
     
another_pointer = &a_map;
     
another_pointer = &another_map;
     
another_pointer a_pointer;  


Keep in mind that typedef simply declares a new name for an existing type, which is why the above example will be valid on every line.

It is technically possible to do the same without typedefs, but the syntax is somewhat more cryptic, difficult to understand, and easy to get confused with. Hence the typedef type approach is usually recommended for this sort of problem.

The typedef approaches also works nicely with more challenging problems such as declaring a pointer to a multi-dimensional array of function pointers. The solution to that problemn without typedef's is far from being a thing of beauty.
__________________
It is only our bad temper that we put down to being tired or worried or hungry; we put our good temper down to ourselves."
-- C.S. Lewis

I like long walks, especially when they're taken by people who annoy me.
--Fred Allen

Last edited by grumpy : November 28th, 2004 at 06:47 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Holding a pointer to a multidimensional array without breaking it's structure.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap