char [] vs char*

To declare a char array,there are mainly two ways ,through [] or to declare a char pointer.This seems a very easy thing,but they really got differences.Let's say we have char A[10]="012345" and char* msg ="hello";

First ,when using sizeof , sizeof(A) will return 10(bytes of the array) while sizeof(msg) will return probably 4(size of char*),despite that they are all addresses(pointers).

Second,we can say  msg = A, or msg ="hi " ,just changing the pointer's value ; however, we can't say A = msg ,neither can we say A = "67890".You see,the pointer way is much more flexible.

But,we could change the [] arrays through functions,which is more confusing and tricky.Let's look at the testing code below.In the changeArray(char Array[]) function,we use [] as the formal argument(形式参数).And we could,surprisingly,to directly give the array a new value. So why would this work without compiling error?

In fact,as formal parameters ,the [] are always considered as pointers.This is a very important thing to keep in mind,and you could tell changeArray is no different from changeArray2 if we don't consider the implementation.

However,despite that no compilation error occurs when calling the function,we will find the actual argument(实际参数)never changes in the main function.Why?Cause we are just changing a local copy of the array.

To really change the array's value,we got several ways .See the next several functions in the code below.

We could choose to change one character  at a time ,whether we pass a char array or a char pointer.I don't figure out how it works,but it just ,works.

The better way is to use another * or & just like we pass an integer by pointer or by reference(引用传递) to really change its value.If we use  **Array,we must add & in the actual argument,cuz we are using the address of the address of the Array,or its first element.While if we use *&,we don't need that,neither do we need to dereference(间接引用)the pointer in the function.

Another question is that why add const?Notice that there are comments telling that if we pass msg to changeArray2 to change a single character,it won't work.Yeah there will be no compiling error,however,the program will go wrong.That' s because we just assign a constant string to the pointer,and the attempt to change a constant string will of course fail.But why will the next two functions work even with so many const?Because in these functions,we are just changing where the pointer points at ,rather than the contents of where it points at.

#include<iostream>

using namespace std;

void getSize(int Array[]){
	cout<<sizeof(Array)<<endl;
}

void changeArray(char Array[]){
	Array="Starscream";
}

void changeArray2(char *Array){
	Array[2]='Q';
}

void changeArray3(const char **Array){
	*Array="Starscream";
}

void changeArray4(const char *&Array){
        Array="666";
}
int main(){
	int *var = new int;
	cout<<"Test for sizeof int[] and int*"<<endl;
	int A[5]={1,2,3,4,5};
	cout<<sizeof(A)<<endl;
	getSize(A);
	
	cout<<"Test for char* and char[] as arguments"<<endl;
	char str[]="Wang Yi Zhi";
	const char * str2 = "HA Ha ha";
	cout<<str<<endl;
	
	changeArray(str);
	cout<<str<<endl; 
	
	changeArray2(str);
	cout<<str<<endl;
	/*  won't work
	changeArray2(str2);
	cout<<str2<<endl;
	*/
	changeArray3(&str2);
	cout<<str2<<endl;
	
	changeArray4(str2);
	cout<<str2<<endl;
}

Results are:

Test for sizeof int[] and int*
20
8
Test for char* and char[] as arguments
Wang Yi Zhi
Wang Yi Zhi
WaQg Yi Zhi
Starscream
666


--------------------------------
Process exited after 0.5211 seconds with return value 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值