1

Possible Duplicate:
In C arrays why is this true? a[5] == 5[a]

  1. If p is a pointer (say int * p), then what does [p] means ? Also what does 4[p] means ? (i.e. multiplying a scalar with [p] )

  2. Suppose xyz is some data type defined by in the program. Then what does the

    void (*xyz)(void);
    

    statement mean?

Community
  • 1
  • 1
user1599964
  • 860
  • 2
  • 13
  • 29
  • please don't ask several questions at once and provide a proper question title. Your first question is answered in the FAQ: http://stackoverflow.com/questions/381542/in-c-arrays-why-is-this-true-a5-5a – Jens Gustedt Jan 20 '13 at 12:49

4 Answers4

6
  1. 4[p] means the same as p[4]. See e.g. http://c-faq.com/aryptr/joke.html.

  2. If xyz is already a data type, then that's an error. If not, then it's the definition of a function pointer called xyz. Assuming that you meant "void" not "coid", then cdecl.org tells us:

    declare xyz as pointer to function (void) returning void

Community
  • 1
  • 1
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • 1
    Strictly speaking, (2) asks for the meaning of the statement. I.e. something like "it's a declaration statement"... – Kerrek SB Jan 20 '13 at 12:36
  • Also, if `xyz` is a data type, then the 2nd declaration statement is a syntax error. –  Jan 20 '13 at 12:38
3

if p is defined as int *p then

[p] is wrong! cause an error: expected expression before ‘[’ token.

Where as 0[p] is correct!

And 0[p] is same as p[0] , similarly p[4] is 4[p].

compiler convert p[4] into *(p + 4) that as *(4 + p) => 4[p]

Additionally, suppose if you have an array say int a[10], you can access elements of array either as a[i] or i[a]

following example will be useful, I think:

int main(){
    int a[5] = {1,2,3,4,5};
    int* p;  // similar declaration of p (you asked)
    p = a;
    int i= 0; 
    for(i=0; i < 5; i++){
        printf("a[i] =  %d  and i[a] = %d \n",a[i],i[a]);
    }
    printf(" using p \n"); // access using pointer.  
    for(i=0; i < 5; i++){
        printf("p[i] =  %d  and i[p] = %d \n",p[i],i[p]);
    }
}    

compile and execution:

:~$ ./a.out 
a[i] =  1  and i[a] = 1 
a[i] =  2  and i[a] = 2 
a[i] =  3  and i[a] = 3 
a[i] =  4  and i[a] = 4 
a[i] =  5  and i[a] = 5 
 using p 
p[i] =  1  and i[p] = 1 
p[i] =  2  and i[p] = 2 
p[i] =  3  and i[p] = 3 
p[i] =  4  and i[p] = 4 
p[i] =  5  and i[p] = 5 

[ANSWER-2 ]

A declaration void (*xyz)(void); creates xyz a pointer to function that returns void and arguments are void. (xyz is not a data-type but a pointer variable) e.g.

void function(void){
 // definition 
}

void (*xyz)(void);   

then xyz can be assigned address of function:

xyz = function;   

And using xyz() you can call function(), A example for void (*xyz)(void):

#include<stdio.h>
void function(void){
    printf("\n An Example\n");
} 
int main(){
    void (*xyz)(void);   
    xyz = function;    
    xyz();
}

Now compile and execute it:

:~$ gcc  x.c
:~$ ./a.out 

 An Example
:~$ 
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
2

what does [p] mean?

Nothing in itself.

Also what does 4[p] mean?

Due to pointer arithmetic, 4[p] means *(4 + p), which is, given that addition is commutative, equivalent to *(p + 4), which in turn can be written as p[4], i. e. it's the 5th element of an array pointed to by p.

If xyz is a data type, then what does void (*xyz)(void); statement mean?

It's a syntax error then.

If xyz is not a data type, then it declares xyz to be a function pointer taking and returning void (i. e. "nothing").

2

1) 4[p] means the same as p[4] both of which essentially mean *(p+4) which means the 5th element from the start of the array p.

2) xyz is the type of a pointer to a function that takes a no arguements and returns nothing.

typedef void (*xyz)(void);    

void func();

xyz f= func;

It could also be the function pointer itself if used in the below fashions

//imagine the above typedef is omitted.

void (*xyz)(void) = func;

void (*xyz)(void);  // uninitialized pointer.
Karthik T
  • 31,456
  • 5
  • 68
  • 87