简体   繁体   English

常数和指针

[英]Constants and Pointers

This is going to be a long-winded post, so I apologize in advance.. but trying to ask multiple questions with context: 这将是一个冗长的帖子,所以我提前道歉..但试图用上下文提出多个问题:

So, I've been trying to learn C++, and I've been picking everything up fairly easily, but I make a habit of trying to do things alongside the tutorials in "slightly different ways", in order to ensure that I understand the functionality of the thing I'm learning. 所以,我一直在努力学习C ++,而且我一直在轻松地学习所有内容,但我习惯于尝试以“略有不同的方式”与教程一起做事,以确保我理解我正在学习的东西的功能。

I seem to have hit a mental roadblock regarding constants and their involvements with pointers. 我似乎已经遇到了关于常数及其涉及指针的精神障碍。

In the previous section, I think I got a pretty solid grip on references, dereferences (and the difference between * when used for dereferences and pointers), and then wrote this code: 在上一节中,我认为我对引用,解引用(以及*用于解引用和指针之间的差异)有一个非常可靠的把握,然后编写了这段代码:

int a[5];
int * p;

p = a;

cout << "Select the value for the first item at address(" << &a[0] << ")" << nl;
cin >> *p;
p++;
cout << "Select the value for the second item at address(" << &a[1] << ")" << nl;
cin >> *p;
p++;
cout << "Select the value for the third item at address(" << &a[2] << ")" << nl;
cin >> *p;
p++;
cout << "Select the value for the fourth item at address(" << &a[3] << ")" << nl;
cin >> *p;
p++;
cout << "Select the value for the fifth item at address(" << &a[4] << ")" << nl;
cin >> *p;

for (int n=0;n<5;n++)
    cout << a[n] << "(" << &a[n] << ")" << nl;

Now, I realize that this isn't necessarily the most "optimized" solution, ever.. but it worked, I was able to "write" into the array I had store on the memory, and display the changes. 现在,我意识到这不一定是最“优化”的解决方案,但它确实有效,我能够“写入”存储在存储器中的数组,并显示更改。 It was neat, and like I said.. I think I got a pretty good understanding of how these things worked. 这很整洁,就像我说的那样......我想我对这些事情的运作方式有了很好的理解。

..and then, I hit the next section, which used the example: ..然后,我点击下一节,使用了这个例子:

const char * terry = "hello";

to teach me, and I quote: "As in the case of arrays, the compiler allows the special case that we want to initialize the content at which the pointer points with constants at the same moment the pointer is declared" (this being on the cplusplus.com tutorial ). 教我,并引用:“就数组的情况而言,编译器允许我们想要初始化指针在声明指针的同时指向常量的内容的特殊情况”(这是在cplusplus.com 教程 )。

Anyway, to get to the point: in order to try and familiarize myself with it and the previous section, I tried writing: 无论如何,为了达到目的:为了尝试熟悉它和上一节,我尝试写:

const char * p = "Hello.";

for (int n=0;n<5;n++)
    cout << p[n] << "(" << &p[n] << ")" << nl;
    cout << nl;

..and this does not work. ..这不起作用。 I essentially tried replicating my previous code (although without the writing aspect) in an attempt to be able to read the RAM-location of each character in this constant array, and I experimented with every possible combination of pointers, references, dereferences, etc.. but I just get a print out of the whole "Hello." 我本质上尝试复制我以前的代码(尽管没有编写方面),试图能够读取这个常量数组中每个字符的RAM位置,并且我尝试了指针,引用,解引用等的每个可能的组合。但我只是打印出整个“你好”。 array with &p. 数组与&p。

How in the hell does one get the reference of a constant array? 如何在地狱中获得恒定数组的引用? I think I understand the basic premise that I'm creating an array and immediately allocating it to memory, and then assigning a pointer (p) to the first block ("H")'s location.. but I can't seem to print the blocks out. 我想我理解的基本前提是我正在创建一个数组并立即将其分配给内存,然后将指针(p)分配给第一个块(“H”)的位置..但我似乎无法打印块。 I was able to get the location of the first block to print at one point (can't remember what code I used to do this) and tied it into the for loop, but even then.. it just ALWAYS printed the location of the first block, even when assigned to follow the [n] count. 我能够获得第一个块的位置,以便在一个点上打印(不记得我曾用过的代码),并将其绑定到for循环中,但即便如此......它只是始终打印出的位置第一个块,即使被指定遵循[n]计数。

I have no idea what I'm missing, here, or if I'm just trying to reach "over my head", but maybe I just don't understand the usage/point of creating an array without being able to location the memory assignment of each item within it.. 我不知道我在这里缺少什么,或者我只是想要“超越我的脑袋”,但也许我只是不明白创建数组的用法/点而无法定位内存分配其中的每个项目..

This brings me to my next issue, though. 这让我想到了下一期。 How does one get the memory location of a char variable, whether constant or not? 如何获取char变量的内存位置,无论是否为常量? I have been having issues with this code, as well: 我也遇到过这段代码的问题:

char const a = 100;
int * b = &a;

cout << a << nl << *b << nl;
cout << nl;

Obviously, this doesn't work.. because I can't assign b to the location of a, since they're different variables.. and obviously making a char pointer is silly, since it's just going to print messed up/null characters instead of the location (I checked this, obviously.. not just assuming). 显然,这不起作用..因为我不能将b分配给a的位置,因为它们是不同的变量..显然使char指针很傻,因为它只是打印乱搞/空字符而不是位置(我检查了这一点,显然......不仅仅是假设)。

I hope I'm just missing an option to use which may just be "more advanced" than where I'm currently at.. but if I'm not, I don't quite see the point in being able to access the block location of any type of variable/constant, if you can't access ALL of them due to restrictions of the type being used. 我希望我只是错过了一个可以使用的选项,它可能比我现在所处的“更高级”...但如果我不是,我不太明白能否访问该块任何类型的变量/常量的位置,如果由于所使用的类型的限制而无法访问所有变量/常量。

This problem is sorta what limits the third quoted code, too: the fact that I can't just assign a variable to hold the &p value, since it itself would have to be a char to be compatible.. which is pointless since char can't hold the eight-digit reference. 这个问题也是限制第三个引用代码的问题:事实上我不能只分配一个变量来保存&p值,因为它本身必须是一个兼容的char ...这是没有意义的,因为char可以不要拿八位数的参考。

In

const char * p = "Hello.";

[...]   cout << &p[n]   [...]

&p[n] is a pointer to char , and cout::operator<< has an overload that takes a pointer to a char and prints a null-terminated string, that's probably why you're confused. &p[n]是一个指向char的指针,而cout::operator<<有一个重载,它接受一个指向char的指针并打印一个以null结尾的字符串,这可能就是为什么你感到困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM