Sizeof ()

Source: Internet
Author: User

I have been preparing for an interview recently, and I still have to make up on the basics. Sizeof (type) is a type of frequently encountered pen questions, in order to be able to remain unchanged, or to clarify the principle of sizeof the most convenient.

Note the following principles:
1. sizeof () is not a function, but an operand, that isKeywords(Most people are surprised at this point );
2. sizeof () returns the actual size of struct, arrays, and variables. However, for pointers, only the size of memory occupied by pointers can be returned, not the size of memory pointed to by pointers, that is, the size of the dynamically allocated memory space cannot be returned;

Exercise questions to consolidate what you have learned:

 1 # Include <stdlib. h>
2 # Include <stdio. h>
3
4 Char * String = " Hello " ;
5 Int VaR = 100 ;
6 Int * Pint = NULL;
7 Int ** Ppint = NULL;
8 Int Array [ 100 ];
9
10 Int Array_func ( Char Para [ 100 ])
11 {
12 Return Sizeof (Para );
13 }
14
15 Int Malloc_func ( Void )
16 {
17 Int * P = NULL;
18 Return Sizeof (P = malloc ( 100 ));
19 }
20
21 Int Main ( Int Argc, Char * Argv [])
22 {
23 Char Para [ 100 ];
24
25 Printf ( " String size: % d \ n " , Sizeof ( String ));
26 Printf ( " VaR size: % d \ n " , Sizeof ( VaR ));
27 Printf ( " Pint size: % d \ n " , Sizeof (Pint ));
28 Printf ( " Ppint size: % d \ n " , Sizeof (Ppint ));
29 Printf ( " Array size: % d \ n " , Sizeof (Array ));
30
31 Printf ( " Array func size: % d \ n " , Array_func (para ));
32 Printf ( " Malloc func size: % d \ n " , Malloc_func ());
33
34 Return 1 ;
35 }

What is the running result? Think about it and then answer:>

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.