PDA

View Full Version : [SOLVED] c/c++ "advanced" pointers question



rbprogrammer
February 16th, 2008, 05:35 AM
ok, i have seen a few "advanced" ways of using pointers and i am trying to figure out what is really declared in these unique declarations. since programming is kind of my thing, i am somewhat baffled at some of these. this is what i have seen:

double *a[n];
double (*b)[n];
double (*c[n])();
double (*d())[n];

my assumptions of these are reasonable at best, for some. now i used two references to determine the exact order of operations of c++. one if from a deitel&deitel book, and the other is http://www.cppreference.com/operator_precedence.html . both agree, at least in the aspect of the two operators in question ( dereference and array access operators ). and this is what i think is going on.

#1) this is a pointer that holds the address of the first element of an array of n elements of type double.
#2). b/c of the order of precedence, this is the same as #1. ie a pointer that holds the base address of an array of n elements.
-----> now here is where my knowledge gets tested
#3). ummm..... a pointer to an array of n elements ... yada, yada, yada ... address of ... yada, yada, yada ... function :confused:
#4). this one is a pointer (called "d") that points to the address of a function that returns an array n elements long of type double.

am i at least close to right?? for 1,2,4 i think i have reasonable guesses, but #3 i just have no idea...... :confused:

stroyan
February 16th, 2008, 06:42 AM
The c++decl command in the cdecl package can be very helpful sorting these out.
You do need to drop the 'n' or change it to an integer constant.

$ c++decl
Type `help' or `?' for help
c++decl> explain double *a[5];
declare a as array 5 of pointer to double
c++decl> explain double (*b)[5]
declare b as pointer to array 5 of double
c++decl> explain double (*c[5])()
declare c as array 5 of pointer to function returning double
c++decl> explain double (*d())[5]
declare d as function returning pointer to array 5 of double
c++decl> exit
$

Zwack
February 16th, 2008, 06:44 AM
double *a[n];


A pointer to an n sized array of doubles. It can also pretty much be written as double **a or
double a[x][n];



double (*b)[n];


Ive never come across this in practice but I think that you might well be right.



double (*c[n])();


a pointer to an array of functions that return double. No, really. that's what I read that as.



double (*d())[n];


An array of pointers to functions that return double. Note that this is not quite the same as above, but in practice is indistinguishable...

I'm not entirely sure about this, and I'm sure I'll be corrected if I'm wrong but that is how I read these.

Z.

LaRoza
February 16th, 2008, 06:49 AM
@OP A pointer to a function is written like so:



int (*putsTwo)(char*);



#include <stdio.h>
int main(int args,char ** argv)
{
int (*putsTwo)(const char*);
putsTwo = puts;
putsTwo("Hello world");

}

rbprogrammer
February 17th, 2008, 01:14 AM
am i correct in thinking that because of the order of precedence, these two are equivalent??


double *a[n];
double (*b)[n];

aks44
February 17th, 2008, 01:55 AM
am i correct in thinking that because of the order of precedence, these two are equivalent??


double *a[n];
double (*b)[n];


No. See stroyan's post:


c++decl> explain double *a[5];
declare a as array 5 of pointer to double
c++decl> explain double (*b)[5]
declare b as pointer to array 5 of double

rbprogrammer
February 17th, 2008, 02:24 AM
oh i see why... i feel stupid, i mixed up the order of precedence of the array access operator and the dereference operator. i got it now.... thanks so much :guitar:

LaRoza
February 17th, 2008, 03:58 AM
oh i see why... i feel stupid, i mixed up the order of precedence of the array access operator and the dereference operator. i got it now.... thanks so much :guitar:

Don't feel stupid, everyone is confused by pointers at some point in their lives.

Extra credit:


char (*(*x[3])())[5];

Zwack
February 17th, 2008, 04:15 AM
Don't feel stupid, everyone is confused by pointers at some point in their lives.

Extra credit:


char (*(*x[3])())[5];


Was that when the programmer sneezed while typing? :D

Z.

LaRoza
February 17th, 2008, 04:29 AM
Was that when the programmer sneezed while typing? :D

Z.

x is an array[3] of pointers to a function returning a pointer to an array[5] of char

rbprogrammer
February 17th, 2008, 05:22 PM
...
Extra credit:


char (*(*x[3])())[5];


what the ....... now an extra credit for you Mr. LaRoza, where on earth would that come in handy?? :lolflag:

LaRoza
February 17th, 2008, 05:25 PM
what the ....... now an extra credit for you Mr. LaRoza, where on earth would that come in handy??

(Just "LaRoza" please)

x is an array[3] of pointers to a function returning a pointer to an array[5] of char.

That may be useful in some situations, but it is not the type of programming I do.

rbprogrammer
February 17th, 2008, 05:50 PM
(Just "LaRoza" please)

x is an array[3] of pointers to a function returning a pointer to an array[5] of char.

That may be useful in some situations, but it is not the type of programming I do.

lol, yea i dont think i will be using that notation in any programming that i will be doing :guitar:

LaRoza
February 17th, 2008, 05:52 PM
lol, yea i dont think i will be using that notation in any programming that i will be doing

Don't you want to be a three start programmer?

http://c2.com/cgi/wiki?ThreeStarProgrammer