Pointers and Strings

本文探讨了C语言中数组名作为首元素地址的概念,并详细解释了如何利用这一特性处理C风格的字符串。通过实例说明了数组名可以视为指向数组首元素的指针,进而应用于字符串的输出。

The special relationship between arrays and pointers extends to C-style strings.Consider the following code:
char flower[10]="rose";
cout<<flower<<"s are red\n";
The name of an array is the address of ite first element,so flower in the cout statement is the address of the char element containing the character r.

The cout object assumes that the address of a char is the address of a string,so it prints the character at that address and then continues printing characters until it runs into the null character(\0).

In short,if you give cout the address of a character,it prints everything from that character to the first null character that follows it.

The crucial element here is not that flower is an array name but that flower acts as the address of a char.This implies that you can use a pointer-to-char variable as an argument to cout,also,because it,too,is the address of a char.Of course,that pointer should point to the beginning of a string.

转载于:https://www.cnblogs.com/hongjiumu/p/3310524.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值