C--(11) Pointers to arrays

Source: Internet
Author: User

Knowledge Points:
• Pointers and variables (pointers to variables)
• Pointers and functions (address passing)
• Pointers and arrays (pointers to arrays)
• Pointers and strings

=================================
Pointers to arrays

1. What is an array pointer
1) the pointer to an array is the starting position of the exponential group in memory
2) The first element of the array and the starting address of the group are consistent

2. the nature of the array name
1) The array name is essentially an address constant, representing the first address of the array, which is the address of the first element.

The array name represents the starting address of the array, does not represent the entire array, and cannot be assigned a whole set of values.

3. The array name is a pointer constant and can be used as a pointer to
The corresponding element address can be calculated with the starting address and subscript

4. Another way to access an array
eg
1) Recall the subscript of an array to access a specific element
2) How to print the specified element by pointer
3. Exchanging values for the first and last elements
Practice:
1. Iterate through all the elements in a loop
2. Assigning values to all elements via scanf

==============================
Pointer variable to array

1. What is a pointer to an array
Pointer variable to save array address
2. How to declare
1) Pointer to one-dimensional array

3. Pointer variable nature to an array
1> essence is still a variable that holds a pointer to a variable, but now holds the first address of an array

4. How to manipulate an array by pointer variables to an array
1) How to get elements
Two ways of 1>: Subscript method, pointer operation method
Subscript method: The same as array usage, with emphasis on understanding pointers and arrays of operations similar to
Pointer arithmetic: By the offset of the first address

5. Pointer arithmetic
Pointer variables can perform +-*/arithmetic just like normal variables, and +-indicates that the point is moving left and right, */meaningless.

1). Add and subtract actions for pointer variables
Thinking: How much is added after the pointer +1
Exercise: Declaring a pointer to char with an address after output +1

2). Think: * (p+1) and *p+1 the difference

3). The relation operation of the pointer

4). Think:
The arithmetic operation of pointers to arrays makes sense. Does the individual pointer operation make sense?

5). The difference between the array name and the pointer variable
The name of the 1> array is the first address of the array, and once the array is established, its address is fixed.
The 2> pointer variable is a variable that has memory space and can be assigned repeatedly

Array pointers can be traversed by using the array pointer + +--offset pointer variables pointing to an array

Practice:
1. Complete the traversal of the elements via pointers to the array (two ways) 5
2. Assigning all elements to the operation via scanf
3. Computes the and of all elements of an array by pointers to the array

6. Role
1) pointers to arrays typically appear in function arguments, which are used to represent the receiving array real parameters

==============================
Array parameters
Implement a function module that iterates through an array

1. How array parameters are represented
sizeof (a)/sizeof (A[0])

1) Normal array form func (int a[5]); Func (int a[]);
2) The form of the pointer func (int *a)

2. Think: Can you directly within the function to find the size of the array, if not, please explain
Func (int a[5]);


3. The array is passed as a parameter
1) Nature
When an array is passed as an argument, it is delivered as an address
Thinking: What does address delivery mean?
2) cause
Reduce useless copies of data
3) complete list of parameters
1> the type of array to receive
2> the length of the received array, which must be passed by the caller
Practice:
1. Design a function to fill the contents of an array into another array
void Copy_arr (int *a1, int *a2, int len); 5
The following exercises all operate as pointers


============================
Pointers and strings
1> How to refer to a string by a pointer variable
2> the characteristics of this type of citation

1. Pointers to character arrays
1) point to a character array
Exercise: The following exercises are done with pointers
1> Write a function that uses pointers to change uppercase in a string to lowercase
2> Write a function that uses pointers to count the number of characters

2. The string that the pointer points to

2) String Constants
1> can directly modify the contents of an element
2> the modified result
3> essence: A pointer directly refers to a string as a constant, called a string constant

C--(11) Pointers to arrays

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.