Connecting Tech Pros Worldwide Help | Site Map

guys what is wrong here pointer question

 
LinkBack Thread Tools Search this Thread
  #1  
Old June 27th, 2008, 04:38 PM
puzzlecracker
Guest
 
Posts: n/a
Default guys what is wrong here pointer question

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

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

  #2  
Old June 27th, 2008, 04:38 PM
Ben Bacarisse
Guest
 
Posts: n/a
Default Re: guys what is wrong here pointer question

puzzlecracker <ironsel2000@gmail.comwrites:
Quote:
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.
  #3  
Old June 27th, 2008, 04:38 PM
Juha Nieminen
Guest
 
Posts: n/a
Default Re: guys what is wrong here pointer question

Ben Bacarisse wrote:
Quote:
puzzlecracker <ironsel2000@gmail.comwrites:
>
Quote:
>int a[3][9];
>f(a);
>>
>void f(int** x)
>{
> //do something with a
>}
>
You have a type miss-match.
[snip]
Quote:
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.
  #4  
Old June 27th, 2008, 04:38 PM
Pascal J. Bourguignon
Guest
 
Posts: n/a
Default Re: guys what is wrong here pointer question

Ben Bacarisse <ben.usenet@bsb.me.ukwrites:
Quote:
puzzlecracker <ironsel2000@gmail.comwrites:
>
Quote:
>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__
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.