C Language Learning notes-8. Pointers

Source: Internet
Author: User

First, what is a pointer

1. A pointer is a variable that holds the memory address of another variable

Pointer-type variable is 4 bytes long, 32 bits

2. The macro defines null, which represents 0

Good programming habits: Set the value to NULL when the pointer is not being used temporarily

3. If the pointer is undefined, the value is 0xCCCCCCCC (in Visual Studio) if it is not initialized

If this address is accessed, the program crashes

4. Pointer action: Modify the real parametric value in the main function

Example: void get (int *input) {

scanf ("%d", input); Note: cannot be written as &input

}

Second, pointers and arrays

1. The addition and subtraction of pointers is measured in data elements, not bytes

* (A + K) equivalent to A[k] &a[k] equivalent to a + K

2. The difference between a pointer and an array: the array has storage space and can store the data

Pointers can only be used to save addresses, no storage space

3. Dynamic array: Example: Scores = (int *) malloc (num * sizeof (int));

if (Scores = = NULL)

Goto Error;

Note: The malloc function returns a null pointer of type void, so the type conversion must be enforced

Need to add error handling

Use array subscript and pointer methods to access

Static arrays are stored in the stack frame, and dynamic arrays are stored in the heap

4. Use the free (...) after using the dynamic array; Release

5. Array of pointers: example: void main (int argc, char *argv[])

ARGC: Holds the number of arguments (including program name), and the element in argv is an array of characters of the base type

6. Pointers pointing to pointers

Example: void get (int **array) {

*array = (int *) malloc (count * sizeof (int));

...}

The address in the keynote function is passed to the called function, and the parameters and arguments are both pointers

The address in the called function is passed to the keynote function, and both the formal parameter and the argument are pointers to pointers (or function return values)

7. Two-d arrays and pointers

When a two-dimensional array is stored in the C language, it does not hold a two-dimensional table, but is stored sequentially, that is, one after another to store

A[I][J]: Corresponding address: A + k * sizeof (int), k = i * Number of elements per line + j

A[i]: One-dimensional array, example: a[0] + 1 point to a[0][1]

A: Point to a fixed-length, one-dimensional array, with each step spanning the length of the array, e.g. A + 1 pointing to a[1]

8. If you call A[i][j]:

Operation on one line: Test1 (A[i]); Test1 parameters: int *pa or int pa[] or int pa[4]

Array operations: Test2 (a); Test2 parameters: Int (*arr) [4] or int arr[][4] or int arr[3][4]

9. Dynamic two-dimensional array

Example: a = (int (*) [4]) malloc (nRows * nCols * sizeof (int));

Third, pointers and strings

1. The ASCII code of the character ' + ' is 0

Note: When defining a character array, you must reserve the space for an array element for '% '

2. If you are pointing to a string constant, the pointer holds the first address of the string

3. The data segment is where the compiler holds string constants

Programmers do not need to apply or release it, and string constants are generally read-only

4. String access: Entire string: str or P (pointer)

A character: Str[i] or * (P + i)

C Language Learning notes-8. Pointers

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.