指向C中的字符串? [英] Pointer to a string in C?

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

问题描述

char *ptrChar;

我知道ptrChar是指向 char 的指针.指向字符串的指针的命令是什么?

I know that ptrChar is a pointer to char. What's the command for a pointer to string?

如果相同(从ptr转换为字符,而将ptr转换为字符串),则下面的变量定义代表什么?

If it's the same (ptr to char vs. ptr to string) — what does the variable definition below represent?

char (*ptr)[N];

我很困惑.

推荐答案

相同的符号用于指向以空字符结尾的字符串的单个字符或第一个字符:

The same notation is used for pointing at a single character or the first character of a null-terminated string:

char c = 'Z';
char a[] = "Hello world";

char *ptr1 = &c;
char *ptr2 = a;      // Points to the 'H' of "Hello world"
char *ptr3 = &a[0];  // Also points to the 'H' of "Hello world"
char *ptr4 = &a[6];  // Points to the 'w' of "world"
char *ptr5 = a + 6;  // Also points to the 'w' of "world"

ptr2ptr3中的值相同; ptr4ptr5中的值也是如此.如果要将某些数据视为字符串,请务必确保其以null终止,并且知道要使用多少空间,这一点很重要.许多问题是由于不了解可用空间以及不知道字符串是否正确以null终止而引起的.

The values in ptr2 and ptr3 are the same; so are the values in ptr4 and ptr5. If you're going to treat some data as a string, it is important to make sure it is null terminated, and that you know how much space there is for you to use. Many problems are caused by not understanding what space is available and not knowing whether the string was properly null terminated.

请注意,上面的所有指针都可以像数组一样被取消引用:

Note that all the pointers above can be dereferenced as if they were an array:

 *ptr1    == 'Z'
  ptr1[0] == 'Z'

 *ptr2    == 'H'
  ptr2[0] == 'H'
  ptr2[4] == 'o'

 *ptr4    == 'w'
  ptr4[0] == 'w'
  ptr4[4] == 'd'

  ptr5[0] ==   ptr3[6]
*(ptr5+0) == *(ptr3+6)


问题的最后补充

char (*ptr)[N];代表什么?

这完全是一个更复杂的野兽.它是指向N字符数组的指针.类型是完全不同的.它的使用方式完全不同;指向的对象的大小完全不同.

This is a more complex beastie altogether. It is a pointer to an array of N characters. The type is quite different; the way it is used is quite different; the size of the object pointed to is quite different.

char (*ptr)[12] = &a;

(*ptr)[0] == 'H'
(*ptr)[6] == 'w'

*(*ptr + 6) == 'w'

请注意,ptr + 1指向未定义的区域,但指向a开头之外的一个12字节数组".假设情况略有不同:

Note that ptr + 1 points to undefined territory, but points 'one array of 12 bytes' beyond the start of a. Given a slightly different scenario:

char b[3][12] = { "Hello world", "Farewell", "Au revoir" };

char (*pb)[12] = &b[0];

现在:

(*(pb+0))[0] == 'H'
(*(pb+1))[0] == 'F'
(*(pb+2))[5] == 'v'

除非很偶然,否则您可能不会碰到指向数组的指针.在过去的25年中,我已经使用过几次,但是用得很少,我无法用一只手指望这些场合(其中一些已经回答了Stack Overflow的问题).除了知道它们的存在之外,它们是获取数组地址的结果,而且您​​可能不想要它,因此您实际上并不需要了解有关数组指针的更多信息.

You probably won't come across pointers to arrays except by accident for quite some time; I've used them a few times in the last 25 years, but so few that I can count the occasions on the fingers of one hand (and several of those have been answering questions on Stack Overflow). Beyond knowing that they exist, that they are the result of taking the address of an array, and that you probably didn't want it, you don't really need to know more about pointers to arrays.

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

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