C language pointer and array knowledge Summary (I)

Source: Internet
Author: User

C language pointer and array knowledge Summary (I) I. Basics of pointers 1. in C, the value of a variable can be changed through a pointer, and the statement symbol for printing the pointer can be: % 08x 2. the essence of a pointer is a variable. Since it is a variable, the address will be allocated. the pointer only stores the address. 3. the difference between passing an address and passing a value function transfer a parameter is only a value assignment/copy function, so there is a difference between passing a function value and passing an address: value transfer cannot change the variable outside the function body, address Transfer can change the external variables of the function body. 4. in what scenarios do we need address transfer? Note: the first is to modify the external variables of the function body. The second is to transfer the complex function data type (which greatly improves the execution efficiency of the C language. # define led (m) m? (N = 1) :( n = 0) 6. note the usage of const: The difference between int const * p and int * const p 2. The basis of array 1. an array is an ordered set of the same type of variables. 2. the array name represents the address of the first element of the array. 3. the address of the array can be obtained only by using the address symbol. 4. the address value of the element at the beginning of the array is the same as that of the array. 5. the address of the first element of the array and the address of the array are two different concepts. 6. an important difference between arrays and pointers [cpp] char * p = "abcd" in character 1.C; extern char * p [] in character 2.c is the printed Address if it is printed directly. please analyze it carefully. 3. Differences between pointers and arrays 1. the comparison between accessing array elements in Pointer form and accessing array elements in the form of an array table below shows that accessing array elements in Pointer form is faster than accessing array elements in the following way. an example of pointer knowledge detection (Motorola interview) [html] # include <stdio. h> int main () {Int a [] = {1, 2, 3, 4, 5}; int * p1 = (int *) (a + 1); int * p2 = (int *) (& a + 1); int * p3 = (int *) (int) a + 1 ); printf ("% d \ n", p1 [3], p2 [-1], p3 [0]);} print the result as follows: [html] 33554432 details can be analyzed by yourself. 3. program instance detected by a processor size [html] # include <stdio. h> int main () {int a = 0x1234; char * p1 = (char *) (int) & a); char * p2 = (char *) (int) & a + 1); printf ("p1 = % 08x, * p1 = % x !! \ N ", p1, * p1); printf (" p2 = % 08x, * p2 = % x !! \ N ", p2, * p2); if (* p1) = 0x34 & (* p2) = 0x12) printf ("this machine is Little edian !! \ N "); else printf (" this machine is Big edian !! \ N ") ;}print the following results: [html] p1 = bf84f814, * p1 = 34 !! P2 = bf84f815, * p2 = 12 !! This machine is Little edian !! The result is clear. 4. pointer operation formula pointer operation can be calculated using the following formula: [html] p + n = (unsigned int) p + n * sizeof (* p); we can see from this formula. for array a, although a and & a are equal, their meanings must be different. 5. the length of a string refers to the number of characters before the end of the first '\ 0'. 3. A typical interview question: how to calculate the length of a string in one sentence [html] # include <stdio. h> # include <assert. h> int strlen (char * s) {return (assert (s), (* s? (Strlen (s + 1) + 1): 0);} int main () {printf ("% d \ n", strlen ("ddwed ")); return 0 ;}

Related Article

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.