Basic C Language Learning 07

Source: Internet
Author: User

=============================================================================
The knowledge points involved are: 1,


=============================================================================
C language is a process-oriented language, is a weak type of language, C source code is basically a myriad of functions of the stack.
That is, a lot of functions are composed of C language source code, that is, its source is basically a function of the composition.

Test () and test (void) are not the same in C language. If you do not write anything, C language is more ambiguous, easy to make mistakes, the result is unknown.
Test () and test (void) are the same in the C + + language.

C language a few loose places (insufficient places, not forbidden places, where it is prone to error).

After-school thinking:
Write a function to find the length of the string. After class, the recursive function is used to find the length of the string.

=============================================================================
Pointer

Pointers are the most abstract, most important, and most commonly used language in C.

The concept of pointers:

The pointer variable is also a variable,
What the pointer holds is an address that points to a piece of memory space,
A pointer is a data type (pointer type).
--------------------------------------
What is the smallest unit of computer memory? Byte (bytes)

For memory, each byte (byte) has a unique number, which is the memory address.

The operating system makes a number for each byte of memory, so it says: A number corresponds to a byte (byte) of space.

To make an analogy:
1-BYTE
2-BYTE
3-BYTE
4-BYTE
Corresponds to
--------------------------------------
how big is an int? A: 4 byte (bytes), so an int occupies 4 numbers (that is, 4 different memory addresses).

Address number: a 4-byte unsigned integer under a 32-bit system, and a 8-byte unsigned integer under a 64-bit system.
(because the address cannot be negative, and because the unsigned number can represent a larger address, the maximum address of the signed number will become smaller)
-----------------------------------------------------------------------------
The definition of a pointer variable:

You can define a pointer variable that points to a variable.
-----------------------------------------------------------------------------
Fetch address operator &

& can get the address of a variable in memory. (The address taken is the memory address)

Register int a;//Register variable, this variable is not inside the memory, but in the CPU, so there is no address,
So the register variable cannot use & to get the address.
-----------------------------------------------------------------------------
No type pointer

Defines a pointer variable, but does not specify what type of data it points to. You can convert void * to other type pointers by forcing conversions.
Other types can also be coerced into void type pointers with (void *).

void *p; The assignment between pointers requires the same type, but any type of pointer can be assigned a value of void *.
-----------------------------------------------------------------------------

1 The example code for Linux is as follows:2 3 intMain ()4 {5     int*p;//defines a pointer variable that can point to an int type address, and the pointer variable is named P. * is not part of the variable name. 6             //int * is a data type. 7     intA//a variable of type int is defined, and the int variable is named a. 8 9A =1;//int * and int are two different kinds of data. Tenp = &a;//Assign the memory address of A to P.  One  Aprintf"%p\n", p);//The 0X7FFF5B2FAEDC output is the number of the first address of a, and will not output four numbers.  -                         //and note: Each time the code executes, the output number will change!  -  the*p =Ten;//The value of a is accessed indirectly through a pointer variable, *P represents the pointer to the value of the variable, and p represents the address that points to the variable.  -printf"A =%d\n", a);//a = 10; The value of a is changed by the method above.  -  -A = -; +printf"%d\n", *p);//100 indirectly accesses the value of a through a pointer variable.  -  +     intb =2; Ap = &b;//the memory address of B is also assigned to P.  at*p = -; -printf"B =%d\n", b);// - -  -     //char *p1 = &a; //equivalent to char *p1; p1 = &a;//two different types of addresses. That is, the pointer type is incompatible. Then we'll try to be strong!  -     Char*P1 = (Char*) &A; -A =123456; in*P1 =0; -printf"A =%d\n", a);//A = 123392 problems can occur even after a strong turn, so avoid pointer-type incompatibility issues.  to  +     void*P2;//can point to any type of address, and void represents an untyped type.  -  the     return 0; *}

The code for Linux is as follows:

-----------------------------------------------------------------------------

Description of the pointer consuming memory

In the same system, the size of the address (or the size of the number) is always the same regardless of what type of variable the pointer is pointing to.

1 The example code for Linux is as follows:2 3 intMain ()4 {5     Char*P1;6     int*P2;7     Long Long*P3;8 9printf"%lu,%lu,%lu\n",sizeof(p1),sizeof(p2),sizeof(p3));//The essence is: What is the size of the number? Ten     return 0;//the output is 8, 8, 8 One                 //Address Number: A 4-byte unsigned integer under a 32-bit system, and a 8-byte unsigned integer under a 64-bit system.  A                 //pointer variables are named P1, P2, p3. How big is the pointer variable size? Because the pointer variable corresponds to the number of the first address of XXX, -                 //that is, the pointer variable corresponds to the number, and the number is the memory address. That is, the number is a 8-byte unsigned integer under a 64-bit system.  -                 //so the size of the pointer variable is the size of the number, and the number is represented by a 8-byte unsigned integer in the 64-bit system.  the                 //For example: the same hotel, the length of the room number is the same.  -  -}

The code for Linux is as follows:

Basic C Language Learning 07

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.