473,842 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

some pointer issues....

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?

Aug 12 '07 #1
34 1918
On 12 Aug., 17:24, "sumedh.... ." <sumedhsak...@g mail.comwrote:
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_doub le[] = {0.5, 1.5, 2.5, 3.5, 4.5};
X[0] = &array_of_5_dou ble[0];
X[1] = &array_of_5_dou ble[1];
X[2] = &array_of_5_dou ble[2];
X[3] = &array_of_5_dou ble[3];
X[4] = &array_of_5_dou ble[4];
#if 0 // why does this not work?
X = {&array_of_5_do uble[0], /* array_of_5_doub le */ \
&array_of_5_dou ble[1], \
&array_of_5_dou ble[2], \
&array_of_5_dou ble[3], \
&array_of_5_dou ble[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_dou ble);

printf("sizeof( X[0]) = %d // X[0] = %p // &array_of_5_dou ble[0]
= %p\n", sizeof(X[0]), X[0], &array_of_5_dou ble[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

Aug 12 '07 #2
On Aug 12, 9:21 pm, anon.a...@gmail .com wrote:
On 12 Aug., 17:24, "sumedh.... ." <sumedhsak...@g mail.comwrote:
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_doub le[] = {0.5, 1.5, 2.5, 3.5, 4.5};
X[0] = &array_of_5_dou ble[0];
X[1] = &array_of_5_dou ble[1];
X[2] = &array_of_5_dou ble[2];
X[3] = &array_of_5_dou ble[3];
X[4] = &array_of_5_dou ble[4];
#if 0 // why does this not work?
X = {&array_of_5_do uble[0], /* array_of_5_doub le */ \
&array_of_5_dou ble[1], \
&array_of_5_dou ble[2], \
&array_of_5_dou ble[3], \
&array_of_5_dou ble[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_dou ble);

printf("sizeof( X[0]) = %d // X[0] = %p // &array_of_5_dou ble[0]
= %p\n", sizeof(X[0]), X[0], &array_of_5_dou ble[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)???? ?

Aug 12 '07 #3

"sumedh.... ." <su**********@g mail.comwrote in message
news:11******** *************@i 13g2000prf.goog legroups.com...
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
Aug 12 '07 #4

"sumedh.... ." <su**********@g mail.comwrote in message
news:11******** **************@ i38g2000prf.goo glegroups.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.
The type of 'main()' is int.

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

-Mike

Aug 12 '07 #5
On Aug 12, 10:42 pm, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
"sumedh.... ." <sumedhsak...@g mail.comwrote in message

news:11******** **************@ i38g2000prf.goo glegroups.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.
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?

Aug 12 '07 #6
Mike Wahler wrote, On 12/08/07 18:42:
"sumedh.... ." <su**********@g mail.comwrote in message
news:11******** **************@ i38g2000prf.goo glegroups.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, 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.
--
Flash Gordon
Aug 12 '07 #7
sumedh..... wrote, On 12/08/07 19:17:
On Aug 12, 10:42 pm, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
>"sumedh..... " <sumedhsak...@g mail.comwrote in message

news:11******* *************** @i38g2000prf.go oglegroups.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.
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,
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.
--
Flash Gordon
Aug 12 '07 #8

"sumedh.... ." <su**********@g mail.comwrote in message
news:11******** *************@i 13g2000prf.goog legroups.com...
On Aug 12, 10:42 pm, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
>"sumedh..... " <sumedhsak...@g mail.comwrote in message

news:11******* *************** @i38g2000prf.go oglegroups.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.
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...
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
Aug 12 '07 #9

"Flash Gordon" <sp**@flash-gordon.me.ukwro te in message
news:0r******** ****@news.flash-gordon.me.uk...
sumedh..... wrote, On 12/08/07 19:17:
>On Aug 12, 10:42 pm, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
>>"sumedh.... ." <sumedhsak...@g mail.comwrote in message

news:11****** *************** *@i38g2000prf.g ooglegroups.com ...

On Aug 12, 9:21 pm, anon.a...@gmail .com wrote:
sizeof(mai n) -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

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.
Huh? Are you saying that the name of a function (without parentheses)
does not yeild a pointer value?

-Mike
Aug 12 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

53
5768
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is difficult to know what is going on. One of these Order Forms you can see here... http://www.cardman.co.uk/orderform.php3
2
1657
by: Robbie Hatley | last post by:
About 20 days ago, "John Harrison" <john_andronicus@hotmail.com> replied to a message which I had written: > > > Q1 . What is the use of pointer to an array? > > > > It points to it. (The name of an array is also a pointer to it.) > > No, the name of an array decays to a pointer to the first element of the > array. A pointer to an array and a pointer to the first element of an array > are not the same thing.
193
9677
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I speak only of features in the spirit of C; something like object-orientation, though a nice feature, does not belong in C. Something like being able to #define a #define would be very handy, though, e.g: #define DECLARE_FOO(bar) #define...
9
5303
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer from integer of different size. Now I thought that when it was a void I could pass anything? Thing is it works when I use an int, but in this case I wanted to use a char. It wouldnt be hard to work around it, but it annoys me because I've heard...
40
3057
by: Steve Rencontre | last post by:
I can't for the life of me see how to do pointer-to-member when the member is actually part of an embedded structure. That is, if I have: struct S1 { int a; }; struct S2 { S1 s; int b; }; how can I get a pointer to the a in an S2?
7
6347
by: Mohan | last post by:
Hi, What are the advantages/disadvantages of using a pointer instead of Reference in the Copy Constructor ? For Example, Writing the Copy constructor for the Class "Temp" as below, Temp(const base *ptrBase)
33
5095
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the actual counting. Here is the latter's definition: // --- Begin ReferenceCountable.h ---------- class ReferenceCountable
9
15089
by: Dave Stallard | last post by:
Pardon if this is the wrong newsgroup for this question, and/or if this question is naive. I have a multi-threaded Windows application in which certain variables/object fields are shared: one thread may write the variable, and the other thread read it. The variables in question may have int or int* types. Question: Is it safe to do this? Or is it possible a read that happens at the same time as a write may retrieve a scrambled value,...
11
2983
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++, that anybody who doesn't use it might as well just program in plain C. I totally disagree with this, because I think C++ has a lot of great features apart from polymorphism, such as the ability to organize code into classes, code reuse through...
0
9715
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10674
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10317
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9454
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7040
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5697
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4500
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4090
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3146
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.