通过指针制作字符串 [英] Making strings by pointers

查看:77
本文介绍了通过指针制作字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到指针指向内存地址,因此我可以使用它来更改在该地址设置的值.像这样:

I have learned that a pointer points to a memory address so i can use it to alter the value set at that address. So like this:

int *pPointer = &iTuna;

pPointer在这里的存储器地址为iTuna.因此,我们可以使用pPointer更改iTuna处的值.如果我打印pPointer,则打印内存地址,如果我打印*pPointer,则打印iTuna处的值

pPointer here has the memory address of iTuna. So we can use pPointer to alter the value at iTuna. If I print pPointer the memory address gets printed and if I print *pPointer then the value at iTuna gets printed

现在看这个程序

    char* pStr= "Hello !";
cout<< pStr << endl;
cout<< *pStr << endl;
system("PAUSE");
return 0;

这里有很多我不明白的东西:

There are a lot of stuff I don't understand here:

  1. 在你好!"中每个字母分别存储,一个指针保存一个存储器地址.那么pStr如何指向所有字母.

另外,当我打印出pStr时,它会打印 Hello!,而不是内存地址.

Also When I print out pStr it prints Hello !, not a memory address.

当我打印出*pStr时,它也打印出了 H ,但并不是pStr所指的全部.

And when I print out *pStr it prints out H only not all what pStr is pointing too.

我真的不明白,这是我的担忧.希望有人可以向我解释这是如何工作的广告,帮助我理解

I really can't understand and these are my concerns. I hope someone can explain to me how this works ad help me understand

推荐答案

  1. "Hello !"是类型为char const[8]且值为{ 'H', 'e', 'l', 'l', 'o', ' ', '!', 0 }数组. pStr是指向其第一个元素的指针;它的最后一个元素的值为0.

  1. "Hello !" is an array of type char const[8] and value { 'H', 'e', 'l', 'l', 'o', ' ', '!', 0 }. pStr is a pointer to its first element; its last element has the value 0.

iostreams库中有一个用于char const *参数的重载,该参数将ar­ gu­ ment视为指向数组第一个元素的指针,并打印每个元素,直到遇到零为止. (用通俗的说法是"Null-ter­ mi­ nat­ ed字符串".)

There is an overload in the iostreams library for a char const * argument, which treats the ar­gu­ment as a pointer to the first element of an array and prints every element until it encounters a zero. ("Null-ter­mi­nat­ed string" in colloquial parlance.)

解引用指向数组第一个元素的指针会为您提供数组的第一个元素,即'H'.等同于pStr[0].

Dereferencing the pointer to the first element of an array gives you the first element of the array, i.e. 'H'. This identical to pStr[0].

这篇关于通过指针制作字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