linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Function pointers
@ 2010-01-21  6:33 kumar11
  2010-01-21  6:57 ` saravanaraj v
  2010-01-21  9:56 ` Glynn Clements
  0 siblings, 2 replies; 5+ messages in thread
From: kumar11 @ 2010-01-21  6:33 UTC (permalink / raw)
  To: linux-c-programming


Could you please explain the meaning of below declaration statement.

char (*pa())[4]; 


Please find the entire code below.

char (*pa())[4]; 
void main(){ 
char(*p)[4]=pa(); 
printf("%d",**p); 
} 
char (*pa())[4]{ 
static char arr[]={'\11','\12','\13','\14'}; 
return &arr; 
}
-- 
View this message in context: http://old.nabble.com/Function-pointers-tp27253432p27253432.html
Sent from the linux-c-programming mailing list archive at Nabble.com.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Function pointers
  2010-01-21  6:33 Function pointers kumar11
@ 2010-01-21  6:57 ` saravanaraj v
  2010-01-21  9:56 ` Glynn Clements
  1 sibling, 0 replies; 5+ messages in thread
From: saravanaraj v @ 2010-01-21  6:57 UTC (permalink / raw)
  To: kumar11; +Cc: linux-c-programming

[root@build ~]# cdecl
Type `help' or `?' for help
cdecl> explain char (*pa())[4];
declare pa as function returning pointer to array 4 of char
cdecl> explain char (*p)[4]
declare p as pointer to array 4 of char
cdecl>


Try the following code.

#include <stdio.h>

char (*pa())[4];
int main(){
        char(*p)[4]=pa();
        /*printf("%d",**p);*/
        printf("%d %d %d %d\n",(*p)[0],(*p)[1],(*p)[2],(*p)[3]);
        return 0;
}
char (*pa())[4]{
        static char arr[]={'\11','\12','\13','\14'};
        return &arr;
}


On Thu, Jan 21, 2010 at 12:03 PM, kumar11 <ravikumar.chakram@gmail.com> wrote:
>
> Could you please explain the meaning of below declaration statement.
>
> char (*pa())[4];
>
>
> Please find the entire code below.
>
> char (*pa())[4];
> void main(){
> char(*p)[4]=pa();
> printf("%d",**p);
> }
> char (*pa())[4]{
> static char arr[]={'\11','\12','\13','\14'};
> return &arr;
> }
> --
> View this message in context: http://old.nabble.com/Function-pointers-tp27253432p27253432.html
> Sent from the linux-c-programming mailing list archive at Nabble.com.
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Function pointers
  2010-01-21  6:33 Function pointers kumar11
  2010-01-21  6:57 ` saravanaraj v
@ 2010-01-21  9:56 ` Glynn Clements
  2010-01-23 22:27   ` Milind A Choudhary
  1 sibling, 1 reply; 5+ messages in thread
From: Glynn Clements @ 2010-01-21  9:56 UTC (permalink / raw)
  To: kumar11; +Cc: linux-c-programming


kumar11 wrote:

> Could you please explain the meaning of below declaration statement.
> 
> char (*pa())[4]; 

"pa" is a function returning a pointer to an array of four characters.

C declarations work by declaring an expression involving the
identifier as having a named type (in this case, "char").

"(*pa())[i]" is a character, for 0 <= i < 4.

Therefore:

"(*pa())" is an array of four characters.

Therefore:

"pa()" is a pointer to an array of four characters.

Therefore:

"pa" is a function returning a pointer to an array of four characters.

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Function pointers
  2010-01-21  9:56 ` Glynn Clements
@ 2010-01-23 22:27   ` Milind A Choudhary
       [not found]     ` <201001241003091879376@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread
From: Milind A Choudhary @ 2010-01-23 22:27 UTC (permalink / raw)
  To: Glynn Clements; +Cc: kumar11, linux-c-programming

On Thu, Jan 21, 2010 at 4:56 AM, Glynn Clements
<glynn@gclements.plus.com> wrote:
>
> kumar11 wrote:
>
>> Could you please explain the meaning of below declaration statement.
>>
>> char (*pa())[4];
>
> "pa" is a function returning a pointer to an array of four characters.
>

You already have got the required answer from others.
C and function pointers can always be confusing. At times you think
you have understood it throughly and but still not be able to decode some
more complex declarations. (cdec can come to help but not always..at least not
in a technical interview ) The best approach that I have come across is
the `Clockwise Spiral` rule. (link :
http://www.c-faq.com/decl/spiral.anderson.html )

Hope this helps..

Thanks & Regards
Milind A Choudhary
http://www.csl.mtu.edu/~machoudh/

-- 
Milind  Arun Choudhary

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Re: Re: Function pointers
       [not found]     ` <201001241003091879376@gmail.com>
@ 2010-01-24  7:59       ` Glynn Clements
  0 siblings, 0 replies; 5+ messages in thread
From: Glynn Clements @ 2010-01-24  7:59 UTC (permalink / raw)
  To: simon; +Cc: Milind A Choudhary, kumar11, linux-c-programming


simon wrote:

> char (*pa())[4]; 
> is this mean that an pa function pointer with return value like a char a[4] and it's argument is nothing ?
> 
> right?

Wrong.

1. pa is not a pointer to a function, it's a function which returns a
pointer. "()" has higher precedence than "*", so "*pa()" is parsed as
"*(pa())" (function returning pointer) rather than "(*pa)()" (pointer
to function).

2. An empty parameter list "()" indicates that the function takes
unspecified arguments. A function which takes no arguments has
"(void)" as the parameter list.

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-01-24  7:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-01-21  6:33 Function pointers kumar11
2010-01-21  6:57 ` saravanaraj v
2010-01-21  9:56 ` Glynn Clements
2010-01-23 22:27   ` Milind A Choudhary
     [not found]     ` <201001241003091879376@gmail.com>
2010-01-24  7:59       ` Re: " Glynn Clements

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).