Need some direction as how to implement a pointer array which will mimic a 2D array. Any suggestions? This is where I am. Do not work, but i am working on it. Please send suggestions.
I think my problem is dereferencing this pointer array. 5 3370
On thing I have found out so far.
int array[ ][ ]
is equivalent to
int (*ptr)[]
But how do I perform the pointer arithmetic and allocate memory at the same time?
for(/*for conditions*/)
{
(*prt ) = malloc(sizeof( int[] ))
for(/*for conditions*/)
(*prt + /*increment value*/)[/*increment value*/] = /*some value*/
I do not have a clue....
I do not have a clue....
Here is your clue:
First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
That is an array of 5 elements each of which is an int.
won't compile. You need to declare the number of elements.
Second, this array:
is still an array of 5 elements. Each element is an array of 10 int.
is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.
won't compile. You need to declare the number of elements.
Third, the name of an array is the address of element 0
Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.
Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int: -
int array[5][10];
-
-
int (*ptr)[10] = array;
-
Fourth, when the number of elements is not known at compile time, you create the array dynamically: -
int* array = new int[value];
-
int (*ptr)[10] = new int[value][10];
-
int (*ptr)[10][15] = new int[value][10][15];
-
In each case value is the number of elements. Any other brackets only describe the elements.
Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code: -
int** ptr = new int[value][10]; //ERROR
-
new returns the address of an array of 10 int and that isn't the same as an int**.
Likewise: -
int*** ptr = new int[value][10][15]; //ERROR
-
new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.
With the above in mind this array: -
int array[10] = {0,1,2,3,4,5,6,7,8,9};
-
has a memory layout of
0 1 2 3 4 5 6 7 8 9
Wheras this array: -
int array[5][2] = {0,1,2,3,4,5,6,7,8,9};
-
has a memory layout of
0 1 2 3 4 5 6 7 8 9
Kinda the same, right?
So if your disc file contains
0 1 2 3 4 5 6 7 8 9
Does it make a difference wheher you read into a one-dimensional array or a two-dimensional array? No.
Therefore, when you do your read use the address of array[0][0] and read as though you have a
one-dimensional array and the values will be in the correct locations.
Post again if you have further questions.
Here is your clue:
Post again if you have further questions.
-
-
main(){
-
int ar[ARSZ], //1D Declared using the square brace notation
-
(*arr)[ARSZ], //1D Declared as a point to 10 elements
-
ar2D[COLSZ][ARSZ]; //2D Declared using the square brace notation
-
-
int i, c;
-
//int (*ptr)[COLSZ][ARSZ] = new int[value][CLOSZ][ARSZ];
-
-
for(i = 0; i < ARSZ; i++)
-
{
-
*((ar) + (i)) = i;
-
(*((ar) + (i)))++;
-
gotoxy(5,1+i);printf("ar[%d] now = %d\n", i, *((ar) + (i)));
-
}
-
getch();
-
clrscr();
-
for(i = 0; i < ARSZ; i++)
-
{
-
(*arr)[i] = i;
-
i[(*arr)]++;
-
gotoxy(30,1+i);printf("ar[%d] now = %d\n", i, (*arr)[i]);
-
}
-
getch();
-
clrscr();
-
for(c = 0; c < COLSZ; c++)
-
for(i = 0; i < ARSZ; i++)
-
{
-
*((ar) + (c + i)) = i;
-
(*((ar) + (c + i)))++;
-
gotoxy(30*c,1+i);printf("ar[%d][%d] now = %d\n", c, i, *((ar) + (c + i)));
-
}
-
getch();
-
exit(EXIT_SUCCESS);
-
}
Brackets ([ ]) indicate single and multidimensional array subscripts.
The expression <exp1>[exp2] is defined as *((exp1) + (exp2)) likewise
<exp1>[exp2][exp3] is defined as *((exp1) + (exp2 + exp3))
where either exp1 is a pointer and exp2 is an integer or exp1 is an integer and exp2 is a pointer. This is as far as my limited knowledge span on arrays. So I am still sort of in the dark here. Please shed some light on the 2D implementation.
Declared as (*arr2D)[COLSZ][ARSZ]?
-
typedef int T;
-
-
-
T **Alloc2D ( int MAX_ROWS, int MAX_COLS ) {
-
int ROWS, COLS;
-
-
T **array = malloc( sizeof(T*) * MAX_ROWS );
-
T *temparray = malloc( sizeof(T) * MAX_ROWS * MAX_COLS );
-
-
for ( ROWS = 0 ; ROWS < MAX_ROWS ; ROWS++, temparray += MAX_COLS )
-
{ array[ROWS] = temparray;
-
for ( COLS = 0 ; COLS < MAX_COLS ; COLS++ ) {
-
array[ROWS][COLS] = ROWS;
-
gotoxy(30*ROWS,1 + COLS);printf("array[%d][%d] now = %d\n",ROWS,COLS,array[ROWS][COLS]);
-
}
-
}
-
return array;
-
}
-
void Free2D( T **array )
-
{
-
free( array[0] );
-
free( array );
-
}
Thank dude your post was very helpful. Search and he shall find. Well this is my last shot at it. Please send me any suggestions. Thanks again.
Please shed some light on the 2D implementation.
Declared as (*arr2D)[COLSZ][ARSZ]?
Here arr2D is a pointer to an array [COLSZ][ARSZ].
It's a pointer. When tyou add 1 to it, the address changes by the sizeof 1 [COLSZ][ARSZ] array. That's all it is. There is no implementation behind it.
To repeat, there are no 2D arrays in C++. All there are are 1D arrays that you can pretend are 2D arrays by type casting &array[0] as (*arr2D)[COLSZ][ARSZ].
When you use array, it's 1D but when you use arr2D it's 2D. Same array.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: jr |
last post by:
Sorry for this very dumb question, but I've clearly got a long way to go!
Can someone please help me pass an array into a function. Here's a starting
point.
void TheMainFunc()
{
// Body of...
|
by: Kieran Simkin |
last post by:
Hi,
I wonder if anyone can help me, I've been headscratching for a few hours
over this.
Basically, I've defined a struct called cache_object:
struct cache_object {
char hostname;
char ipaddr;...
|
by: Simon Schaap |
last post by:
Hello,
I have encountered a strange problem and I hope you can help me to
understand it. What I want to do is to pass an array of chars to a
function that will split it up (on every location where...
|
by: fix |
last post by:
Hi all,
I feel unclear about what my code is doing, although it works but I am
not sure if there is any possible bug, please help me to verify it.
This is a trie node (just similar to tree nodes)...
|
by: David Mathog |
last post by:
If this:
int i,sum;
int *array;
for(sum=0, i=0; i<len; i++){
sum += array;
}
is converted to this (never mind why for the moment):
|
by: truckaxle |
last post by:
I am trying to pass a slice from a larger 2-dimensional array to a
function that will work on a smaller region of the array space. The
code below is a distillation of what I am trying to...
|
by: Alexei A. Frounze |
last post by:
Hi all,
I have a question regarding the gcc behavior (gcc version 3.3.4).
On the following test program it emits a warning:
#include <stdio.h>
int aInt2 = {0,1,2,4,9,16};
int aInt3 =...
|
by: intrepid_dw |
last post by:
Hello, all.
I've created a C# dll that contains, among other things, two functions
dealing with byte arrays. The first is a function that returns a byte
array, and the other is intended to...
|
by: sandy |
last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory',
within my class Directory (Can you already smell disaster?)
Each Directory can have subdirectories so I thought to put these...
|
by: isaac2004 |
last post by:
hello, i posted with a topic like this but got no real feedback(prob
cuz of lapse in my explanation) so i am trying it again. i am trying
to set up a function that brings in a txt file and adds the...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |