C形式的字符串,指针,数组 [英] C style strings, Pointers, arrays

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

问题描述

我无法理解一个C风格的字符串是什么。早期的快乐新年

I'm having trouble understanding what a C-style string is. Happy early New Year

我所知道的:
指针持有的内存地址。解引用指针会给你的数据在内存的位置。

What I know: A pointer holds a memory address. Dereferencing the pointer will give you the data at that memory location.

int x = 50;
int* ptr = &x;    //pointer to an integer, holds memory address of x

cout << "&x: " << &x << endl;  //these two lines give the same output as expected
cout << "ptr: " << ptr << endl;

cout << "*ptr: " << dec << (*ptr) << endl;  //prints out decimal number 50
                                           //added dec, so the program doesnt 
                //continue to printout hexidecimal numbers like it did for the 
                 //the memory addresses above
cout << "&ptr: " << &ptr << endl;  //shows that a pointer, like any variable,
                                  //has its own memory address

现在什么我不明白(使用什么上面为源我的困惑):
有多种方法来声明字符串。我正在学习C ++,但你也可以使用C风格的字符串(好懂,虽然逊色于C ++字符串)

Now to what I don't understand (using what's above as the source for my confusion): There are multiple ways to declare strings. I'm learning C++, but you can also use C-style strings (good to understand, although inferior to C++ strings)

string intro = "Hello world!"; 
//the compiler will automatically add a null character, \0, so you don't have to
//worry about declaring an array and putting a line into it that is bigger than 
//it can hold. 

C风格:

char version1[7] = {'H','i',' ','y','o','u','\0'};
char version2[] = "Hi you"; //using quotes, don't need null character? added for you?
char* version3 = "Hi you";

版本3就是我遇到麻烦了。在这里,有一个指针为char。我知道数组名是一个指向第一个元素在数组中。

Version3 is where I'm having trouble. Here, there is a pointer to a char. I know that an array name is a pointer to the first element in an array.

cout << " &version3: " << &version3 << endl; //prints out location of 'H'
cout << " *version3: " << *version3 << endl; //prints out 'H'
cout << "  version3: " <<  version3 << endl; //prints out the whole string up to
                                             //automatically inserted \0

在一节中的我知道,打印出的指针的名称将打印出其持有的地址。这里,打印出的指针打印出整个字符串的名称。做双引号你不知何故告诉程序:嘿,我知道你是一个指针,你被初始化为H的位置,但因为我看到这些双引号,快进存储器中的1个字节和打印输出你看到的一切,直到你达到一个\\ 0(1字节运动,因为焦炭的是1个字节大)。

Before, in the section "what I knew," printing out the name of a pointer would print out the address that it held. Here, printing out the name of a pointer prints out the whole string. Do the double quotes around "Hi you" somehow tell the program: "hey I know you are a pointer, and you are initialized to the location of 'H', but because I see these double quotes, skip forward 1 byte in memory location and printout everything you see until you reach a \0" (1 byte movement because char's are 1 byte large).

它是如何打印出一个指针打印出一个字符串?打印出前一个指针名字打印出来,它被初始化的内存地址。

How is it that printing out a pointer prints out a string? Before printing out a pointer name printed out the memory address it was initialized to.

编辑:请问 COUT&LT;&LT; &安培;版本3 打印出H或指针的位置的位置,版本3 ,其中包含'H'的内存地址?

Does cout << &version3 print out the location of 'H' or the location of the pointer, version3, which holds the memory address of 'H'?

推荐答案

打印出的char * COUT 从打印的工作方式不同,比方说,一个为int * COUT 。对于C风格的字符串兼容的&LT重载版本;&LT; 这需要一个的char * 参数对待的char * 作为一个C风格的字符串。如果你想打印一个的char * 持有的内存地址,你可以将其转换为无效*

Printing out a char* with cout works differently from printing, say, an int* with cout. For compatibility with C-style strings, the overloaded version of << which takes a char* argument treats the char* as a C-style string. If you want to print the memory address that a char* holds, you can cast it to a void*.

是的,如果你写的任何

char *s1 = "hi lol";
char s2[] = "hi haha";

一个NUL( \\ 0 )终止是在字符串的结尾为你添加。这两者之间的区别是, S1 是一个指向一个字符串,其中你是不是内容,按照C标准,应该进行修改,而 S2 阵列的,就是分配给你在栈上的内存块,这是的初始化的持有价值喜哈哈,你可以自由地修改其内容请你。内存分配给数组的量是完全足以容纳用作初始化字符串,并自动为您决定的,这也是为什么方括号可以是空的。

a NUL (\0) terminator is added for you at the end of the string. The difference between these two is that s1 is a pointer to a string literal the contents of which you are not, according to the C standard, supposed to modify, whereas s2 is an array, that is, a block of memory allocated for you on the stack, which is initialized to hold the value "hi haha", and you are free to modify its contents as you please. The amount of memory allocated for the array is exactly enough to hold the string used as the initializer, and is determined for you automatically, which is why the square brackets can be empty.

一个侧面说明:在C,如果你输入个char [3] =ABC; ,那么取值将被初始化为 {'A','b','C'} 没有的一个NUL终结者!这是因为在标准说,在这种情况下的字符串用一个NUL终止的如果有空间阵列中初始化的条款的的(或一些类似的措辞)。在C ++中,这是不是这样的。有关详细信息,请参阅No当固定大小的字符数组时没有足够的空间,空终止初始化编译错误。

One side note: in C, if you enter char s[3] = "abc";, then s will be initialized to {'a', 'b', 'c'}, without a NUL terminator! This is because of a clause in the standard that says that strings in this context are initialized with a NUL terminator if there is room in the array (or some similar wording). In C++ this is not the case. For more, see No compiler error when fixed size char array is initialized without enough room for null terminator.

修改,以应对您的问题补充:如果您有的char * s =...,你 COUT&LT;&LT; &安培; S; ,将打印在那里了<中的指针取值存储,而不是地址的地址code>取值保存(在何种取值指字符串的第一个元素的地址)。

Edit, in response to your added question: If you have char *s = "...", and you cout << &s;, it will print the address where the pointer s is stored, rather than the address that s holds (the address of the first element of the string to which s refers).

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

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