C++入门的一些记录

本人随便编了一个平方根的函数

#include <iostream>
#include <cmath>
using namespace std;

void f(float);

int main(){
	float a;
	cin>>a;
	f(a);
	return 0;
}

void f(float n){
	float ans;
	ans=sqrt(n);
	cout<<n<<"的平方根是:"<<ans<<endl;
	
}

C++的ascii码有趣,输出的形式由一开始变量定义类型决定

#include <iostream>
using namespace std;

int main(){
	char a;
	int b;
	cin>>a;
	b=a+1;
	a=b;
	cout<<a<<"的ascii码"<<b<<endl;
	return 0;
}

指针的初体验

#include <iostream>
using namespace std;

int main(){
	int a=10,b=20;
	int*m=&a;
	cout<<m<<"的值是"<<a<<"内存"<<sizeof(m)<<endl;
	return 0;
}

指针交换的方式,为了清晰过程,我把每一步交换方式打印出来

#include <iostream>
using namespace std;

void swap01(int*p1,int*p2){
	cout<<"初始:"<<endl;
	cout<<"p1="<<p1<<"  *p1="<<*p1<<"\n";
	cout<<"p2="<<p2<<"  *p2="<<*p2<<"\n";
	int temp=*p1;
	cout<<"int temp=*p1的时候\n"; 
	cout<<"p1="<<p1<<"  *p1="<<*p1<<"\n";
	cout<<"p2="<<p2<<"  *p2="<<*p2<<"\n";
	*p1=*p2;
	cout<<"*p1=*p2的时候\n";
	cout<<"p1="<<p1<<"  *p1="<<*p1<<"\n";
	cout<<"p2="<<p2<<"  *p2="<<*p2<<"\n";
	*p2=temp;
	cout<<"*p2=temp的时候\n";
	cout<<"p1="<<p1<<"  *p1="<<*p1<<"\n";
	cout<<"p2="<<p2<<"  *p2="<<*p2<<"\n";
} 

int main(){
	int a=10,b=20;
	swap01(&a,&b);
	cout<<"a="<<a<<"b="<<b<<"\n";
	
}

数组就是一种地址?

//数组与指针
#include <iostream>
using namespace std;
int main(){
	int arr[10]={1,2,3,4,5,6,7,8,9,10};
	int*p;
	p=arr;
	for(int i=0;i<=9;i++){
		cout<<"第"<<i+1<<"个是" <<*p<<endl;
		p++;
	} 
	return 0;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值