A question about pointer

I

I_have_nothing

Hi! I am new in C.
I am reading a C programming book writen in Chinese.
In Page 134 the author says(in Chinese):
"The compiler will not accept the following declarations:
int **ary;
int (*ary);
int ary[ ][ ];
"

But in a sample code a friend gave me there is a line having:
" extern double matrix[][]; "
and the Compiler does not complain about it.

My question is at what situation(s) it is OK to have "matrix[][]"?
In "extern"? In formal parameter? or In what kinds of situtatuions?

Thanks!
 
B

Barry Schwarz

Hi! I am new in C.
I am reading a C programming book writen in Chinese.
In Page 134 the author says(in Chinese):
"The compiler will not accept the following declarations:
int **ary;

There is nothing wrong with a pointer to pointer to int.
int (*ary);

The parentheses are superfluous but that is still a pointer to int.
int ary[ ][ ];

This is a syntax error. Under some circumstances it is permitted to
leave the first dimension unspecified (as when defining the parameter
of a function) but it is never legal to omit them all.
"

But in a sample code a friend gave me there is a line having:
" extern double matrix[][]; "

Change the warning level to its maximum sensitivity and specify
compliance with the standard and no extensions.
and the Compiler does not complain about it.

My question is at what situation(s) it is OK to have "matrix[][]"?
In "extern"? In formal parameter? or In what kinds of situtatuions?

Thanks!



<<Remove the del for email>>
 
T

Taran

I_have_nothing said:
Hi! I am new in C.
I am reading a C programming book writen in Chinese.
In Page 134 the author says(in Chinese):
"The compiler will not accept the following declarations:
int **ary;

This is Ok. A pointer to pointer to int. Acceptable to compiler.
int (*ary);

The parenthesis doesn't add any anything to the declaration. Maybe it
has been added to make it more readable to someone!
int ary[ ][ ];

This has a problem. you instruct the compiler to reserve some memory
for ary. But you never told it how much? When the array is not being
initialized where it is declared you have to give the size of the
array.
For 2-D array it must be atleast the last index, for 1-D there's no
other option!
But when you initialize the array the compiler can 'count' and find how
many memory space to allocate.

E.g.
int arr[3] will allocate memory to store 3 ints.
int arr[]={0,1,2}, the compiler counts 3 ints and allocates that much
space.
int arr[] doesn't say anything nor can the compiler derive the memeory
to be allocated.
int arr[2][4] the compiler allocates memory for 6 ints.
int arr[][4] ={..intialization...} here since the compiler knows that
there are 'sets'/'rows' for 4 elments each it can derive the first
index.
But in a sample code a friend gave me there is a line having:
" extern double matrix[][]; "
Yes the compiler would not complain, as 'matrix' would be declared and
defined somewhere else with proper indices. Where it would have had
followed one of the above stated declarations.
and the Compiler does not complain about it.

My question is at what situation(s) it is OK to have "matrix[][]"?
In "extern"? In formal parameter? or In what kinds of situtatuions?

Thanks!

HTH
 
C

Christian Kandeler

Taran said:
But in a sample code a friend gave me there is a line having:
" extern double matrix[][]; "
Yes the compiler would not complain, as 'matrix' would be declared and
defined somewhere else with proper indices.

No, I don't think so. matrix[] is an incomplete type, and so matrix[][]
would be an array of an incomplete type, which is explicitly disallowed by
the Standard.


Christian
 
V

Vijay Kumar R. Zanvar

Taran said:
I_have_nothing said:
Hi! I am new in C.
I am reading a C programming book writen in Chinese.
In Page 134 the author says(in Chinese):
"The compiler will not accept the following declarations:
int **ary;

This is Ok. A pointer to pointer to int. Acceptable to compiler.
int (*ary);

[..]

int arr[2][4] the compiler allocates memory for 6 ints.

6? ITYM, 8. Their interpretation, however, is somewhat different.

[..]

Kind regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,797
Messages
2,569,646
Members
45,373
Latest member
Vast3

Latest Threads

Top