Click here to Skip to main content
11,924,487 members (57,963 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked

Declare a reference for a two dimensional array without typecasting

, 25 Dec 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
This not so useful tip tells you how to declare a reference for a two dimensional array without needing to use typecasting.
int x[1][20];
int (*p)[20] = x;

Here p has become a reference to x.
Usually in most cases, we use:
int** p = (int**) x;

So, this tip gives you a hint about how to declare another reference for x without typecasting.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mukit, Ataul
Chief Technology Officer Rational Technologies
Bangladesh Bangladesh
Your Lord does not like people who waste by extravagance, and only the righteous C s, and does so in increments, and they are of those people who are mindful of what memory and time has in offer and they waste none of it.

You may also be interested in...

Comments and Discussions

GeneralRe: Unfortunately, in the code I've seen both in the projects I ... Pin
Stefan_Lang3-Jan-12 3:57
memberStefan_Lang3-Jan-12 3:57 
GeneralIn your case as It was mentioned x is an address and what th... Pin
SiarheiY27-Dec-11 4:46
memberSiarheiY27-Dec-11 4:46 
GeneralRe: the first dimensions size can be anything FYKI Pin
Mukit, Ataul27-Dec-11 23:15
memberMukit, Ataul27-Dec-11 23:15 
GeneralReason for my vote of 4 maybe Stefan_Lang is right. Pin
yellowcrd26-Dec-11 19:56
memberyellowcrd26-Dec-11 19:56 
GeneralWhy do you say it's not useful? I'd say it is, especially si... Pin
Stefan_Lang5-Dec-11 23:19
memberStefan_Lang5-Dec-11 23:19 
Why do you say it's not useful? I'd say it is, especially since the second form you presented is wrong! The second form declares p as a pointer (or array of pointers) of pointers to int, and it will require additional memory to store the array of pointers it points to. The first form does not require additional memory to store pointers, because x is not the address to an array of pointers, instead it is the address of the first element! That's quite a difference, and the reason why your second form will likely cause your app to crash!

P.S.: type casting, especially C-style casts, are always bad, and almost almost always semantically wrong and will crash your app. If the compiler complains that types are incompatible you better believe it - it's built to detect this kind of thing, while most programmers aren't.
GeneralRe: Never thought of it that way about the second form taking ex... Pin
Mukit, Ataul6-Dec-11 18:50
memberMukit, Ataul6-Dec-11 18:50 
GeneralRe: Type casting is done implicitly.Isn't it? Pin
yellowcrd26-Dec-11 20:21
memberyellowcrd26-Dec-11 20:21 
GeneralRe: "P.S.: type casting, especially C-style casts, are always ba... Pin
PIEBALDconsult3-Jan-12 3:43
memberPIEBALDconsult3-Jan-12 3:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151125.3 | Last Updated 25 Dec 2011
Article Copyright 2011 by Mukit, Ataul
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid