This topic is now archived and is closed to further replies.


Pointers and multi Dimensional Arrays (i am in need of explanation)

Recommended Posts

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <ctime>

#define CLASSNO 3
const int STUDENTNO=40;

void AssignGrades(int MyTwoDimensionalArray[][40])
	for(int cntr1=0,cntr3=0;cntr1<3;++cntr1)
		for(int cntr2=0;cntr2<40;++cntr2,cntr3++)

void Show(int MyTempArray [][40])
	for(int cntr1=0;cntr1<3;++cntr1)
		for(int cntr2=0;cntr2<40;++cntr2)

int main()
//	int*  tempPtr=S1;   //That Line Causes Error

       S1 is an two dmensional array.And the name ''S1'' is the adress of first element of the array
	   if it was an Single Dimensional Array then the line

  int* TempPtr=S1;   wouldn''t give error;
	  Assume that i need to get the starting adress of this two dimensional array somewhy
	  (maybe i will directly use it to reach the elements)

  int** TempPtr=S1 is also giving error in that two dimensional array.
  All i wantto know is actually if the name of the array is the adress of first element
  in array.Then int multi dimensional arrays Then name would be so ...
  how many times do i have to make direction (int* and int**) to get the starting point of ,
  multidimensional arrays
	return 0;
the archer who shoots his arrow beyond his target is no more successful than the one who''s arrow didn''t even reach the target.

Share this post

Link to post
Share on other sites
All you need is an explicit cast to make this work. However, don''t cast it to int**. Multidimensional arrays are continuous in memory like single dimensional arrays, meaning you can treat them like single dimensional arrays (as long as you don''t go past the bounds). This would be the type int*, which when deferred, retrieves the value you want. However, if you use int**, it''s like saying you have an array that stores pointers. Instead of treating the values in your array like actual values, it will interpret them as pointers. Needless to say, you''re assigning sequential values from 0x00000000 to 0x00000077, which obviously aren''t valid pointers. Try to access that memory and you''d get an exception.

Here is a basic example of what I''m talking about:

int main()
int array[3][40] = {0}; // In memory as 120 continuous integers

int* right = (int*)array;
int** wrong = (int**)array;

cout << right[119]; // outputs 0 (in bounds)

cout << right[120]; // outputs random value (out of bounds, uninitialized)

cout << wrong[0][0]; // Treats wrong[0] as a pointer (which is 0x00000000), the second deference ([0]) is an access violation (can''t access memory location 0x00000000)


Try it out

Share this post

Link to post
Share on other sites
Well, first of all, if S1 is a single-dimensional array, then S1 is the adress of the first element, and you can do something like:

int S1[10];
int *ptr = S1;

If S1 is a Two-dimensional array then ''S1'' is the adress of the first element in the two-dimensional array. However is *not* a pointer to a pointer, because it''s not pointing to any variable direction, that''s why you can´t do:

int S1[10][10];
int **ptr = S1; // S1 is not a (int**) type!!!

If you want a pointer-variable to point to the begining of a two dimensional array, then you need to do this:

int S1[2][2] = { {1,2,}, {3,4,}, };
int *ptr = S1[0];
// however this ''transforms your two-dimensional array into a
// single-dimensional array like A[] = {1,2,3,4,};

And even worst, that is evil :-D

Also, you shouldn´t be passing multidimensional arrays as parameters, like in:

void Show(int MyTempArray [][40]);

If you want that info to be available in your function, you
should make your arrays globals, or if you don´t like gloval variables, then make an struct or class, put the array inside and pass a pointer to the struct/class to your function

struct MyData
int array[3][3];
void Show(MyData* pData);

here is an example to confuse you a little more

//---------------------------------------------------------------#include <iostream.h>

//---------------------------------------------------------------void main()

int S1[5]={1,2,3,4,5,};
int *ptr = S1;

int S2[3][3]={ {1,2,3,},{4,5,6,},{7,8,9,}, };
int *pptr= S2[0];

cout << "*S1: "<< *S1 << endl
<< "*ptr: "<< *ptr << endl
<< "*S2[2]: "<< *S2[2] << endl
<< "*(S2+2):"<< *(S2+2) << endl
<< "pptr[5]:"<< pptr[5] << endl;

PD: Wow! my longest post ever!!! :-D

Share this post

Link to post
Share on other sites