C language-06 complex data types-03 pointer,-06 data type-03 pointer

Source: Internet
Author: User

C language-06 complex data types-03 pointer,-06 data type-03 pointer
Pointer variable definition

Variable type * variable name;

# Include <stdio. h> int main () {// pointer: You can access the corresponding bucket based on an address value. // int before the pointer Variable p: pointer Variable p can only point to int type data int * p; // defines a pointer Variable p
Int a = 90; p = & a; // the pointer Variable p points to the variable a. the pointer variable can only store addresses.
* P = 10; // * different from * when defining pointer variables, it indicates that the corresponding bucket a = 20 is accessed; printf ("% d \ n ", * p); return 0 ;}
Pointer-related
/* % D int % f float \ double % ld long % lld long % c char % s string % zd unsigned long */# include <stdio. h> int main () {char * cp; int * ap; long * bp; printf ("cp = % zd, ap = % zd, bp = % zd \ n ", sizeof (cp), sizeof (ap), sizeof (bp ));
// Any pointer occupies 8 bytes of storage space 64-bit compiler think about why the pointer should be categorized (the computer determines the number of bytes of content retrieved from the memory based on the pointer type)}

 

Exercise
// Modify the external variable # include <stdio. h> void change (int * p); int main () {int a = 10; change (& a); printf ("a = % d \ n", ); return 0;} void change (int * p) {* p = 20 ;}

 

// Use a pointer to indirectly return multiple values. # include <stdio. h> int main () {int a = 15; int B = 10; int he = 0, cha = 0; test (a, B, & he, & cha ); printf ("and % d, difference is % d", he, cha);} void test (int a, int B, int * c, int * d) {* c = a + B; * d = a-B ;}
Pointer and array
# Include <stdio. h>/* 1. array Element access method int ages [5]; int * p; p = ages; 1> array name [subscript] ages [I] 2> pointer variable name [subscript] p [I] 3> * (p + I) // The pointer's + 1 is not a value plus 2. pointer variable + 1. How much the address value is added depends on the pointer type int * 4 char * 1 double * 8 */void change (int array []); int main () {// 20 bytes int ages [5] = {10, 11, 19, 78, 67}; change (ages); return 0 ;} // use a pointer to receive an array. the pointer variable array points to the first element of the array void change (int * array) {printf ("% d \ n ", array [2]); // printf ("% d \ n", * (array + 2);}/* void change (int array []) {int s = sizeof (array); printf ("% d \ n", s);} */void test () {double d = 10.8; double * dp; dp = & d; printf ("dp = % p \ n", dp); printf ("dp + 1 = % p \ n", dp + 1 ); int ages [5] = {10, 9, 8, 67, 56}; int * p; // The pointer Variable p points to the first element of the array p = & ages [0]; // The array name is the address of the array and the address of the first element of the array // p = ages; /* p ---> & ages [0] p + 1 ---> & ages [1] p + 2 ---> & ages [2] p + I ---> & ages [I] * // printf ("% d \ n ", * (p + 2); printf ("% d \ n", p [2]);/* for (int I = 0; I <5; I ++) {printf ("ages [% d] = % d \ n", I, * (p + I);} * // printf ("% p \ n ", p); // printf ("% p \ n", p + 1); // printf ("% p \ n", p + 2 );}
Pointer and string
# Include <stdio. h>/* 1. the constant area stores some constant strings. heap object 3. stack stores local variables: Two Methods for defining strings 1> using the array char name [] = "itcast"; // string variables * features: the characters in the string can be modified * usage: the content of the string needs to be modified frequently // char name [] = "it"; // name [0] = 'T '; // printf ("% s \ n", name ); // "it" = 'I' + 'T' + '\ 0' 2> using the pointer char * name = "itcast"; // String constant * features: A string is actually a constant string, and the characters in it cannot be modified * usage: the content of the string does not need to be modified, and this string is often used and cached, the newly defined pointer is out of memory space. // char * name2 = "it"; // char * name3 = "it"; // * name2 = 'T '; // The program will crash. It is a constant that cannot be modified // printf ("% c \ n", * name2 ); // The pointer variable name2 points to the string's first character printf ("% p \ n", name2, name3 ); // do not open the new space with the same address // printf ("% s \ n", name2); // until \ 0 */int main () {char name [20] is encountered; printf ("Enter name: \ n"); scanf ("% s", name ); // 'J' 'a 'C' k'' \ 0' // printf ("% c \ n", name [3]); // printf ("the input string is % s \ n", name); return 0 ;}// define the string array void test2 () {char * name = "jack"; // int ages [5]; // pointer array (String Array) char * names [5] = {"jack ", "rose", "jake"}; // two-dimensional character array (String Array) char names2 [2] [10] = {"jack", "rose "};}

 

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.