字符串指针和c中的字符数组 [英] String pointer and array of chars in c

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

问题描述

我刚刚开始学习C,发现关于字符串指针和字符串(char的数组)有些困惑.有人可以帮我清一下头吗?

I just start learning C and found some confusion about the string pointer and string(array of char). Can anyone help me to clear my head a bit?

// source code
char name[10] = "Apple";
char *name2 = "Orange";

printf("the address of name: %p\n", name);
printf("the address of the first letter of name: %p\n", &(*name));
printf("first letter of name using pointer way: %c\n", *name);
printf("first letter of name using array way: %c\n", name[0]);
printf("---------------------------------------------\n");
printf("the address of name2: %p\n", name2);
printf("the address of the first letter of name2: %p\n", &(*name2));
printf("first letter of name2 using pointer way: %c\n", *name2);
printf("first letter of name2 using array way: %c\n", name2[0]);

// output
the address of name: 0x7fff1ee0ad50
the address of the first letter of name: 0x7fff1ee0ad50
first letter of name using pointer way: A
first letter of name using array way: A
---------------------------------------------
the address of name2: 0x4007b8
the address of the first letter of name2: 0x4007b8
first letter of name2 using pointer way: O
first letter of name2 using array way: O

所以我假设name和name2都指向他们自己的第一个字母的地址.那么我的困惑是(请参见下面的代码)

so I assume that both name and name2 point to the address of their own first letter. then my confusion is(see the code below)

//code
char *name3; // initialize a char pointer
name3 = "Apple"; // point to the first letter of "Apple", no compile error

char name4[10]; // reserve 10 space in the memory
name4 = "Apple"; // compile errorrrr!!!!!!!!!!

我创建了一个名为name2的char指针,并指向了"Apple"的第一个字母的name2指针,然后,我创建了另一个char数组并在内存中分配了10个空间.然后尝试使用name4(该地址指向"Apple"的第一个字母).结果,我遇到了编译错误.

I create a char pointer called name2 and name2 pointer to the first letter of "Apple" which is fine, then I create another array of char and allocate 10 space in the memory. and then try to use name4 which is an address points to the first letter of "Apple". As a result, I got a compile error.

我对这种编程语言感到非常沮丧.有时它们的工作方式相同.但是有时候他们没有.谁能解释原因,以及我是否真的想在分隔的行中创建字符串或字符数组.我该怎么做??

I am so frustrated by this programming language. sometimes they works the same way. but sometimes they doesn't. Can anyone explain the reason and if I really want to create a string or array of chars in separated lines. how I can do that???

非常感谢...

推荐答案

可以在声明数组时初始化它,如下所示:

You can initialize an array when you declare it, like this:

int n[5] = { 0, 1, 2, 3, 4 };
char c[5] = { 'd', 'a', 't', 'a', '\0' };

但是由于我们通常将char数组视为字符串,因此C允许出现特殊情况:

But since we typically treat char arrays as strings, C allows a special case:

char c[5] = "data";  // Terminating null character is added.

但是,一旦声明了数组,就无法重新分配它.为什么?考虑类似

However, once you've declared an array, you can't reassign it. Why? Consider an assignment like

char *my_str = "foo";  // Declare and initialize a char pointer.
my_str = "bar";        // Change its value.

第一行声明一个char指针,并将其对齐"到foo中的第一个字母.由于foo是字符串常量,因此它与所有其他常量一起位于内存中的某个位置.重新分配指针时,将为其分配一个新值:bar的地址.但是原始字符串foo保持不变.您已经移动了指针,但没有更改数据.

The first line declares a char pointer and "aims" it at the first letter in foo. Since foo is a string constant, it resides somewhere in memory with all the other constants. When you reassign the pointer, you're assigning a new value to it: the address of bar. But the original string, foo remains unchanged. You've moved the pointer, but haven't altered the data.

但是,在声明数组时,根本就没有声明指针.您要保留一定数量的内存并为其命名.所以这行

When you declare an array, however, you aren't declaring a pointer at all. You're reserving a certain amount of memory and giving it a name. So the line

char c[5] = "data";

以字符串常量data开始,然后分配5个新字节,将其称为c,然后将字符串复制到其中.您可以完全访问数组的元素,就好像您声明了指向它们的指针一样.数组和指针(在大多数情况下)可以这种方式互换.

starts with the string constant data, then allocates 5 new bytes, calls them c, and copies the string into them. You can access the elements of the array exactly as if you'd declared a pointer to them; arrays and pointers are (for most purposes) interchangeable in that way.

但是由于数组不是指针,因此无法重新分配它们.

您不能使c在其他任何地方指向",因为它不是指针.这是内存区域的名称.

You can't make c "point" anywhere else, because it's not a pointer; it's the name of an area of memory.

可以更改字符串的值,一次更改一个字符,或通过调用类似strcpy()的函数:

You can change the value of the string, either one character at a time, or by calling a function like strcpy():

c[3] = 'e';       // Now c = "date", or 'd', 'a', 't', 'e', '\0'
strcpy(c, "hi");  // Now c = 'h', 'i', '\0', 'e', '\0'
strcpy(c, "too long!") // Error: overflows into memory we don't own.


效率提示

还要注意,初始化数组通常会复制数据:将原始字符串从常量区域复制到数据区域,程序可以在其中进行更改.当然,这意味着您使用的内存是预期的两倍.您可以避免复制,而通常通过声明一个指针来节省内存.这样一来,无论字符串的长度如何,都将字符串保留在恒定区域中,并且仅为指针分配足够的内存.

Note, also, that initializing an array generally makes a copy of the data: the original string is copied from the constant area to the data area, where your program can change it. Of course, this means you're using twice as much memory as you may have expected. You can avoid the copy and generally save memory by declaring a pointer instead. That leaves the string in the constant area and allocates only enough memory for a pointer, regardless of the length of the string.

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

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