第三章 C++中的C(三)

本文介绍了C++中的各种数据类型,包括基本内部类型、bool类型、说明符以及指针的基本概念。同时探讨了如何利用指针和引用修改外部对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

3.4 数据类型简介

   数据类型定义使用存储空间的方式。通过定义数据类型,告诉编译器怎样创建一片特定的存储空间,以及怎样操纵这篇存储空间。

   数据类型可以是内部的或抽象的。内部数据类型是编译器本来能理解的数据类型,直接与编译器关联。用户定义的数据类型是程序员创建的类型,作为一个类,它们一般被称为抽象数据类型(自己定义的类,是抽象数据类型)。

3.4.1 基本内部类型

   标准C的内部类型规范值规定内部类型必须能存储的最大值和最小值。如果机器基于二进制,则最大值可以直接转换成容纳这个值所需的最少位数。

   系统头文件limits.h和float.h中定义了不同的数据类型可能存储的最大值和最小值。

   char是用于存储字符的,使用最小的8位的存储,int存储整数值,使用最小两个字节的存储空间。float和double类型存储浮点数,一般使用IEEE的浮点格式。float用于单精度浮点数,double用于双精度浮点数。

3.4.2 bool类型与true和false

   标准C++的bool类型有两种由内部的常量true(转换为整数1)和false(转换成整数0)表示的状态。

   非零值为true而零值为false

   指针在必要的时候也自动转换成bool值。

3.4.3 说明符

   说明符用于改变基本内部类型的含义并把它们扩展成一个更大的集合。有四个说明符:long、short、signed和unsigned

   long和short修改数据类型具有的最大值和最小值。一般的init必须至少有一个short int型的大小。整数类型的大小等级是:short int、int、long int。浮点数的大小等级是:float、double和long double

   signed和unsigned修饰符告诉编译器怎样使用整数类型和字符的符号位 。unsigned数不保存符号,因此有一个多余的位可用,所以它能存储比signed数大两倍的整数。

//:C03:Specify.cpp
// Demonstrate the use of specifiers
#include <iostream>
using namespace std;

int main()
{
	char c;
	unsigned char cu;
	int i;
	unsigned int iu;
	short int is;
	short iis; // same as short int
	unsigned short int isu;
	unsigned short iisu;
	long int il;
	long iil; // same as long int
	unsigned long int ilu;
	unsigned long iilu;
	float f;
	double d;
	long double ld;
	cout << "\n char = " << sizeof(c)
		 << "\n unsigned char = " << sizeof(cu)
		 << "\n int = " << sizeof(i)
		 << "\n unsigned int = " << sizeof(iu)
		 << "\n short = " << sizeof(is)
		 << "\n unsigned short = " << sizeof(isu)
		 << "\n long = " << sizeof(il)
		 << "\n unsigned long = " << sizeof(ilu)
		 << "\n float = " << sizeof(f)
		 << "\n double =" << sizeof(d)
		 << "\n long double = " << sizeof(ld)
		 << endl;
	
	
	return 0;
}


3.4.4 指针简介

   不管什么时候运行一个程序,都是首先把它装入计算机内存。因此,程序中的所有元素都驻留在内存的某处。所以程序中的每一个元素都有地址。

   定义什么样的元素和定义元素的方式通常决定元素在内存中放置的地方。

   ‘&’运算符可以取得元素的地址。例:

//:C03:YourPets2.cpp
#include <iostream>
using namespace std;

int dog, cat, bird, fish;

void f(int pet)
{
	cout << "Pet id number: " << pet << endl;
}

int main()
{
	int i, j, k;
	cout << "f(): " << (long)&f << endl;
	cout << "dog: " << (long)&dog << endl;
	cout << "cat: " << (long)&cat << endl;
	cout << "bird: " << (long)&bird << endl;
	cout << "fish: " << (long)&fish << endl;
	cout << "i: " << (long)&i << endl;
	cout << "j: " << (long)&j << endl;
	cout << "k: " << (long)&k << endl;
	
	return 0;
}

   定义一个指针时,必须规定它指向的变量类型

   注意:int* ipa, ipb, ipc;

   这个只有ipa是一个指针,其它两个是int型变量。

   C++编程的一般原则是在定义时进行初始化

3.4.5 修改外部对象

   通常向函数传递参数时,在函数内部生成该参数的一个拷贝。这称为按值传递。例:

//:C03:PassByValue.cpp
#include <iostream>
using namespace std;

void f(int a)
{
	cout << "a = " << a << endl;
	a = 5;
	cout << "a = " << a << endl;
}

int main()
{
	int x = 47;
	cout << "x = " << x << endl;
	f(x);
	cout << "x = " << x << endl;
	
	return 0;
}

从上面这个程序可以看出f(x)把X的值传给了a但最后并没有修改x的值。因此,为了修改X的值,就可以运用指针。如:

//:C03:PassAddress.cpp
#include <iostream>
using namespace std;

void f(int* p)
{
	cout << "p = " << p << endl;
	cout << "*p = " << *p << endl;
	*p = 5;
	cout << "p = " << p << endl;
}

int main()
{
	int x = 47;
	cout << "x = " << x << endl;
	cout << "&x = " << &x << endl;
	f(&x);
	cout << "x = " << x << endl;
	
	return 0;
}


这么,通过给函数传递指针可以允许函数修改外部对象。

3.4.6 C++引用简介

    在C和C++中指针的作用基本上是一样的,但是C++增加了另外一种给函数传递地址的途径。即按引用传递

    引用和指针的不同之处在于:带引用的函数调用比带指针的函数调用在语法构成上更清晰。例修改PassAddress.cpp:

//:C03:PassReference.cpp
#include <iostream>
using namespace std;

void f(int& r)
{
	cout << "r = " << r << endl;
	cout << "&r = " << &r << endl;
	r = 5;
	cout << "r = " << r << endl;
}

int main()
{
	int x = 47;
	cout << "x = " << x << endl;
	cout << "&x = " << &x << endl;
	f(x);
	cout << "x = " << x << endl;
	
	return 0;
}
    通过这个例子,可以看出使用指针和引用的不同用法。
  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凡唯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值