当前位置:网站首页>C primer plus learning notes - 6. Arrays and pointers
C primer plus learning notes - 6. Arrays and pointers
2022-07-24 05:01:00 【Charles Ren】
List of articles
summary
Array and pointer are closely related , So this book puts them into a chapter to learn . Let's take a look .
Array
We usually call a variable that stores only one value a scalar scalar, Those that store multiple values are called vector
Initialization mode
int fix = 1; // Scalar
int power[8] = {
1, 2, 3, 4, 5, 6, 7, 8};//vector
const int power2[8] = {
1, 2, 3, 4, 5, 6, 7, 8};// Read only array , The content cannot be modified
int power[] = {
1, 2, 3, 4, 5, 6, 7};// Use initialization list , There is no need to specify the number of elements
int power[6] = {
[5]=22}; // Partial initialization , The remaining values are initialized to 0,(C99)
Use uninitialized arrays
#include <stdio.h>
#define SIZE 4
int main(void)
{
int no_data[SIZE]; // Declare only uninitialized arrays
int i;
printf("%2s%14s\n",
"i", "no_data[i]");
for (i = 0; i < SIZE; i++)
printf("%2d%14d\n", i, no_data[i]); // Random values are printed
return 0;
}
Use an array of uninitialized values , The existing garbage value on the memory will be printed .
Use partially initialized arrays
#include <stdio.h>
#define SIZE 4
int main(void)
{
int no_data[SIZE]={
3,5}; // Initialize partial values
int i;
printf("%2s%14s\n",
"i", "no_data[i]");
for (i = 0; i < SIZE; i++)
printf("%2d%14d\n", i, no_data[i]); // Only the first two numbers are printed and entered , Later, it will be assigned as 0
return 0;
}
If partially initialized , The remaining value is initialized to 0.
Array assignment
int no_data[5]={
3,5,4,5,6};
int data[5];
data = no_data;// Don't allow
data[5] = {
3,4,5,7,7}; // It doesn't work , Because it has been declared , You can no longer assign values by initialization
Array boundary
The compiler does not check that array subscripts are used properly , So the array subscript is used beyond the bounds , It will cause the program to change the value of other variables , This will cause the program to terminate abnormally .
C Why not check that the array is out of bounds ?
Before the program runs , The array subscript value has not been determined , If the compiler needs to add additional code to check each subscript of the array at runtime , It will reduce the running speed of the program . Therefore, the compiler does not check for subscript out of bounds , And trust programmers .
Multidimensional arrays
float rain[5][10]; // Statement 5 That's ok 10 Array of columns , The first parameter represents the line of the element , The second represents the column .
Arrays and pointers
Array notation actually uses pointers in disguise .
The array name is the address of the first element of the array
int power[4];
power and &power[0] Both represent the address of the first element of the array .
Both are constants , Will not change . But you can assign it to a pointer variable .
#include <stdio.h>
#define SIZE 4
int main(void)
{
short dates [SIZE];
short * pti;
short index;
double bills[SIZE];
double * ptf;
// The address of the first element of the array
pti = dates;
ptf = bills;
printf("%23s %15s\n", "short", "double");
// Print poiter+index The address of
for (index = 0; index < SIZE; index ++)
printf("pointers + %d: %10p %10p\n",
index, pti + index, ptf + index);
return 0;
}

The above program prints the address of each element in the array .
The first line is to print the address of the beginning of each array , The next line is the pointer +1 After the address .
You can see C Middle pointer +1, What is added is a storage unit , That is to say +1 The address after is the address of the next element in the array , Instead of the address of the next byte .
- such as short Array , The pointer +1, Add address hexadecimal c To e That is to say 2 byte . because short Occupy 2 byte .
- such as double Array , The pointer +1, Add address hexadecimal 0 To 8 That is to say 8 byte . because double Occupy 8 byte .

