473,474 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

2D array, compiler error

Hello all, I am trying to pass 2D array to a func, as follows, it is showing
compiler error.

void helloptr(int **a){

a[0][0] = 0xB;

}

int main(int argc, char* argv[]){

int a[10][10]={0};

helloptr(a);

return 0;

}


Aug 30 '07 #1
2 1649
"Imran" <im******@in.bosch.comwrites:
Hello all, I am trying to pass 2D array to a func, as follows, it is showing
compiler error.

void helloptr(int **a){

a[0][0] = 0xB;

}

int main(int argc, char* argv[]){

int a[10][10]={0};

helloptr(a);

return 0;

}
This is in the FAQ.

6.18: My compiler complained when I passed a two-dimensional array to
a function expecting a pointer to a pointer.

A: The rule (see question 6.3) by which arrays decay into pointers
is not applied recursively. An array of arrays (i.e. a two-
dimensional array in C) decays into a pointer to an array, not a
pointer to a pointer. Pointers to arrays can be confusing, and
must be treated carefully; see also question 6.13.

If you are passing a two-dimensional array to a function:

int array[NROWS][NCOLUMNS];
f(array);

the function's declaration must match:

void f(int a[][NCOLUMNS])
{ ... }

or

void f(int (*ap)[NCOLUMNS]) /* ap is a pointer to an array */
{ ... }

In the first declaration, the compiler performs the usual
implicit parameter rewriting of "array of array" to "pointer to
array" (see questions 6.3 and 6.4); in the second form the
pointer declaration is explicit. Since the called function does
not allocate space for the array, it does not need to know the
overall size, so the number of rows, NROWS, can be omitted. The
width of the array is still important, so the column dimension
NCOLUMNS (and, for three- or more dimensional arrays, the
intervening ones) must be retained.

If a function is already declared as accepting a pointer to a
pointer, it is almost certainly meaningless to pass a two-
dimensional array directly to it.

See also questions 6.12 and 6.15.

References: K&R1 Sec. 5.10 p. 110; K&R2 Sec. 5.9 p. 113; H&S
Sec. 5.4.3 p. 126.
--
"I don't have C&V for that handy, but I've got Dan Pop."
--E. Gibbons
Aug 30 '07 #2
Imran wrote:
>
Hello all, I am trying to pass 2D array to a func, as follows, it
is showing compiler error.

void helloptr(int **a) {

a[0][0] = 0xB;
}
What 2d array? All I see is a pointer to a pointer to int.
Sometimes this may be used to reference a 1d array of pointers to
ints. Read the faq.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 30 '07 #3

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

Similar topics

13
by: HappyHippy | last post by:
Hi, I'm wondering what you think about this piece of code: #include<iostream> int main() { int size; std::cin >> size;
20
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
15
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have ...
33
by: Zytan | last post by:
I want to make a zero element array. I know that Nothing is not the same as a zero element array, since I can't get the length of, or iterate through, an array = Nothing. I could make a zero...
7
by: arnuld | last post by:
this programme gives unusual output. i am not able to find out where the semantic bug lies: /* C++ Primer - 4/e * * an example from section section 7.2.4, page 241 * STATEMENT * write a...
25
by: biplab | last post by:
Hi all, I am using TC 3.0..there if I declare a integer array with dimension 162*219...an error msg saying that too long array is shown....what should I do to recover from this problem???
3
by: David K in San Jose | last post by:
I'm using managed (CLR) C++ in VS2005 to create a Windows app that contains a form named "MyForm". In the code for that form I'm trying to invoke some static functions by using an array of function...
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.