Click here to Skip to main content
15,888,320 members
Home / Discussions / C#
   

C#

 
GeneralRe: Understanding Jagged Arrays Pin
N8tiv20-May-13 7:30
N8tiv20-May-13 7:30 
GeneralRe: Understanding Jagged Arrays Pin
Matt T Heffron20-May-13 8:49
professionalMatt T Heffron20-May-13 8:49 
GeneralRe: Understanding Jagged Arrays Pin
Richard MacCutchan20-May-13 22:26
mveRichard MacCutchan20-May-13 22:26 
GeneralRe: Understanding Jagged Arrays Pin
N8tiv23-May-13 15:24
N8tiv23-May-13 15:24 
GeneralRe: Understanding Jagged Arrays Pin
Richard MacCutchan23-May-13 20:42
mveRichard MacCutchan23-May-13 20:42 
GeneralRe: Understanding Jagged Arrays Pin
N8tiv23-May-13 21:15
N8tiv23-May-13 21:15 
GeneralRe: Understanding Jagged Arrays Pin
Richard MacCutchan23-May-13 22:22
mveRichard MacCutchan23-May-13 22:22 
GeneralRe: Understanding Jagged Arrays Pin
Jasmine250120-May-13 10:44
Jasmine250120-May-13 10:44 
This is one thing C++ programmers must learn really well or you start to run into trouble. C# has taken the dangerous stuff away, but it's now so simple that programmers don't understand what's going on, and when the details matter, it's often tricky because those details are hidden from you.

So, I don't know if this will be helpful or not, but looking at arrays at the low level always helped me...

C++
int x[5];

That's a single-dimensional array - in C++ it's just 5 memory locations (0000-0004) containing int VALUES.

C++
int x[5][5];

That's a two-dimensional array, but it's still LINEAR in memory - it's 25 integers in a row (0000-0024) (note: I'm using decimal memory addresses here for simplicity)

C++
int x[5][];

This is where it gets tricky - that initializes 5 pointers, which point to integer arrays which aren't initialized yet. We have to initialize them, so if we did this...

C++
x[0] = new int[5];
x[1] = new int[2];
....etc...


Now, in x[0] we have a pointer, pointing to a (probably distant) memory location of 5 integers, same as the array in the first example (and x[1] points to a 2-element array). Our x array contains pointers to those other arrays. THAT is a jagged array (x is, not the arrays x points to). See the difference now between the two-dimensional and jagged? A two-dimensional array is just a continuous block of memory with a clever way of indexing it, and that block holds the actual values. A jagged array holds no values, only pointers to other arrays.

And, C++ allows us to do this...
C++
int x[5][5];
x[22] = 42;


....which sets the value "42" in the 23rd element of the array, which is the same as element [4][2] (the third "column" of the 5th "row")

Confused yet?
GeneralRe: Understanding Jagged Arrays Pin
N8tiv20-May-13 10:47
N8tiv20-May-13 10:47 
GeneralRe: Understanding Jagged Arrays Pin
Jasmine250120-May-13 10:52
Jasmine250120-May-13 10:52 
GeneralRe: Understanding Jagged Arrays Pin
N8tiv20-May-13 10:57
N8tiv20-May-13 10:57 
GeneralRe: Understanding Jagged Arrays Pin
N8tiv20-May-13 11:06
N8tiv20-May-13 11:06 
GeneralRe: Understanding Jagged Arrays Pin
Jasmine250120-May-13 11:18
Jasmine250120-May-13 11:18 
GeneralRe: Understanding Jagged Arrays Pin
N8tiv20-May-13 12:00
N8tiv20-May-13 12:00 
GeneralRe: Understanding Jagged Arrays Pin
Jasmine250120-May-13 12:47
Jasmine250120-May-13 12:47 
GeneralRe: Understanding Jagged Arrays Pin
N8tiv20-May-13 13:04
N8tiv20-May-13 13:04 
GeneralRe: Understanding Jagged Arrays Pin
Jasmine250120-May-13 13:31
Jasmine250120-May-13 13:31 
GeneralRe: Understanding Jagged Arrays Pin
N8tiv20-May-13 13:41
N8tiv20-May-13 13:41 
GeneralRe: Understanding Jagged Arrays Pin
N8tiv23-May-13 21:17
N8tiv23-May-13 21:17 
Questioncounting in c# Pin
MKS Khalid20-May-13 0:50
MKS Khalid20-May-13 0:50 
AnswerRe: counting in c# Pin
Eddy Vluggen20-May-13 1:17
professionalEddy Vluggen20-May-13 1:17 
AnswerRe: counting in c# Pin
Pete O'Hanlon20-May-13 1:26
mvePete O'Hanlon20-May-13 1:26 
AnswerRe: counting in c# Pin
Keith Barrow20-May-13 2:12
professionalKeith Barrow20-May-13 2:12 
AnswerRe: counting in c# Pin
Richard MacCutchan20-May-13 3:10
mveRichard MacCutchan20-May-13 3:10 
GeneralRe: counting in c# Pin
MKS Khalid20-May-13 8:32
MKS Khalid20-May-13 8:32 

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.