dates+2 == &date[2] The address of the first element is moved back by two digits , That is, the address of the third element
So we can deduce The value of the subscript of an element It can also be expressed as arr[i] == *(arr+i)
Here is a difference :
*(dates + 2) //dates The value of the third element in the array , He and dates[2] Equivalent
*dates + 2 //dates The value of the first element in the array +2
Pointer types
therefore , When we declare a pointer , You must specify the type of pointer . Such pointer +1 What is moving is actually the storage unit of this pointer type . such as int The pointer , Every time +1, The actual movement of the pointer is 4 Bytes .
Definition of pointer
- The value of the pointer is the memory address of the object it points to .
- Use in front of the pointer * Operator can get the value of the object pointed to by the pointer .
- The pointer +1, It's his corresponding storage unit +1.
function , Array , The pointer
We want to pass an array into the function , How to do ?
It can be like this , Use array parameters
Array names as formal parameters
#include <stdio.h>
#define SIZE 10
int sum(int ar[], int n);
int main(void)
{
int marbles[SIZE] = {
20,10,5,39,4,16,19,26,31,20};
long answer;
answer = sum(marbles, SIZE);
printf("The total number of marbles is %ld.\n", answer);
printf("The size of marbles is %zd bytes.\n",
sizeof marbles);
return 0;
}
int sum(int ar[], int n) // how big an array?
{
int i;
int total = 0;
for( i = 0; i < n; i++)
total += ar[i];
printf("The size of ar is %zd bytes.\n", sizeof ar);
return total;
}
Let's put a function
Use pointers as formal parameters
#include <stdio.h>
# define SIZE 10
int sum(int ar[], int n);
int main(void)
{
int marbles[SIZE] = {
20,10,5,39,4,16,19,26,31,20};
long answer;
answer = sum(marbles, SIZE);
printf("The total number of marbles is %ld.\n", answer);
printf("The size of marbles is %zd bytes.\n",
sizeof marbles);
return 0;
}
int sum(int *arr, int n)
{
int i;
int total = 0;
for( i = 0; i < n; i++)
total += ar[i]; // It's also written as total += *(ar++);
printf("The size of ar is %zd bytes.\n", sizeof ar);
return total;
}
remember : The array name is the address of the first element of the array .
Our array marbles It's the array name , That is, the address of the first element of the array .
and sum The formal parameter to be received by the function is a int Type pointer , It's an address . Then we pass the address of the first element of the array into the function . At this time ,arr It means marbles The array name of this array , So in the function, we directly use arr[4]
So when using a function to receive an array, the following two forms declare equivalence
int sum(int ar[], int n);
int sum(int *arr, int n);
Pointer operator priority
* and ++ Operators have the same precedence , But the law of union is from left to right
#include <stdio.h>
int data[2] = {
100, 200};
int moredata[2] = {
300, 400};
int main(void)
{
int * p1, * p2, * p3;
p1 = p2 = data;
p3 = moredata;
printf(" *p1 = %d, *p2 = %d, *p3 = %d\n",
*p1 , *p2 , *p3); // 100 100 300
printf("*p1++ = %d, *++p2 = %d, (*p3)++ = %d\n",
*p1++ , *++p2 , (*p3)++); // 100 200 300
printf(" *p1 = %d, *p2 = %d, *p3 = %d\n",
*p1 , *p2 , *p3); // 200 200 301
return 0;
}
Use of the pointer
Initialization of the pointer
When the pointer is initialized ,“=” The right operand of must be the address of the data in memory , It can't be a variable .
There are two main types of methods for initializing pointers :
One is to initialize a pointer variable with an address that already exists in memory ; The second is to apply for a new memory through pointer variables and assign initial values .
One is to initialize a pointer variable with an address that already exists in memory , There are several common methods :
- The address of the variable :
int a = 10;
int * p = NULL;
p = &a;
These two sentences are equivalent to int * p = &a; - address constant
Assign a pointer constant to a pointer
Such as :long p = (long)(unsigned long)0xfffffff0; - Address of array
char ary[100];
char *cp = ary; - String constant
char *cp = “abcdefg”;
For a pointer that is not sure what type to point to , After defining it, it's best to initialize it to NULL, And check the pointer when dereferencing it , Prevent dereference of null pointers . in addition , It's a good habit to provide a legal initial value for any newly created variable in the program , It can help you avoid some unnecessary trouble .
The second is to apply for a new piece of memory through pointer variables, that is, dynamic memory allocation
- use malloc Function and free function , At this time, you need to include <stdlib.h>
int *a =(int )malloc(nsizeof(int));
…
free (a); Release the pointer here - use new/malloc And delete/free:
(1) Variable
int * p = new int(10); Indicates that the data stored in the memory pointed by the pointer is 10
…
delete p;
(2) Array
int *arr = new int[10]; Indicates that... Can be stored in the array 10 Elements
…
delete[] arr;
(3) Structure
struct test{
int a;
int b;
};
int c = 0;
test * t = new test();
c = t->a;
Using uninitialized pointers
Before using the pointer , It must be initialized with the assigned address .int *pt;
When the declaration creates a pointer , The system only allocates memory for storing the pointer itself , The memory for storing data is not allocated .
such as
int *pt;
*pt = 5; // Serious mistakes
Serious mistakes , Because this sentence means to 5 Stored in pt Point to . And then pt It only states , I haven't pointed to a position yet , So I don't know if 5 Where does it exist , The program may erase other data , Or cause the program to crash .
Pointer operation
// ptr_ops.c -- pointer operations
#include <stdio.h>
int main(void)
{
int urn[5] = {
100,200,300,400,500};
int * ptr1, * ptr2, *ptr3; // Declaration pointer
ptr1 = urn; // Assign an address to a pointer , Equivalent to ptr1 = &urn[0];
ptr2 = &urn[2]; // Assign an address to a pointer
// Dereference pointer and get address
printf("pointer value Pointer value , dereferenced pointer Dereference pointer , pointer address The address of the pointer :\n");
printf("ptr1 = %p, *ptr1 =%d, &ptr1 = %p\n",
ptr1, *ptr1, &ptr1);//ptr1 = 0x7ffd7c103ad0, *ptr1 =100, &ptr1 = 0x7ffd7c103ac8
// pointer addition Pointer addition
ptr3 = ptr1 + 4;
printf("\nadding an int to a pointer:\n");
printf("ptr1 + 4 = %p, *(ptr4 + 3) = %d\n",
ptr1 + 4, *(ptr1 + 3));//ptr1 + 4 = 0x7ffd7c103ae0, *(ptr4 + 3) = 400
// increment a pointer The pointer ++
ptr1++;
printf("\nvalues after ptr1++:\n");
printf("ptr1 = %p, *ptr1 =%d, &ptr1 = %p\n",
ptr1, *ptr1, &ptr1); //ptr1 = 0x7ffd7c103ad4, *ptr1 =200, &ptr1 = 0x7ffd7c103ac8 The pointer itself moves to the next element
// decrement a pointer The pointer --
ptr2--;
printf("\nvalues after --ptr2:\n");
printf("ptr2 = %p, *ptr2 = %d, &ptr2 = %p\n",
ptr2, *ptr2, &ptr2); //ptr2 = 0x7ffd7c103ad4, *ptr2 = 200, &ptr2 = 0x7ffd7c103ac0
--ptr1; // restore to original value
++ptr2; // restore to original value
printf("\nPointers reset to original values:\n");
printf("ptr1 = %p, ptr2 = %p\n", ptr1, ptr2); //ptr1 = 0x7ffd7c103ad0, ptr2 = 0x7ffd7c103ad8
// subtract one pointer from another Pointer minus pointer
printf("\nsubtracting one pointer from another:\n");
printf("ptr2 = %p, ptr1 = %p, ptr2 - ptr1 = %td\n",
ptr2, ptr1, ptr2 - ptr1);//ptr2 = 0x7ffd7c103ad8, ptr1 = 0x7ffd7c103ad0, ptr2 - ptr1 = 2 ( It represents the subscript position distance between elements
// subtract an integer from a pointer Subtract a number from the pointer
printf("\nsubtracting an int from a pointer:\n");
printf("ptr3 = %p, ptr3 - 2 = %p\n",
ptr3, ptr3 - 2); //ptr3 = 0x7ffd7c103ae0, ptr3 - 2 = 0x7ffd7c103ad8
return 0;
}
Use const Protection data
Functions can pass parameters in two ways , One is to pass by value , One is pointer passing . If you pass an array , Then we all need to use pointer passing . Because the value passing array function stack must have enough space to receive a copy of the original array , And it's inefficient . So we usually use pointer passing .
But there is a security risk in passing by pointer, that is, it may modify the data in the original array , But sometimes we don't want functions to modify the data in our array , To ensure the integrity of the data . We can use const keyword .
/* displays array contents */
void show_array(const double ar[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%8.3f ", ar[i]);
putchar('\n');
}
/* multiplies each array member by the same multiplier */
// If you use const modification , Then the compiler will report an error
void mult_array(double ar[], int n, double mult)
{
int i;
for (i = 0; i < n; i++)
ar[i] *= mult;
}
const Several uses of modifier
// Modify the array
const double dip[SIZE] = {
20.0, 17.66, 8.2, 15.3, 22.22};
dip[2] = 3.4; // Compiler error
double *pt = dip; // Don't allow , because dip yes const Array , Similarly, don't put const Array passed to normal parameter pointer
// Decorated pointer type : Changing the pointed value is not allowed
double rates[4] = {
20.0, 17.66, 8.2, 15.3, 22.22};
const double *pd = rates;// Point to the first address of the element
*pd = 4.5; // Don't allow
pd[2] = 4; // Don't allow
double rates2[2] = {
23,4};
pd = rates2;// allow ,pd The pointer can point to other positions
// Decorate individual pointers : The pointer cannot point to another location
double arr[4] = {
20.0, 17.66, 8.2, 15.3, 22.22};
double * const pt2 = arr;
pt2 = &arr[2]; // Don't allow , Cannot point to other places
*pt = 33.1; // allow , You can modify the value pointed to arr[0]
// Decorated pointer type + Single pointer : You cannot point to other locations or modify values
double arr2[4] = {
20.0, 17.66, 8.2, 15.3, 22.22};
const double* const pc = arr2;
pc = &arr2[2]; // Don't allow , Cannot point to other places
*pc = 33.1; // Don't allow
Pointer compatibility
The assignment between pointers is more strict than the type between values .
That is, different types of pointers cannot be assigned , Otherwise, a compilation error will be reported .
such as int and double Cannot assign values between ,int** and int* Cannot assign values to each other .

边栏推荐
- How much do you know about thread pool and its application
- Summary of common errors in wechat applet cloud development
- 排序——QuickSort
- MapReduce介绍
- Recruitment | embedded software (MCU) Engineer
- Rlib learning - [4] - algorithmconfig detailed introduction [pytoch version]
- Solution to the prompt of online account opening and transfer in stock speculation that the depository and transfer services are not activated (China Merchants Bank)
- Two methods of modifying configuration files in PHP
- 几种常见的排序
- Zhaoyi innovation gd25wdxxk6 SPI nor flash product series comes out
猜你喜欢

Uniapp learning

Journey of little black leetcode: 590. Post order traversal of n-ary tree

几种常见的排序

利用a*启发式搜索解决迷宫寻路问题

链接预测中训练集、验证集以及测试集的划分(以PyG的RandomLinkSplit为例)

MapReduce介绍

Middle aged crisis, workplace dad who dare not leave, how to face life's hesitation

Kingbase v8r6 cluster installation and deployment case - script online one click capacity reduction

Little black leetcode journey: 100 same trees
![[postgraduate entrance examination vocabulary training camp] day 10 - capital, expand, force, adapt, depand](/img/9a/a218c46806cf286f0518a72809e084.png)
[postgraduate entrance examination vocabulary training camp] day 10 - capital, expand, force, adapt, depand
随机推荐
Array force buckle (continuously updated)
节都需能有问题制端口, 第一个下标 。很多机器成
Summary of common errors in wechat applet cloud development
一文带你深入浅出C字符串函数和内存函数
Rlib learning - [4] - algorithmconfig detailed introduction [pytoch version]
How to solve the engine prompt alias herodb and game engine startup exceptions?
Post SQL era: edgedb 2.0 Release Notice
Esp32:arduino tutorial summary
[Huang ah code] Introduction to MySQL - 3. I use select *, and the boss directly rushed me home by train, but I still bought a station ticket
Infineon launched the world's first TPM security chip with post quantum encryption technology for firmware update
The second chapter is encog's data acquisition
To 3mm; Provide safe and stable product execution according to the sender IID
Chapter 1 regression, classification & clustering
Problems and solutions of QT (online installation package) crash in win10 installation
What is the proper resolution of the computer monitor? Introduction to the best resolution of monitors of various sizes and the selection of different wallpapers
Chapter III encog workbench
Event extraction and documentation (2020-2021)
The difference between statement and Preparedstatement and how to use placeholders
Common cross domain problems
Why can't I log on my baidu account? Introduction to the solution of baidu account unable to log in