C language (pointer)

Source: Internet
Author: User
Tags bitwise

Main.m

1-22 class Notes

Lecturer: Xiao Hui

Author: Wang Xuewen

Created by Lanouhn on 15/1/22.

Copyright (c) 2015 Lanouhn. All rights reserved.

Pointer (pointer)

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {

/*

Pointers: Address numbers (similar to house number)

int a = 10;

Use & (address symbol) to get the first address number of the memory allocated by the variable

printf ("%p\n", &a);

Pointer variable: The variable used to store the address number

Defining formats

Data type + variable name = initial value;

Note: The data type determines *p the content is, how many bytes are taken

Shaping pointer variables

When defining a pointer variable, * tells the compiler that this is a pointer variable

int *p = NULL;

printf ("%p\n", p); 0x0

p = & A;

Direct access: Get in-memory data through variable a

printf ("%d\n", a);

Indirect access: Get in-memory data by pointer p

Find memory by memory address number, and take out in-memory data with *

printf ("%d\n", *p);

*/

When the pointer variable is not re-assigned, it means that the pointer is redirected

int a = ten, B = 5;

int *P1 = &a;

int *P2 = &b;

P1 = p2;

printf ("%d\n", *P1);

int a = N, b = 10;

int * x = &a;

int * y = &b;

*x = 11;

int *p = &a;

p = y;

*y = 8;

printf ("A =%d, B =%d\n", A, b);

int a = 4, B = 8, c = 10;

int * x = &a;

x = &c;

int *y = &b;

int *z = &c;

*z = 5;

*x = 12;

x = y;

*y = *z + *x;

printf ("A:%d, B:%d, C:%d\n", A, B, c);

Memory address number is lower than the storage unit has operating system control, does not allow access

int *p =null;

*p = 100; Error

2. Wild pointers, defining pointer variables without assigning an initial value

int *p;

*p = 5;

printf ("%d\n", *p);

3. When the pointer variable is defined, * How to write

int *P1 = null;//recommended

int* P2 = NULL;

INT*P3 = NULL;

int * P4 = NULL;

The number of bytes that the pointer variable occupies

1. Regardless of data type

2. Related to the mantissa of the operating system, 32-bit under pointer variable station 4 byte, 64-bit under pointer variable 8 bytes

/*

printf ("%lu\n", sizeof (char *));

printf ("%lu\n", sizeof (short));

printf ("%lu\n", sizeof (int *));

printf ("%lu\n", sizeof (long));

printf ("%lu\n", sizeof (float *));

int a = 130;

Char *p = &a;

printf ("%d\n", *p);

Bit arithmetic

&: Bitwise AND

The same 1 is 1, otherwise 0

printf ("%d\n", 9 & 5);

// | : Bitwise OR

The same 0 is 0, otherwise 1

printf ("%d\n", 9 | 5);

^ Bitwise XOR OR

The difference is 1, the same is 0

printf ("%d\n", 9 ^ 5);

~: Take Back

1 change to 0, 0 to 1

printf ("%d\n");

<<: Move Left

Move left, high drop, status 0

printf ("%d\n", 3 << 4);

Move right

Move right, sign bit also move, positive high 0, negative high 1;

printf ("%d\n", >> 3);

NOTE: bitwise-AND, bitwise-OR, bitwise-XOR or requires the number of participating operations to appear in the complement form;

Original code: is itself

Anti-code:

1. The inverse of a positive number is its own

2. The inverse of a negative number is the sign bit invariant, and the value part takes the inverse

Complement:

Data is stored in the form of complement in memory

1. A positive complement is his own

2. Complement of negative number: sign bit unchanged, value part inverse plus 1

Arrays are stored continuously in memory.

int array[5] = {1, 2, 3, 4, 5};

for (int i = 0; i < 5; i++) {

printf ("%p\n", &array[i]);

}

The array name is actually the first address of the array.

Array constant equals & Array[0]

printf ("%p\n", array);

printf ("%p\n", &array[0]);

int *p = array;

printf ("%d\n", array[1]);

printf ("%d\n", p[1]);

printf ("%d\n", * (P + 1));

printf ("%d\n", * (array + 1));

printf ("%d\n", * (1 + array));

printf ("%d\n", 1[array]);

Pointer arithmetic

The data type of the data variable also determines the pointer to add 1 operations to jump a few bytes

int *p1 = NULL;

printf ("%p\n", p1);

printf ("%p\n", p1 + 1);

int array[5] = {1, 2, 3, 4, 5};

int *p = array;

* (p++) = 3;

*p = 5;

* (p + 1) = 8;

for (int i = 0; i < 5; i++) {

printf ("%d", array[i]);

}

printf ("\ n");

P + 1

p++ or ++p

Same: Takes the first address of the next element

Different: ++p or p++, will let the pointer redirect

int array[5] = {1, 2, 3, 4, 5};

for (int i = 0; i < 5; i++) {

printf ("%d", array[i]);

printf ("%d", * (array + i));

} printf ("\ n");

Array with 10 elements, random assignment, value range [22, 33]

int arr[10] = {0};

for (int i = 0; i < i++) {

* (arr + i) = Arc4random ()% 12 + 22;

printf ("%d", * (arr + i));

} printf ("\ n");

BOOL flag = YES;

for (int i = 0; i < 10-1 && flag; i++) {

flag = NO;

for (int j = 0; J < 10-1-I; j + +) {

if ((* (arr + j) > * (arr+ J + 1)) {

int temp = * (arr + j);

* (arr + j) = * (arr + j + 1);

* (arr + j + 1) = temp;

flag = YES;

}

}

}

for (int i = 0; i < i++) {

printf ("%d", * (arr + i));

}

printf ("\ n");

*/

int array[10] = {0};

int *p = array;

The difference between the name of the array (the first address of the array) and the pointer variable pointing to the first address of the array

1. Array name (array header address) is a constant address and cannot be modified

int a = 5;

Array = &a;

2. sizeof (), if it is an array name (the first address of the array), computes the number of bytes that the array occupies; If it is a pointer variable, the number of bytes that the pointer variable occupies is calculated

printf ("%lu\n", sizeof (array));

printf ("%lu\n", sizeof (p));

Pointers and strings

Character array

String

Array of strings

Char string[] = "iphone";

printf ("%s\n", string);

printf ("%s\n", string + 3);

Char string[] = "I love u!";

* (string + 1) = ' + ';

* (string + 7) = ' + ';

printf ("%s\n", string);

printf ("%s\n", string + 3);

printf ("%s\n", string + 7);

printf ("%s\n", string + 8);

Char string[] = "IPhone";

* (string + 1) = ' P ';

Char str[] = "HAHA"; The array name is a constant address and is not sufficient to redirect

string = str;

Char *p = "IPhone";

* (p + 1) = ' P '; P points to the constant area, and the contents of the constant area cannot be modified

p = "HAHA";

printf ("%s\n", p);

return 0;

}

C language (pointer)

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.