Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Some usage of sizeof (http://www.go4expert.com/forums/usage-sizeof-t24334/)

wdliming 1Jan2011 10:14

Some usage of sizeof
 
Code:

#include<stdio.h>
#include<string.h>
char foo()
{
        printf("foo() has been called.\n");
        return 'a';
}
/*void foo3(char a3[3])
{
        int c3 = sizeof( a3 ); // c3 ==
        printf("c3=sizeof( a3 )=%d\n",c3);
}
void foo4(char a4[])
{
        int c4 = sizeof( a4 ); // c4 ==
        printf("c4=sizeof( a4 )=%d\n",c4);
}*/

/*mian function*/
int main()
{
        int n;
        n = 10;
        char ary[n];                 
        printf("sizeof(ary)      =%d\n", sizeof(ary)); // ok. output: 10
       
        size_t sz = sizeof( foo() );        // The type return value of the foo()is char,
                                                                        //besides sz=sizeof(char),foo()Will not be called
        printf("sizeof( foo() )  =%d\n", sz);
       
        /*****************************Data Type*****************************************************/
        printf("sizeof(char)        =%d\n",sizeof(char));
        printf("sizeof(signed char)  =%d\n",sizeof(signed char));
        printf("sizeof(unsigned char)=%d\n",sizeof(unsigned char));
        printf("sizeof(int)          =%d\n",sizeof(int));
        printf("sizeof(signed int)  =%d\n",sizeof(signed int));
        printf("sizeof(unsigned int) =%d\n",sizeof(unsigned int));
        printf("sizeof(float)        =%d\n",sizeof(float));
        printf("sizeof(double)      =%d\n",sizeof(double));
       
        /******************************point**********************************************************/
        char *pc = "abc";
        int *pi;
        //string * ps;
        char **ppc = &pc;
        void (*pf)();//
        printf("sizeof(pc)          =%d\n",sizeof(pc)); //4
        printf("sizeof(pi)          =%d\n",sizeof(pi)); //4
        //sizeof( ps ); //
        printf("sizeof(ppc)        =%d\n",sizeof(ppc)); //4
        printf("sizeof(pf)          =%d\n",sizeof(pf));// 4
       
  /***********************Array*****************************************************************/
        char a1[] = "abc";
        int a2[3];
        printf("sizeof(a1[])    =%d\n",sizeof(a1));// 4
       
        printf("sizeof(a2[])    =%d\n",sizeof(a2[3]));//3*4=12(attach to int)
       
        /*Calculate the numbers of the array*/
       
        int c1; //  Total length / length of a single element
        printf("c1= sizeof( a1 ) / sizeof(char) =%d\n",c1= sizeof( a1 ) / sizeof( char ));
        int c2;//  Total length / length of the first element
        printf("c2= sizeof( a1 ) / sizeof(a1[0])=%d\n",c2 = sizeof( a1 ) / sizeof( a1[0] ));
        //printf("c4=sizeof( a2 )=%d\n",foo4(a2));
       
        /******************************Structure*****************************************************/
        struct S1
        {
                char c;
                int i;
        };
        printf("sizeof(S1)=%d\n",sizeof(struct S1));
        struct S2
        {
                int i;
                char c;
        };
        printf("sizeof(S2)=%d\n",sizeof(struct S2));
        struct S3
        {
                char c1;
                S1 s;
                char c2;
        };
        printf("sizeof(S3)=%d\n",sizeof(struct S3));
       
        /*************************************Bit field****************************************************/
        struct BF1
        {
                char f1 : 3;
                char f2 : 4;
                char f3 : 5;
        };
        printf("sizeof(BF1)=%d\n",sizeof(struct BF1));
        union U
        {
                int i;
                char c;
                S1 s;
        };
        printf("sizeof(U)=%d\n",sizeof(U));
        return 0;
}

Welcome to give some constructive suggestions!
Thank you!

xpi0t0s 1Jan2011 18:23

Re: Some usage of sizeof
 
Please use code tags when posting code. It preserves the formatting.

Also please read the posting guidelines.

shabbir 2Jan2011 10:13

Re: Some usage of sizeof
 
Quote:

Originally Posted by xpi0t0s (Post 76713)
Please use code tags when posting code. It preserves the formatting.

Also please read the posting guidelines.

As of Now I have done it for you and so read the link in the reasons about what is code blocks and how you can use them


All times are GMT +5.5. The time now is 11:51.