"C Language" learning note 5--Hands (1)

Source: Internet
Author: User
Tags function definition function prototype

1. Pointers: A way to use addresses in symbolic form.

Because the computer's hardware instructions are very dependent on the address, a program that uses pointers is more efficient. In particular, pointers can effectively handle arrays, and array-to-ground notation actually uses pointers in a disguised manner.

Example: The array name is the address of the first element of the array. In other words, if Flizny is an array, the following statement is established

Flizny = = &flizny[0]

Both Flizny and &flizny represent the address of the first element of the array (& is the address operator). Both are constants that do not change during program operation, but you can assign them to pointer variables, and then you can modify the pointer variables.

//Pointer Address#include<stdio.h>#defineSIZE 4intMain () { ShortDates[size]; //declaring a pointer variable of type short     Short*PTI;  Shortindex; DoubleBills[size]; Double*PTF; PTI=dates; PTF=bills; printf ("%23s%15s\n"," Short","Double");  for(index =0; Index < SIZE; index++) printf ("pointers +%d:%10p%10p\n", index, PTI + index, PTF +index); return 0; } /*output:short doublepointers + 0:000000000062fe30 000000000062fe10pointers + 1:000000000 062fe32 000000000062fe18pointers + 2:000000000062fe34 000000000062fe20pointers + 3:000000000062fe36 000000000062FE28 /c2>*/

I do not know why, I used to go to school to look at the hands of the book, and now look at the pointer is suddenly very simple. Perhaps now the code is much more, it is easy to understand the variables, values, variable names, address of the relationship.

So now it seems that the school to open C language as a computer language introductory subject is really debatable, admittedly c is the first high-level language, learned C language will be easy to learn other computer language. But learning a simple, crude computer language like Java/python can make it easy for students to understand how the program is written. And then go back to the bottom of the principle it will be much easier.

I think the pointer can be interpreted as a variable. The size of this variable depends on the address size of the computer (my computer is 64 bits, so a pointer size is 8 bytes). For example in the above program

// declares a pointer of type short, with a pointer increment of sizeof (short) = 2, which means that PTI + 1 corresponds to the address that PTI represents + 2  Short * PTI; // declares a pointer of type double, with a pointer increment of sizeof (double) = 4, meaning that PTL + 1 indicates the address that PTI points to + 4 Double * PTF;

Look at the above program results are also easy to see, and then read the picture is also in line with the

2. Functions, arrays, and pointers

Because the array name is the address of the first element of the array, the array name required as the actual parameter is a pointer to the form parameter that matches it. Only in this case will C interpret int ar[] and int * AR as the same. In other words, AR is a pointer to int.

Since the function prototype can omit the parameter name, the following 4 prototypes are equivalent.

int sum (intint  n); int sum (intint); int sum (intint  n)int sum (intint);

However, parameter names cannot be omitted from function definitions, and the following two forms of function definitions are equivalent:

