简体   繁体   English

c - 为什么将字符指针索引为int是有意义的?

[英]c - why does it make sense that indexing a character pointer is an int?

char *a = "apple";
printf("%s\n", a);  // fine
printf("%s\n", a[1]);  // compiler complains an int is being passed

Why does indexing a string pointer give me an int? 为什么索引字符串指针给我一个int? I was expecting it to just print the string starting at position one (which is actually what happens when i use &a[1] instead). 我期待它只是从第一个位置打印字符串(实际上当我使用&a[1]时会发生这种情况)。 why do i need to get the address? 为什么我需要获得地址?

That's just how the [] operator is defined - a[1] , when a is a char * , fetches the next char after the one pointed to by a ( a[0] is the first one). 这就是[]运算符的定义方式 - a[1] ,当achar * ,在aa[0]是第一个)指向的char之后获取下一个char

The second part of the puzzle is that char values are always promoted to int (or rarely, unsigned int ) when passed as part of a function's variable-length argument list. 这个难题的第二部分是,当作为函数的可变长度参数列表的一部分传递时, char值总是被提升为int (或很少, unsigned int )。

a is equivalent to &a[0] , and it prints from the first character - so it makes sense that &a[1] would print starting from the second character. a等价于&a[0] ,并从第一个字符打印 - 因此从第二个字符开始打印&a[1]是有意义的。 You can also just use a + 1 - that's completely equivalent. 你也可以使用a + 1 - 这完全相同。

If you use the %c conversion specifier, which prints a single character, you can use a[1] to print just the second character: 如果使用打印单个字符的%c转换说明符,则可以使用a[1]仅打印第二个字符:

printf("%c\n", a[1]);

The expression a[1] yields a single char , and in expressions that is widened to an int . 表达式a[1]产生一个char ,并且在扩展为int的表达式中。
You can print a char with %c : 您可以使用%c打印字符:

char *a = "apple";
printf("%c\n", a[1]);   // prints 'p'

You can achieve what you want by using a+1 , like 您可以使用a+1来实现您想要的效果

printf("%s\n", a+1);    // prints 'pple'

An other way to explain this: 另一种解释方法:

char *a2 = a+1;   // a2 points to 'pple'   
a[1] ≡ *(a+1)  ≡ *a2 

%s expects a char* pointer. %s需要一个char *指针。 Char alone is interpreted as an integer. 单独的Char被解释为整数。 Moreover, a[1] gives you the second element, not the first! 而且,[1]给你第二个元素,而不是第一个元素!

Characters (ie the kind of thing that a[1] evaluates to) are integers, but the "%s" formatter for printf() expects a pointer. 字符(即[1]求值的东西)是整数,但printf()的“%s”格式化程序需要一个指针。 Note that the fact that this error was detected at all is an extended feature offered by some compilers - it's not part of Standard C. Other compilers would simply fail at run-time, probably with a core dump. 请注意,检测到此错误的事实是某些编译器提供的扩展功能 - 它不是标准C的一部分。其他编译器只会在运行时失败,可能是核心转储。

char *a = "bla"

a : is a char* , and you should use %s for printf(...) ; a :是char* ,你应该使用%s作为printf(...) ;

a[1] is equivalent to *(a+1) , then a[1] is a char , and you should use %c for printf(...) ; a[1]相当于*(a+1) ,则a[1]char ,你应该使用%c作为printf(...) ;

&a[1] is equivalent to &(*(a+1)) , then &a[1] is a char* , and you should use %s for printf(...) ; &a[1]相当于&(*(a+1)) ,然后&a[1]char* ,你应该使用%s作为printf(...) ;

This is more like a pointer question. 这更像是一个指针问题。 To better understand how pointers work, think this way: 为了更好地理解指针是如何工作的,请这样思考:

char *j;

j is a char* j是一个char*
*j is a char , and equivalent with j[0] *j是一个char ,等同于j[0]
*(j+1) is a char , and equivalent with j[1] *(j+1)是一个char ,等同于j[1]
&(*j) is a char* , and equivalent with &j[0] , equivalent with j &(*j)char* ,与&j[0]等效,等同于j
&j is a char** &j是一个char**

Another example: 另一个例子:

char j**

j is a char** j是一个char**
*j is a char* *jchar*
**j is a char , and equivalent with *(*(j+0)+0) , and equivalent with j[0][0] **j是一个char ,等价于*(*(j+0)+0) ,等价于j[0][0]
&j is a char*** &j是一个char***

and so on... 等等...

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

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