char 和 char*(指针)

2022-01-12 00:00:00 pointers char c++ type-mismatch dereference

我想了解指针是如何工作的,所以我创建了这个小程序.首先,我创建了一个指向 char 的 p 指针.

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.

第一个问题到此为止.如果我创建一个指针,它的值是一个内存地址(如果我将它指向一个非指针对象),但这次在我的示例中是哈哈".为什么它在 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)

#include <iostream>

int main() 
{
 /* char *p;
    cin >> p;                      //forexample: haha */

    char * p = "haha";
    char q = *p;
    std::cout << "&q = " << &q << std::endl;   //&q = h
    std::cout << "q  = " <<  q << std::endl;   //q = h

    return 0;
}

更多:如果我先用 char a[100] 分配内存;字符 *p=a;然后&q = h???,所以h"有些乱.但它应该是一个内存地址!我的问题是,为什么不解决呢?

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.但是&qp不同,这个q=*p只是复制p指向的第一个字符到q,它不能改变q的地址――它的地址是不可改变的.对于 cout <<&q - 运算符 <<(ostream&, char* p) 期望 p 指向以 NULL 结尾的字符串 - 而 &q 指向包含 "H"<的内存/code> 但是这个字符后面是什么没人知道 - 所以你会在屏幕上看到一些垃圾.使用 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.

相关文章