473,322 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

count of pointer to array question

Does anyone know how to get a count for a pointer to an array

for example

double** someFnct () {

cout <<"Enter num of row: " <<endl;
cin >> n;

cout <<"Enter num of column: " <<endl;
cin >> m;

double** matrixX = new double[n];
for (int i = 0; i<numOfRows; i++) [
matrixX[i] = new double(m);
}
return matrixX;
}

double** tmpMtrx = someFunct();

int row = sizeof tmpMtrx / sizeof* tmpMtrx; // does not work
int column = // don't even know where to begin with this one
Any insight would be greatly appreciated. Thanks
Jul 22 '05 #1
6 1808
On Sat, 16 Oct 2004 18:08:54 -0700, Jackie wrote:
Does anyone know how to get a count for a pointer to an array
.... int row = sizeof tmpMtrx / sizeof* tmpMtrx; // does not work
int column = // don't even know where to begin with this one
Any insight would be greatly appreciated. Thanks

basically you have to record the no. of cols/rows.
double** someFnct (int &rows, int &cols);

Jul 22 '05 #2
"Jackie" <da*******@yahoo.com> wrote...
Does anyone know how to get a count for a pointer to an array
I don't think you describe it well, but IIUIC, you are trying to
get the size of a dynamically allocated array from the pointer.
That is not possible in C++.

for example

double** someFnct () {

cout <<"Enter num of row: " <<endl;
cin >> n;

cout <<"Enter num of column: " <<endl;
cin >> m;

double** matrixX = new double[n];
I think you want

double** matrixX = new double*[n];
for (int i = 0; i<numOfRows; i++) [
There is no 'numOfRows'. I believe you called it 'n'. So this
should be

for (int i = 0; i < n; ++i) {
matrixX[i] = new double(m);
And here you probably want

matrixX[i] = new double[m];
}
return matrixX;
}

double** tmpMtrx = someFunct();

int row = sizeof tmpMtrx / sizeof* tmpMtrx; // does not work
int column = // don't even know where to begin with this one


If you want to know what sizes the array was allocated with, pass
addresses of two integers into your 'someFunct' and let it tell
you what sizes it used:

double ** someFunc(int* nRows, int* nCols) {
...
if (nRows) *nRows = n;
if (nCols) *nCols = m;
return matrixX;
}

...

int rows, columns;
double** tmpMtrx = someFunct(&rows, &columns);

Otherwise, ask your questions outside and let someFunct allocate
the memory based on the numbers you pass in.

Victor
Jul 22 '05 #3

"Jackie" <da*******@yahoo.com> wrote in message
news:e7**************************@posting.google.c om...
Does anyone know how to get a count for a pointer to an array


Impossible, use a vector instead. Look up vectors in your favourite C++
book.

john
Jul 22 '05 #4
Jackie wrote:
Does anyone know how to get a count for a pointer to an array

for example

double** someFnct () {

cout <<"Enter num of row: " <<endl;
cin >> n;

cout <<"Enter num of column: " <<endl;
cin >> m;

double** matrixX = new double[n];
for (int i = 0; i<numOfRows; i++) [
matrixX[i] = new double(m);
}
return matrixX;
}

double** tmpMtrx = someFunct();

int row = sizeof tmpMtrx / sizeof* tmpMtrx; // does not work
int column = // don't even know where to begin with this one
Any insight would be greatly appreciated. Thanks

You can't, you will use variables n and m for that.
A better way is to do:
#include <iostream>
#include <vector>

int main()
{
using namespace std;

unsigned m ,n;

cout <<"Enter num of row: " <<endl;
cin >> n;

cout <<"Enter num of column: " <<endl;
cin >> m;

vector<vector<double> >matrixX(n, vector<double>(m));
}

In this matrixX.size() returns the number of rows, and matrixX[i].size()
returns the number of columns.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #5
Jackie wrote:
Does anyone know how to get a count for a pointer to an array

for example

double** someFnct () {

cout <<"Enter num of row: " <<endl;
cin >> n;

cout <<"Enter num of column: " <<endl;
cin >> m;

double** matrixX = new double[n];
for (int i = 0; i<numOfRows; i++) [
matrixX[i] = new double(m);
}
return matrixX;
}

double** tmpMtrx = someFunct();

int row = sizeof tmpMtrx / sizeof* tmpMtrx; // does not work
int column = // don't even know where to begin with this one
Any insight would be greatly appreciated. Thanks


A suggestion: use a matrix class.

http://groups.google.com/groups?selm...concentric.net

There are plenty of open source matrix libraries.

G
Jul 22 '05 #6
Thanks everyone,

I appreciate the suggestions and comments, it was enlightening and
helpful. I apologize for the careless code post. I went with vectors
instead. Long time Java/SQL programmer, but first week learning C++.
I'm learning to like C++ more and more.

Thanks again everyone.

Jack
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: overbored | last post by:
I can do this: int asdf; int* zxcv = asdf; but not this: int asdf; int** zxcv = asdf;
9
by: sangeetha | last post by:
Hello, Is there any performance difference in using of the following two declaration? int (*ptr); //Array of 10 int pointers int *ptr; // pointer-to-array of 10. Regards, Sangeetha.
11
by: x-pander | last post by:
given the code: <file: c.c> typedef int quad_t; void w0(int *r, const quad_t *p) { *r = (*p); }
204
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 =...
28
by: Wonder | last post by:
Hello, I'm confused by the pointer definition such as int *(p); It seems if the parenthesis close p, it defines only 3 integers. The star is just useless. It can be showed by my program: ...
68
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
17
by: gk245 | last post by:
For instance, if i had a array like this: array = {1, 2, 3, 5, 6, ......}; How do i tell C go get me the number of values in the array? Not the value itself, but how MANY of them there are. ...
14
by: sheroork | last post by:
Can anyone help me in getting to understand pointer to pointer with examples? Appritiate in advance. Sagar
9
by: subramanian100in | last post by:
The following portion is from c-faq.com - comp.lang.c FAQ list · Question 6.13 int a1 = {0, 1, 2}; int a2 = {{3, 4, 5}, {6, 7, 8}}; int *ip; /* pointer to int */ int (*ap); /* pointer to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.