Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > some pointer issues....

Reply
Thread Tools

some pointer issues....

 
 
Mike Wahler
Guest
Posts: n/a
 
      08-12-2007

"Flash Gordon" <> wrote in message
news:...
> Mike Wahler wrote, On 12/08/07 18:42:
>> "sumedh....." <> wrote in message
>> news: oups.com...
>>> On Aug 12, 9:21 pm, anon.a...@gmail.com wrote:
>>> sizeof(main) -> 1
>>> sizeof(main()) -> sizeof(int)?????

>>
>> The type of 'main' is a pointer type.

>
> Wrong. The time of main is a function,


Yes, its type is 'function'. But in a program,
the name (without parens) will yield an address.

-Mike


 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      08-12-2007
Mike Wahler wrote, On 12/08/07 20:34:
> "Flash Gordon" <> wrote in message
> news:...
>> Mike Wahler wrote, On 12/08/07 18:42:
>>> "sumedh....." <> wrote in message
>>> news: oups.com...
>>>> On Aug 12, 9:21 pm, anon.a...@gmail.com wrote:
>>>> sizeof(main) -> 1
>>>> sizeof(main()) -> sizeof(int)?????
>>> The type of 'main' is a pointer type.

>> Wrong. The time of main is a function,

>
> Yes, its type is 'function'. But in a program,
> the name (without parens) will yield an address.


N1124 section 6.3.2.1 para 4 it says
| A function designator is an expression that has function type.
| Except when it is the operand of the sizeof operator54) or the
| unary & operator, a function designator with type ‘‘function
| returning type’’ is converted to an expression that has type
| ‘‘pointer to function returning type’’.

Further, the constraints section of the definition of the sizeof
operator says:
| The sizeof operator shall not be applied to an expression that
| has function type or ...

So as I said the type is function, not pointer to funtion, and a
diagnostic (warning or error) is *required*.
--
Flash Gordon
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      08-12-2007
Mike Wahler wrote:
>
> "Flash Gordon" <> wrote in message


> > Because main is not a pointer and Mike was wrong.

>
> Huh? Are you saying that the name of a function (without parentheses)
> does not yeild a pointer value?


The only two times when an expression of function type is not
automatically converted to a pointer, are:
1 as an operand of &
2 as an operand of sizeof

Since sizeof is not defined for expressions of function type,
the only thing that you can do with an expression of function type
in a correct C program, is to derive a pointer from it.

--
pete
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-12-2007
"Mike Wahler" <> writes:
[...]
> 'main' (without parentheses) yields the address of
> the function 'main()'.


In most contexts, yes.

> So sizeof(main) will give
> the size of a pointer to a function.


No, the operand of a sizeof operator is one of the contexts in which a
function name (more generally an expression of function type) is not
converted to a pointer. 'sizeof(main)' (or 'sizeof main'; the
parentheses are not necessary) is a constraint violation. <OT>gcc's
"-pedantic" option causes it to emit the required diagnostic.</OT>

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
sumedh.....
Guest
Posts: n/a
 
      08-13-2007
On Aug 13, 2:35 am, pete <pfil...@mindspring.com> wrote:
> Mike Wahler wrote:
>
> > "Flash Gordon" <s...@flash-gordon.me.uk> wrote in message
> > > Because main is not a pointer and Mike was wrong.

>
> > Huh? Are you saying that the name of a function (without parentheses)
> > does not yeild a pointer value?

>
> The only two times when an expression of function type is not
> automatically converted to a pointer, are:
> 1 as an operand of &
> 2 as an operand of sizeof
>
> Since sizeof is not defined for expressions of function type,
> the only thing that you can do with an expression of function type
> in a correct C program, is to derive a pointer from it.
>
> --
> pete


pete would you like to explain by giving an example?
if i derive a pointer from it what would be the type of that pointer?

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      08-13-2007
sumedh..... wrote:
>
> On Aug 13, 2:35 am, pete <pfil...@mindspring.com> wrote:
> > Mike Wahler wrote:
> >
> > > "Flash Gordon" <s...@flash-gordon.me.uk> wrote in message
> > > > Because main is not a pointer and Mike was wrong.

> >
> > > Huh? Are you saying that the name of a function (without parentheses)
> > > does not yeild a pointer value?

> >
> > The only two times when an expression of function type is not
> > automatically converted to a pointer, are:
> > 1 as an operand of &
> > 2 as an operand of sizeof
> >
> > Since sizeof is not defined for expressions of function type,
> > the only thing that you can do with an expression of function type
> > in a correct C program, is to derive a pointer from it.
> >
> > --
> > pete

>
> pete would you like to explain by giving an example?
> if i derive a pointer from it what would be the type of that pointer?


The type of (&main) is a pointer to a function
returning type int and taking no arguments.

--
pete
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      08-13-2007
pete wrote:
>
> sumedh..... wrote:


> > > Since sizeof is not defined for expressions of function type,
> > > the only thing that you can do with an expression of function type
> > > in a correct C program, is to derive a pointer from it.
> > >
> > > --
> > > pete

> >
> > pete would you like to explain by giving an example?
> > if i derive a pointer from it what would
> > be the type of that pointer?

>
> The type of (&main) is a pointer to a function
> returning type int and taking no arguments.


Well actually, how many arguments depends on how main is defined.

--
pete
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      08-13-2007
sumedh..... wrote:
> On Aug 13, 2:35 am, pete <pfil...@mindspring.com> wrote:
>> Mike Wahler wrote:
>>
>>> "Flash Gordon" <s...@flash-gordon.me.uk> wrote in message
>>>> Because main is not a pointer and Mike was wrong.
>>> Huh? Are you saying that the name of a function (without parentheses)
>>> does not yeild a pointer value?

>> The only two times when an expression of function type is not
>> automatically converted to a pointer, are:
>> 1 as an operand of &
>> 2 as an operand of sizeof
>>
>> Since sizeof is not defined for expressions of function type,
>> the only thing that you can do with an expression of function type
>> in a correct C program, is to derive a pointer from it.
>>

> pete would you like to explain by giving an example?
> if i derive a pointer from it what would be the type of that pointer?
>

Does this make it clearer?

typedef int (*Fn)(void);

int main( void ) {
Fn fn = main;
}

--
Ian Collins.
 
Reply With Quote
 
sumedh.....
Guest
Posts: n/a
 
      08-13-2007

> Does this make it clearer?
>
> typedef int (*Fn)(void);
>
> int main( void ) {
> Fn fn = main;
>
> }
>
> --
> Ian Collins.


typedef int (*Fn)(void);
int main( void ) {
Fn fn = main;
printf("%d\n",sizeof(fn)); // o/ps 4 (that is size of pointer to
function returning int.)
printf("%d\n",sizeof(&fn)); // o/ps 4 (that is size of pointer to
function returning int.)
printf("%d\n",sizeof(&main)); // o/ps 4 (that is size of pointer to
function returning int.)
printf("%d\n",sizeof(main)); //o/ps 1 inspite of main & (&main) are
both the same thing.. pointer //to function returning int
printf("%d %d \n",fn,&fn); //o/ps addresses A & B
printf("%d %d \n",main,&main); //o/ps addresses A & A
system("PAUSE");
return 0;
}

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      08-13-2007
sumedh..... wrote:
>> Does this make it clearer?
>>
>> typedef int (*Fn)(void);
>>
>> int main( void ) {
>> Fn fn = main;
>>
>> }
>>


*Please don't quote signatures.*

>
> typedef int (*Fn)(void);
> int main( void ) {
> Fn fn = main;


> printf("%d\n",sizeof(main)); //o/ps 1 inspite of main & (&main) are
> both the same thing.. pointer //to function returning int


Constraint violation, see 6.5.3.4.1

"The sizeof operator shall not be applied to an expression that has
function type..."

Did this even compile?

--
Ian Collins.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57