LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-01-2003, 10:38 PM   #1
dhanakom
LQ Newbie
 
Registered: Jul 2003
Location: Birmingham
Posts: 20

Rep: Reputation: 0
pointer to a 2D array


Hi,

I have a pointer to a 2D array and I declared it as follows

T (*element_) [ ];


and I allocate my array as follows, (in the constructor ofcourse)

T (*element_)[n];
element_ = new T [m][n];


I'am trying to get a member function return the base address of the individual rows. But I'm in a kind of fix when attempting to declare a function which will do that.

Can someone suggest me how I declare a function, which will return the address of each rows ?

Thanks
 
Old 09-02-2003, 02:54 AM   #2
devoyage
Member
 
Registered: Aug 2003
Distribution: many
Posts: 37

Rep: Reputation: 15
I'm not familiar w/ your syntax, but don't forget that a pointer IS the address... just print it
ex:
Code:
#include<iostream>
using namespace std;
int main(){
    int **x,row=5,col=5;

    x = new (int*)[row];
    for(int i=0;i<row;i++)
        x[i]= new int[col];

    for(int i=0;i<row;i++)
        cout << x + i << endl;

return(0);
}
this gives me:
Code:
0x804a040
0x804a044
0x804a048
0x804a04c
0x804a050
which are the addresses of the rows.

