ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 ?
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.
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
*/
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.
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.
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
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 ...
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'.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.