是文字字符串索引标准C? [英] Is literal string indexing standard C?

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

问题描述

我偶然发现了code containig像

I stumbled upon code containig something like

char c = "abc"[1];

和它编译和运行良好用gcc! C 'B'这个前pression后。

and it compiles and runs fine with gcc! c will be 'b' after this expression.

这是标准的索引文本字符串或者是纯属运气,它的作品?

Is this standard to index literal strings or is it mere luck that it works?

推荐答案

当然是,一个字符串文字是数组类型。它在EX pression转换为指针字符键,就像是一个任何指针字符

Of course it is, a string literal is of array type. It is converted to a pointer to char in the expression and is like an any pointer to char.

char c = "abc"[1];

char *p = "abc";
char c = p[1];

是等效的。

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

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