Otaru C++ Multi-Chapter ⑧ (Two) Pointers and Arrays

Table of contents

1. In some cases, array variable names in C++ can be regarded as pointers.

2.C language scanf input statement, printf output statement

3. Use pointers as dynamic arrays


Otaru C++ multiple chapters ⑧ (1) Pointer variables icon-default.png?t=N176https://blog.csdn.net/weixin_44775255/article/details/129031168

Otaru C++ multiple chapters ⑧ (3) Pointers and strings, (4) Functions and pointersicon-default.png?t=N176 https://blog.csdn.net/weixin_44775255/article/details/129397866

1. In some cases, array variable names in C++ can be regarded as pointers.

#include<iostream>
using namespace std;
//	1.用数组名访问数组
int a[]={10,11,12,13,14,15};
int *p=a+1;
int main(){
	cout<<*a<<endl; //默认输出第一个 
	cout<<*(a+2)<<endl;
	cout<<*(++p)<<endl; 	
    return 0;
}

  1. *a represents, initially pointing to the position a[0], which is 10.
  2. *(a+2) a+2 means that the address of array a is moved back 2 digits, which is the address of a[2], and the pointer points to 12.
  3.  *p = a+1, then p points to the address of a[1], ++p points to the address of a[2], *(++p) is the value 12 of a[2].
  • It can be found that the array variable a can be used as a pointer, pointing to the first array element by default!
  • Pointers are dynamic data structures, and arrays are static data structures. Pointers are used in input statements in C language. Next, let’s learn about the input scanf and output printf of C language.


2.C language scanf input statement, printf output statement

Before using C language input and output, you must import the library: include<cstdio>  //C language standard IO

Output format: printf ("format control string"  ,  output list)

#include<iostream>
#include<cstdio>
using namespace std;
int main(){
	// 输出用法例子 
	printf("整数:%d,长整数:%ld\n",100*1234,1+3*10000000);	
	printf("单精度:%f,双精度:%lf\n",1.5*1234,1+3*1.55);	
	printf("保留2位:%.2f, 9个占位并保留3位:%9.3f\n",3.14*1422,3.14159*3);	
	printf("字符:%c, 字符串:%s",'A',"abccd"); 
	
//	 输入用法例子
	int a;
	double b;
	float c;
	scanf("%d%lf%f",&a,&b,&c);
	printf("a=%d  b=%lf  c=%f",a,b,c); 
	return 0;
	
}

 

scanf uses array name

//2.scanf使用数组名
#include<iostream>
#include<cstdio>
using namespace std;
int a[5];
int main(){
	for(int i=0;i<5;i++){
		scanf("%d",a+i); //a+i就是地址,a+0,a+1,a+4等地址 
	}
	for(int i=0;i<5;i++){
//		cout<<*(a+i)<<" ";
		printf("a[%d]=%d\n",i,a[i]);  //a[i]就是 *(a+i)
	} 
	return 0;
}


3. Use pointers as dynamic arrays

What is a dynamic array? The length of the array space can be changed, not fixed.

Using pointers as arrays can achieve dynamic changes.

For example: Find the sum of array elements.

//3.指针也可以看成数组名
#include<iostream>
#include<cstdio>
using namespace std;
int n;
int *a; //指针变量a当数组名使用 
int main(){
	scanf("%d",&n);
	//指针变成数组怎么写? 
	a = new int[n+1]; // 申请连续的n+1片的空间给指针a
	for(int i=0;i<5;i++){ // 输入值到数组
		scanf("%d",&a[i]);
	}
	for(int i=1;i<5;i++){ // 求和运算
		a[i] = a[i] + a[i-1];
	}
	for(int i=0;i<5;i++){ // 输出结果
		printf("%d ",a[i]);
	}
	return 0;
} 

Guess you like

Origin blog.csdn.net/weixin_44775255/article/details/129396791
Recommended