char和char *(pointer) [英] char and char* (pointer)
问题描述
我想了解指针如何工作,所以我创建了这个小程序。首先我创建一个p指针,指向一个char。
I would like to understand how pointers work, so i created this small program. first of all i create a p pointer, which points to a char.
第一个问题是这一点。如果我创建一个指针,它的值是一个memoryaddress(如果我指向一个非指针对象),但这次是哈哈在我的例子。为什么它在char *中以这种方式工作?我怎么可以通过cin >> p增加价值?
The first question is at this point. If i create a pointer, the value of it is a memoryaddress (if i point it to a non-pointer object), but this time it is "haha" in my example. Why does it work this way in char*? And how i can add value to it with cin >> p?
我的第二个问题是,我创建了一个q char,它在我创建它的点上具有* p指针的值。但它的值和地址也是h,但为什么?它必须是这个char对象的内存地址!这是毫无意义的:D(mingw - gcc)
My second question is that, i created a q char, which has the value of the *p pointer at the point i created it. BUT its value and address are "h" too, but why? It must be the memory address of this char object! It's pointless :D (mingw - gcc)
int main() {
char *p;
cin >> p; //forexample: haha
char q = *p;
cout << "&q = " << &q << endl; //&q = h
cout << "q = " << q << endl; //q = h
return 0;
}
更多:如果我先用char a [100] char * p = a;那么& q = h»ŢĹ,所以h和一些混乱。但它应该是一个memoryaddress!
MORE: If i allocate memory first with char a[100]; char *p=a; then &q = h»ŢĹ, so "h" and some mess. but it should be a memoryaddress! and my question is, why is not it address then?
推荐答案
想想 char * p;
从内存中的地址。
Think of char* p;
as of address in memory. You did not initialize this pointer so it does not point to anything, you cannot use it.
要始终安全:
或者初始化指针到指针零:
To be safe always:
either initialize pointer to zero:
char *p = 0; // nullptr in C++11
或初始化到某些自动
void foo() {
char a[100];
char *p = a;
}
或全局内存:
char a[100];
void foo() {
char *p = a;
}
或获取动态内存:
char* p = new char [100];
然后您可以使用p(如果不为NULL)读取数据并从p ...
Then you can use p (if not NULL) to read data and to read from p...
对于您误解的 operator>> (std :: istream& char * p)
。这个运算符期望 p
指向一些内存(自动,全局,动态 - 不管),它不会自己分配内存。它只是从输入流读取字符,直到空格并将其复制到 p
指向的内存 - 但 p
到一些内存。
For your misunderstaning of operator >> (std::istream&, char* p)
. This operator expects that p
points to some memory (automatic,global,dynamic - no matter) - it does not allocate memory by itself. It just reads characters from input stream until whitespace and copy it to the memory pointed by p
- but p
must already points to some memory.
取地址 char q;
。当然你可以取地址 q
:& q
,类型是 char * p
。但& q
与 p
不同,此 q = * p
只是复制 p
指向 q
指向的第一个字符,它不能更改 q
- 其地址不可更改。对于 cout<< & q
- 运算符<< (ostream& char; p)
期望 p
指向NULL终止的字符串和& q
指向包含H
的内存,但是这个字符后没有人知道 - 所以你会在屏幕上得到一些垃圾。使用 cout<< q
以打印单个字符。
For taking address of char q;
. Of course you can take address of q
: &q
, and it type is char* p
. But &q
is different that p
, and this q=*p
just copies first character pointed by p
to q
, it cannot change address of q
- its address is unchangeable. For cout << &q
- operator << (ostream&, char* p)
expects that p
points to NULL terminated string - and &q
points to memory containing "H"
but what is after this character no one knows - so you will get some garbage on screen. Use cout << q
to print single character.
这篇关于char和char *(pointer)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!