473,322 Members | 1,755 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.

guys what is wrong here pointer question

int a[3][9];
f(a);

void f(int** x)
{
//do something with a
}
Jun 27 '08 #1
3 1000
puzzlecracker <ir*********@gmail.comwrites:
int a[3][9];
f(a);

void f(int** x)
{
//do something with a
}
You have a type miss-match. 'a' is an array (of length 3) whose
elements are arrays of 9 ints. 'f' must be passed a pointer to a
pointer to int. There is no implicit conversion that allows 'a' to be
passed to 'f'.

The parameter could be declared like this:

void f(int x[][9]);

or alternatively,

void f(int (*x)[9]);

The reasons are messy. In most contexts, expressions of type "array
of T" are converted to "pointer to T" (so void f(int *x) is correct
when passing an array of ints) but this conversion applies only to the
outer array type. The 'a' in 'f(a)' is therefore converted to a value
of type "pointer to array of 9 int" and not "pointer to pointer to
int".

--
Ben.
Jun 27 '08 #2
Ben Bacarisse wrote:
puzzlecracker <ir*********@gmail.comwrites:
>int a[3][9];
f(a);

void f(int** x)
{
//do something with a
}

You have a type miss-match.
[snip]
The reasons are messy.
I wouldn't say it's so "messy". I think it's rather simple: The
function f() above expects an array of pointers, or more precisely, a
pointer to the first element of an array which contains pointers.

'a' is not such an array. It doesn't contain pointers. 'a' is
basically just an array which can be double-indexed. Thus there's simply
no way to cast it so that it would "contain pointers" as elements. It
just contains ints as elements.
Jun 27 '08 #3
Ben Bacarisse <be********@bsb.me.ukwrites:
puzzlecracker <ir*********@gmail.comwrites:
>int a[3][9];
f(a);

void f(int** x)
{
//do something with a
}

You have a type miss-match. 'a' is an array (of length 3) whose
elements are arrays of 9 ints. 'f' must be passed a pointer to a
pointer to int. There is no implicit conversion that allows 'a' to be
passed to 'f'.

The parameter could be declared like this:

void f(int x[][9]);

or alternatively,

void f(int (*x)[9]);

The reasons are messy. In most contexts, expressions of type "array
of T" are converted to "pointer to T" (so void f(int *x) is correct
when passing an array of ints) but this conversion applies only to the
outer array type. The 'a' in 'f(a)' is therefore converted to a value
of type "pointer to array of 9 int" and not "pointer to pointer to
int".
Alternatively, you could write:

void f(int** x)
{
// do something with x
for(int i=0;i<3;i++){
for(int j=0;j<9;j++){
x[i][j]=i*j;
}
}
}

int main(void)
{
int* a[3];
for(int i=0;i<3;i++){
a[i]=new int[9];
}
f(a);
for(int i=0;i<3;i++){
delete[](a[i]);
}
return(0);
}

That is, int** is a pointer to a pointer to an integer, which, with
the weak typing of C/C++ is the same as a pointer to a C array of
pointers to C arrays of ints. (A pointer to a C array being a pointer
to the first element of that C array).

--
__Pascal Bourguignon__
Jun 27 '08 #4

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

Similar topics

21
by: Jenski182 | last post by:
Is this from a good coder? or a rubbish one? Whichever way, it was paid for his services, just want to know if its worth while.... Thanks Jen x #include <iostream.h> #include <conio.h>
7
by: Snake | last post by:
Hi guys, I have question about classes. when u create class called Test. and you define variable Test c; so does this act like( a variable c of type Test pointing to an abject )? The thing that...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
8
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
24
by: temper3243 | last post by:
Hi, Many people have used this code in my project. It works because b is using the extra memory for other 4 variables.I access obj max. well here are a few questions 1) Where does it fail. I...
318
by: jacob navia | last post by:
Rcently I posted code in this group, to help a user that asked to know how he could find out the size of a block allocated with malloc. As always when I post something, the same group of people...
16
by: xz | last post by:
For example, I have a Class Date with internal variable year, month and day. In Java I would write: class Date{ int year; int month; int day; Date(int year, int month, int day) { this.year...
2
by: Giorgos Keramidas | last post by:
On Sun, 05 Oct 2008 18:22:13 +0300, Giorgos Keramidas <keramida@ceid.upatras.grwrote: My apologies. I should have been less hasty to hit `post'. If showtext() is passed a null pointer, it may...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
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
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
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 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.