的C指针运算二维数组 [英] C pointer arithmetic for 2D arrays

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

问题描述

// strings is a 2D array (each string is 11 bytes long)
char strings[][11] = {"0123456789", "2222244444", "3333366666"};   

printf("String 3 Character 2 is %c\n", strings[2][1]);

我如何使用指针运算而不是 code此print语句串[2] [1]

推荐答案

在C, A [B] 相同 *(一+ b)(而且由于加法是可交换的,这意味着它也等同于 b [A] 。人们写国际混淆C $ C $ç竞赛常常依赖于这一点,使用像 X [字符串]; 不用说,这是最好避免那种事情,除非你是故意被邪恶虽然...

In C, a[b] is the same as *(a+b) (and since addition is commutative, that implies that it's also equivalent to b[a]. People writing for the International Obfuscated C Code Contest frequently rely on this, using things like x["string"];. Needless to say, it's best to avoid that sort of thing unless you're intentionally being evil though...

编辑:对于任何人谁是确保他们的问题的理解是达到标准应随时分析以下正确predict输出运行它之前:

For anybody who's sure their understanding of the subject is up to snuff should feel free to analyze the following and correctly predict its output before running it:

#include <stdio.h>

char *c[] = { "ENTER", "NEW", "POINT", "FIRST" };
char **cp[] = { c+3, c+2, c+1, c };
char ***cpp = cp;

main()
{
    printf("%s", **++cpp);
    printf("%s ", *--*++cpp+3);
    printf("%s", *cpp[-2]+3);
    printf("%s\n", cpp[-1][-1]+1);
    return 0;
}

如果没记错,信贷(错?)那个特定code去萨德·史密斯。

If memory serves, the credit (blame?) for that particular code goes to Thad Smith.

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

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