or I suppose you could do:
Code:
cout << &x[i] << endl;
but (I don't think) there's any point of that ( no pun intended

EDIT:
I guess you don't want to print them (I'm _really_ tired), but this should still illustrate how to get what you want (do something other than cout them )
sorry for getting off track.

Last edited by devoyage; 09-02-2003 at 03:16 AM.
 
Old 09-02-2003, 12:16 PM   #3
abd_bela
Member
 
Registered: Dec 2002
Location: algeria
Distribution: redhat 7.3, debian lenny
Posts: 627

Rep: Reputation: 31
hi,
in your declaration T (*element_)[n]; there are several errors:
1. the size , the value of n, must be known at the compiling time, because you decalre a static arrays, if you want a dynamic arrays use news and a pointer to pointer like char **tata;
2. the parenthesis after T , decalre T as a function , use instead bracket [.


This is an example:
#include<iostream.h>


void func1( int **ptr, int row1, int col1){
cout << "give "<< row1 <<" times " << col1 << "numbers"<<endl;
for (int i=0; i < row1; i++)
for (int j=0; j < col1; j++)
cin >> ptr[i][j];
}
int main(){
int row,col,**x; // for dynamic allocation
cout << "enter nb of rows "; cin >> row;
cout << "enter nb of cols "; cin >> col;

x = new (int*)[row];
for(int i=0;i<row;i++)
x[i]= new int[col];
// call a function

func1(x,row,col);
// here you get the array x filled in the funct1
// the address of the rows are x[0], x[1]...
for(int i=0;i<row;i++)
cout << "x[ "<< i << "] = " << x[i] << endl;

return(0);
}
/*this is a test ;

enter nb of rows 3
enter nb of cols 2
give 3 times 2numbers
11 22
33 44
55 66
x[ 0] = 0x804a118
x[ 1] = 0x804a128
x[ 2] = 0x804a138
*/
 
Old 09-02-2003, 12:53 PM   #4
devoyage
Member
 
Registered: Aug 2003
Distribution: many
Posts: 37

Rep: Reputation: 15
Quote:
Originally posted by abd_bela

cout << "x[ "<< i << "] = " << x[i] << endl;
...

x[ 0] = 0x804a118
x[ 1] = 0x804a128
x[ 2] = 0x804a138
I think that you are printing out the address of the the first element in each colum here, not the addresses of each row.

it's like doing this:
Code:
cout << *(x + i) << endl;
where you are printing the value of memory that (x+i) holds.

Of course, this may be what dhanakom wants... it just depends which one you call row/colum
 
Old 09-02-2003, 01:46 PM   #5
dhanakom
LQ Newbie
 
Registered: Jul 2003
Location: Birmingham
Posts: 20

Original Poster
Rep: Reputation: 0
Hi,

All the replies suggest a different way of declaring a pointer to the 2D array.

But the following works jus fine

T (*ptr)[n];
//meaning ptr will hold the address of an array(base address) of n elements of type T

and when I again say
ptr = new T [m][n];

I mean ptr will hold the base address of an array of 'm' elements each of which is an array of 'n' elements. Note that the second 'n' becomes a part of the type.I have a running program with this declaration. This is exactly what devoyage code does, but for few changes in the syntax.

But suppose u want to declare a function which will return the address of each row, how will u declare the function prototype

should it be T (*) [] fun(parm1,param2,param3)

this apparently means this function will return the address of an array(base address) whose elements are of type T. But the above declaration gives a compilation error.

what might be wrong here ??


Thanks,
 
Old 09-02-2003, 02:56 PM   #6
devoyage
Member
 
Registered: Aug 2003
Distribution: many
Posts: 37

Rep: Reputation: 15
Quote:
Originally posted by dhanakom
Hi,
T (*ptr)[n];

This is exactly what devoyage code does, but for few changes in the syntax.
Except for the fact that n must evaluate to a constant at compile time, i.e. not dynamic. ( I still don't understand that other part

Quote:

But suppose u want to declare a function which will return the address of each row, how will u declare the function prototype
Just like I said in your other post:
Code:
T **fun();
This is quite similar to what you have, just conforming to compiler needs a l ittle more I guess.

You can't return all of the addresses, because you can't 'return' arrays, only the address of the first element. (unless you want to throw them into some kind of structure I guess)

Sorry if I'm not understanding your questions. I hope this gives you some ideas at least.

-Devoyage
 
Old 09-06-2003, 12:14 PM   #7
abd_bela
Member
 
Registered: Dec 2002
Location: algeria
Distribution: redhat 7.3, debian lenny
Posts: 627

Rep: Reputation: 31
hi,
to Devoyage,
the addresses are printed bye the program, you can test it !!!
In fact it is not the same as you code !!
in your case x = new (int*)[row];

x is an array for row (5 ) elements each one contains an address, the address of the first is the same as x , the address of the second is the next 4bytes, and so on,
that is :
0x804a040 location of x same as location of x[0]
0x804a044 location of x[1]
0x804a048 location of x[2] not the contains of it
0x804a04c
0x804a050

now try to print x[i] and you get the same thing as mine

best regards
 
Old 09-07-2003, 04:22 AM   #8
devoyage
Member
 
Registered: Aug 2003
Distribution: many
Posts: 37

Rep: Reputation: 15
Quote:
Originally posted by abd_bela
hi,
0x804a040 location of x same as location of x[0]
0x804a044 location of x[1]
0x804a048 location of x[2] not the contains of it
I couldn't agree more, that is what I was trying to say. Maybe we were misunderstanding eachother or something.

Quote:
Originally posted by me
I think that you are printing out the address of the the first element in each colum here, not the addresses of each row.

it's like doing this:
Code:
cout << *(x + i) << endl;
Now, here's the output of a new and inproved tester ...
Code:
printing 'x + i'...
0x804a040
0x804a044
0x804a048
0x804a04c
0x804a050 
printing '*(x + i)'...
0x804a060
0x804a080
0x804a0a0
0x804a0c0
0x804a0e0
printing 'x[i]'...
0x804a060
0x804a080
0x804a0a0
0x804a0c0
0x804a0e0
Like I said before, it depends on which one you call row,colum.

I was using the convention var[ROW][COLUM], and by the looks of your declerations, I thought you were too.

to print the address of the first element in each row, I used 'x+i', which is what he asked. You were printing the address of the first element of each colum, namely 'x[i]', the contents of 'x + i'.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer notation vs array notation? pablowablo Programming 5 03-14-2005 12:34 PM
number of element in a array of pointer? os2 Programming 7 03-09-2005 11:50 AM
about array and pointer seeLnd Programming 5 05-30-2004 07:56 AM
sending pointer array to function marek Programming 4 04-15-2004 04:46 PM
Assigning a string to a variable (not a pointer, not a array) JStew Programming 3 11-18-2002 08:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration