some pointer issues....

S

sumedh.....

double * X[5]

size of X->??
size of X[0]->??

double (*X)[5]
size of X->??
size of X[0]->??


Also if i want to print sizeof(main) it gives me 1
and sizeof(main()) gives me 4 why?
 
A

anon.asdf

double * X[5]

size of X->??
size of X[0]->??

double (*X)[5]
size of X->??
size of X[0]->??

Also if i want to print sizeof(main) it gives me 1
and sizeof(main()) gives me 4 why?

Try this test program:

/****************************/
#include <stdio.h>

double *X[5];
/* X is an array of 5 "pointers to double" */

double (*Y)[5];
/* Y is a pointer to an array of 5 doubles */

int main(void)
{
double array_of_5_double[] = {0.5, 1.5, 2.5, 3.5, 4.5};
X[0] = &array_of_5_double[0];
X[1] = &array_of_5_double[1];
X[2] = &array_of_5_double[2];
X[3] = &array_of_5_double[3];
X[4] = &array_of_5_double[4];
#if 0 // why does this not work?
X = {&array_of_5_double[0], /* array_of_5_double */ \
&array_of_5_double[1], \
&array_of_5_double[2], \
&array_of_5_double[3], \
&array_of_5_double[4]};
#endif
printf("sizeof(double) = %d // sizeof(double *) = %d //
sizeof(double (*)[5]) = %d\n", \
sizeof(double), sizeof(double *), sizeof(double (*)[5]));
/* 8 */

printf("sizeof(X) = %d // X = %p\n", sizeof(X), X,
&array_of_5_double);

printf("sizeof(X[0]) = %d // X[0] = %p // &array_of_5_double[0]
= %p\n", sizeof(X[0]), X[0], &array_of_5_double[0]);
// X[0] is the first element of our array: here it is a pointer to a
double with value 0.5

printf("sizeof(Y) = %d\n", sizeof(Y));

printf("sizeof(Y[0]) = %d\n", sizeof(Y[0])); // 40

printf("sizeof(main) = %d // sizeof(main()) = %d\n", sizeof(main),
sizeof(main()));

return 0;
}

/*
main is a function pointer!
main() is the invocation of the function "int main(void)" which will
return an integer of the following size:
sizeof(int) == sizeof(main())
*/

Explanations:

double *X[5];
/* X is an array of 5 "pointers to double" */
Thus sizeof(X) = 5*sizeof(double *)
"double *" is a pointer to a double (tip: move from right to left)


double (*Y)[5];
/* Y is a pointer to an array of 5 doubles */
Thus sizeof(Y) = sizeof(double (*)[5])
"double (*)[5]" is a pointer to an array of 5 doubles (tip: move from
inside to outside)
/*


/*
main is a function pointer!
main() is the invocation of the function "int main(void)" which will
return an integer of the following size:
sizeof(int)
This sizeof(main()) is the same size, i.e. the size of the returned
integer
*/

-anon.asdf
 
S

sumedh.....

double * X[5]
size of X->??
size of X[0]->??
double (*X)[5]
size of X->??
size of X[0]->??
Also if i want to print sizeof(main) it gives me 1
and sizeof(main()) gives me 4 why?

Try this test program:

/****************************/
#include <stdio.h>

double *X[5];
/* X is an array of 5 "pointers to double" */

double (*Y)[5];
/* Y is a pointer to an array of 5 doubles */

int main(void)
{
double array_of_5_double[] = {0.5, 1.5, 2.5, 3.5, 4.5};
X[0] = &array_of_5_double[0];
X[1] = &array_of_5_double[1];
X[2] = &array_of_5_double[2];
X[3] = &array_of_5_double[3];
X[4] = &array_of_5_double[4];
#if 0 // why does this not work?
X = {&array_of_5_double[0], /* array_of_5_double */ \
&array_of_5_double[1], \
&array_of_5_double[2], \
&array_of_5_double[3], \
&array_of_5_double[4]};
#endif
printf("sizeof(double) = %d // sizeof(double *) = %d //
sizeof(double (*)[5]) = %d\n", \
sizeof(double), sizeof(double *), sizeof(double (*)[5]));
/* 8 */

printf("sizeof(X) = %d // X = %p\n", sizeof(X), X,
&array_of_5_double);

printf("sizeof(X[0]) = %d // X[0] = %p // &array_of_5_double[0]
= %p\n", sizeof(X[0]), X[0], &array_of_5_double[0]);
// X[0] is the first element of our array: here it is a pointer to a
double with value 0.5

printf("sizeof(Y) = %d\n", sizeof(Y));

printf("sizeof(Y[0]) = %d\n", sizeof(Y[0])); // 40

printf("sizeof(main) = %d // sizeof(main()) = %d\n", sizeof(main),
sizeof(main()));

return 0;

}

/*
main is a function pointer!
main() is the invocation of the function "int main(void)" which will
return an integer of the following size:
sizeof(int) == sizeof(main())
*/

Explanations:

double *X[5];
/* X is an array of 5 "pointers to double" */
Thus sizeof(X) = 5*sizeof(double *)
"double *" is a pointer to a double (tip: move from right to left)

double (*Y)[5];
/* Y is a pointer to an array of 5 doubles */
Thus sizeof(Y) = sizeof(double (*)[5])
"double (*)[5]" is a pointer to an array of 5 doubles (tip: move from
inside to outside)
/*

/*
main is a function pointer!
main() is the invocation of the function "int main(void)" which will
return an integer of the following size:
sizeof(int)
This sizeof(main()) is the same size, i.e. the size of the returned
integer
*/

-anon.asdf

gr8 explanation:
just wanted to know why does
sizeof(main) -> 1
sizeof(main()) -> sizeof(int)?????
 
M

Mike Wahler

sumedh..... said:
double * X[5]

Array of five pointers to type double
size of X->??

size of X is reported by

sizeof X

or

sizeof(double *[5])

I recommend the first method.
size of X[0]->??

size of X[0] is reported by

sizeof X[0]

or

sizeof(double)

Again, I recommend the first method.
double (*X)[5]

Pointer to an array of five doubles
size of X->??

size of X is reported by

sizeof X

or

sizeof(double(*)[5]

Again, I recommend the first method.
size of X[0]->??

size of X[0] is reported by

sizeof X[0]

or

sizeof(double[5])

Again, I recommend the first method.
Also if i want to print sizeof(main) it gives me 1
and sizeof(main()) gives me 4 why?

'main' (without parentheses) yields the address of
the function 'main()'. So sizeof(main) will give
the size of a pointer to a function. 'main()' invokes
the function main(), and returns its return value. This
type is 'int'. So 'sizeof(main())' gives the size of
type 'int'.

Note that these type sizes will vary among platforms (and
could conceivably vary among implementations for the same
platform).

-Mike
 
M

Mike Wahler

sumedh..... said:
On Aug 12, 9:21 pm, (e-mail address removed) wrote:
sizeof(main) -> 1
sizeof(main()) -> sizeof(int)?????

The type of 'main' is a pointer type.
The type of 'main()' is int.

Obviously, the sizes of these two different types
are not the same on your platform.

-Mike
 
S

sumedh.....

The type of 'main' is a pointer type.
The type of 'main()' is int.

Obviously, the sizes of these two different types
are not the same on your platform.

-Mike

i have read that size of pointer is 2bytes,
its 4bytes on my compiler... not an issue...
now...
If its a pointer type? why an integer/float/void/double pointer
declared inside main yields there size as 4 and main as 1?
so is this pointer diff than others?
 
F

Flash Gordon

Mike Wahler wrote, On 12/08/07 18:42:
The type of 'main' is a pointer type.

Wrong. The time of main is a function, not a pointer. sizeof(main) is
actually a constraint violation and a diagnostic is required. Of course,
with some C compilers (e.g. gcc) you have to poke the compiler hard
enough to get all the required diagnostics. Personally I think this gcc
extension is pointless.
The type of 'main()' is int.

Obviously, the sizes of these two different types
are not the same on your platform.

Indeed.
 
F

Flash Gordon

sumedh..... wrote, On 12/08/07 19:17:
i have read that size of pointer is 2bytes,

Whatever you read is implementation specific, not part of C. Also
irrelevant.
its 4bytes on my compiler... not an issue...

As you see. I've used implementations where the sizeof a pointer is 1.
now...
If its a pointer type? why an integer/float/void/double pointer
declared inside main yields there size as 4 and main as 1?
so is this pointer diff than others?

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

Mike Wahler

sumedh..... said:
i have read that size of pointer is 2bytes,
its 4bytes on my compiler... not an issue...

The C language does not specify particular
sizes for pointer types. This depends upon your
implementation.
now...
If its a pointer type? why an integer/float/void/double pointer
declared inside main yields there size as 4 and main as 1?
so is this pointer diff than others?

There are several different pointer types in C. E.g.
'pointer to int', 'pointer to double', 'pointer to function',
etc. There's no requirement that the sizes of these pointers
are the same?

BTW why are you so concerned about these sizes? If some
part(s) of your program needs to know, just use the sizeof
operator. The specific sizes should not concern you.

-Mike
 
M

Mike Wahler

Flash Gordon said:
sumedh..... wrote, On 12/08/07 19:17:

Whatever you read is implementation specific, not part of C. Also
irrelevant.


As you see. I've used implementations where the sizeof a pointer is 1.


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?

-Mike
 
M

Mike Wahler

Flash Gordon said:
Mike Wahler wrote, On 12/08/07 18:42:

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
 
F

Flash Gordon

Mike Wahler wrote, On 12/08/07 20:34:
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*.
 
P

pete

Mike said:
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.
 
K

Keith Thompson

Mike Wahler said:
'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>
 
S

sumedh.....

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?
 
P

pete

sumedh..... said:
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.
 
P

pete

pete said:
sumedh..... wrote:

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.
 
I

Ian Collins

sumedh..... said:
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;
}
 
S

sumedh.....

Does this make it clearer?

typedef int (*Fn)(void);

int main( void ) {
Fn fn = main;

}

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;
}
 
I

Ian Collins

*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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top