What does this declarations means?

  • Thread starter Thread starter shermaine80
  • Start date Start date
  • Tags Tags
    Means
Click For Summary

Discussion Overview

The discussion revolves around the interpretation of complex pointer declarations in C/C++, specifically focusing on two declarations: (a) int*p[3][3] and (b) int*(*p())[10]. Participants explore the meaning of these declarations, their implications, and the potential for confusion in their usage.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants suggest that (a) represents a 3-dimensional array where each element is a pointer to an integer, while others clarify that it is a 3x3 array of integer pointers.
  • There is a discussion about the declaration int a[3], with some stating it is simply an array, while others argue it points to information at a specific index.
  • Participants express differing views on (b), with some suggesting it indicates obfuscated code and others attempting to clarify its meaning as an array of function pointers.
  • One participant points out that int*(*p())[10] is problematic in terms of syntax, suggesting that it should be int*(*p[10])(); instead.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the interpretations of the declarations, with multiple competing views and some confusion regarding the syntax and semantics of the code.

Contextual Notes

There are unresolved issues regarding the correct interpretation of pointer declarations and the implications of syntax in C/C++. Some assumptions about the nature of arrays and pointers are not fully clarified.

shermaine80
Messages
30
Reaction score
0
(a) int*p[3][3]
(b) int*(*p())[10];

(a) points to address of data in the row 3, column 3?
(b) can you advise me? thanks?
 
Technology news on Phys.org
Before I answer (a), do you know what

int a[3]

declares? What about

int a[3][3]



(b) means that some silly hacker likes to write obfuscated code, rather than use typedef's. :wink:
 
int a[3] is an array. It points to info at array[3]
 
shermaine80 said:
(a) int*p[3][3]
int a[3] is an array. It points to info at array[3]
It doesn't just point to information at array[3] (what do you mean by that?), it actually contains 3 integers (in the programming sense).

Code:
T name[N]
Are arrays with N elements of type T.
Code:
T name[N1][N2];
Are arrays of N1 elements of arrays of N2 elements of type T.
In your first case we have a 3-dimensional array, where each element is another 3-dimensional array in which each element is a pointer to an integer.

It's often called a 3x3 multi-dimensional array, one example use would be:
Code:
int x,y,z;
int* a[3][3];
a[0][0] = &x; // Set the very first element of both arrays to point to x
a[0][2] = &y; // Let the last element of the first array point to y.
a[2][2] = &z; // Let the last element in both arrays point to z
As for the second, either someone misunderstood the syntax of function pointers or they are trying to confuse you.
Code:
int*(*p())[10];
Just like last time, this is an array, so p is an array of 10 int*(*())
The outer set of parentheses means nothing in this code, so it's equivalent to:
Code:
int** ();
Which is just a function returning a pointer to a pointer to an integer. So p is an array of functions, but we don't have first-class functions in C or C++, so we end up with an unusable array.

What was most likely meant if this is real code is:
Code:
int*(*p)()[10];
Which is an array of 10 functions pointers, each pointing to a function returning a pointer to an integer and taking no arguments.
 
Last edited:
Just like last time, this is an array, so p is an array of 10 int*(*())
Almost: p is function that returns an int*[10].

Also,

int*(*p)()[10];

doesn't quite work: gcc complains with

`p' declared as function returning an array

You're looking for

int*(*p[10])();
 
Last edited:

Similar threads

Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
7
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 29 ·
Replies
29
Views
4K
Replies
6
Views
2K
  • · Replies 13 ·
Replies
13
Views
1K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 35 ·
2
Replies
35
Views
4K