int sum (intint  n) {  // function contents omitted   }int SUM (intint// function contents omitted }

Look at the following example program, the size of marbles is bytes, indicating that marbles represents an array, and the size of AR is 8 bytes, indicating that the function receives an address ();

//The sum of the elements of an array#include<stdio.h>#defineSIZE 10//declaring function PrototypesintSumintAr[],intn);intMain () {intMarbles[size] = { the, at, -, the, -, A, the, -, -, *}; Longanswer; Answer=sum (marbles, SIZE); printf ("The sum of all elements of marbles is%1d.\n", answer); printf ("the size of the marbles is%u bytes.\n",sizeofmarbles); return 0; } intSumintAr[],intN) {inti; intTotal =0;  for(i =0; I < n; i++) { Total+=Ar[i]; } printf ("the size of AR is%u bytes.\n",sizeofar); returnTotal ;} /*the size of the Output:ar is 8 bytes.marbles and the sum of all the elements is the size of 241.marbles bytes.*/

3. Using pointer parameters

/*using pointer parameters*/#include<stdio.h>#defineSIZE 10intSump (int* Start,int*end);intMain () {intMarbles[size] = { the, at, -, the, -, A, the, -, -, *}; Longanswer; Answer= Sump (marbles, marbles +SIZE); printf ("The total number of marbles is%1d.\n", answer); return 0; } //using pointer arithmetic intSump (int* Start,int*end) {     intTotal ;  while(Start < end)//Cross Judgment{ Total+ = * START;//accumulate the value of an array elementstart++;//to point the pointer to the next element     }          returnTotal ;} /*Output:the total number of marbles is 241.*/

4. Pointer operation

A. Assignment: You can assign an address to a pointer.

B. dereference: The * operator gives the value stored on the address indicated by the pointer.

C. Access: Like all variables, pointers also have their own addresses and values, or you can use the FETCH operator & to get the address of the pointer itself

D. The pointer is added to the integer: You can use the + operator to add pointers to integers, or to add integers to pointers. In either case, the integer is multiplied by the size of the type pointed to by the pointer (in bytes), and the result is added to the initial address.

E. Pointer increment (decrement): increments (decrements) A pointer to an array so that the pointer points to the bottom (top) element of the array.

F. Pointer minus an integer: The result of subtracting an integer from the initial address multiplied by the size of the type pointed to by the pointer (in bytes).

G. Pointer differential: You can calculate the difference of two pointers. The difference of the address pointed to by two pointers/the size of the type pointed to by the pointer

H. Comparison: Use a relational operator to compare the values of two pointers, provided that two pointers point to objects of the same type

//pointer Manipulation#include<stdio.h>intMain () {inturn[5] = { -, $, -, -, -}; int* PTR1, * ptr2, *PTR3; PTR1=urn; PTR2= &urn[2]; printf ("pointer value, dereferenced pointer, pointer address:\n"); printf ("ptr1 =%p, *ptr1 =%d, &ptr1 =%p\n", PTR1, *PTR1, &ptr1); PTR3= Ptr1 +4;//pointer additionprintf"\ n Adding an int to a pointer:\n"); printf ("ptr1 + 4 =%p, * (PTR1 + 4) =%d\n", PTR1 +4, * (PTR1 +4)); PTR1++;//Increment pointerprintf"\ n values after ptr1++:\n"); printf ("ptr1 =%p, *ptr1 =%d, &ptr1 =%p\n", PTR1, *PTR1, &ptr1); PTR2--;//Decrement Pointerprintf"\ n values after ptr2--:\n"); printf ("ptr2 =%p, *ptr2 =%d, &ptr2 =%p\n", PTR2, *PTR2, &ptr2); //Recovery--ptr1; ++ptr2; printf ("\ Pointers Reset to original values:\n"); printf ("ptr1 =%p, ptr2 =%p\n", PTR1, PTR2); //one pointer minus another pointerprintf"\ Subtracting one pointer from another:\n"); printf ("ptr2 =%p, ptr1 =%p, ptr2-ptr1 =%1d\n", PTR2, PTR1, PTR2-ptr1); //A pointer minus an integerprintf"\ n subtracting an int from a pointer:\n"); printf ("PTR3 =%p, ptr3-2 =%p\n", PTR3, PTR3-2); return 0; }  /*output:pointer value, dereferenced pointer, pointer address:ptr1 = 000000000062fe30, *ptr1 = +, &ptr1 = 00000000 0062fe28 adding an int to a POINTER:PTR1 + 4 = 000000000062fe40, * (PTR1 + 4) = All values after ptr1++:p TR1 = 000000000062 FE34, *PTR1 = $, &ptr1 = 000000000062fe28 values after ptr2--:p TR2 = 000000000062fe34, *ptr2 = $, &ptr2 = 0000 00000062FE20 pointers Reset to original VALUES:PTR1 = 000000000062fe30, ptr2 = 000000000062fe38 subtracting One pointer fr Om another:ptr2 = 000000000062fe38, ptr1 = 000000000062fe30, ptr2-ptr1 = 2 subtracting an int from a POINTER:PTR3 = 0000 00000062fe40, ptr3-2 = 000000000062fe38*/ 

5. Do not dereference the uninitialized pointer:

  

int * PT;   // Uninitialized Pointer 5;   // a serious error. Because PT hasn't been initialized yet, we don't know who the PT is pointing to, the dereference doesn't know who to assign 5 to

6. Protect data in an array (passed by value or by address)

A. Value passing and address passing in functions: when writing a function that handles a primitive type (such as int), whether to pass a value of type int or to pass a pointer to type int. It is usually passed directly, and the pointer is passed only if the program needs to change the value in the function. For a number of groups without a choice, pointers must be passed because it is efficient to do so. If you pass an array by value by a function, you must allocate enough space to store a copy of the original array, and then copy all the data from the array to the new array. If you pass the address of an array to a function, it is much more efficient to have the function handle the original array directly.

B. Delivery of addresses can cause problems (unexpected changes to values). C usually passes data by value, because it guarantees the integrity of the data.

7. Use const for formal parameters: if the function's intent is not to modify the contents of the data in the array, you should use the keyword const when declaring the formal parameter in the function prototype and function definition

   

  

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.