方括号在C中如何工作? [英] How do square brackets work in C?

查看:86
本文介绍了方括号在C中如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用C,并且试图了解基础知识.大量的教程将告诉您一切,并让您相信它而没有任何真实的解释,并且我没有发现可以理解的答案.

I've just started with C and I'm trying to understand the basics. Plenty of tutorials will tell you things and have you believe it without any real explanation and there are no answers on that I can find that are human readable.

以下:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  int *a;

  a = malloc(5 * sizeof(int));

  a[2] = 4;

  printf("%d\n", a[0]); // Prints 0
  printf("%d\n", a[2]); // Prints 4

  return 0;
} 

我没有明确声明int *a作为指向数组的指针,但是如果我给它分配了一些内存,那么我可以像 had 那样将a声明为数组.声明带有方括号的指针只是我下面所做的快捷方式吗?方括号实际上是在做一些指针运算吗?

I have not explicitly declared int *a as a pointer to an array, but if I allocate it some memory, I can then use a like I had declared it as an array. Is declaring a pointer with square brackets just a shortcut for what I've done below? Are square brackets actually doing some pointer arithmetic?

讨厌的第二个问题

为什么将内存地址分配给a而不分配给*a?

Why is the memory address assigned to a and not *a?

推荐答案

我还没有明确声明int *a作为指向数组的指针,但是如果我给它分配了一些内存,那么我可以像必须将其声明为数组一样使用a.声明带有方括号的指针只是我下面所做的捷径吗?

I have not explicitly declared int *a as a pointer to an array, but if I allocate it some memory, I can then use a like I had declared it as an array. Is declaring a pointer with square brackets just a shortcut for what I've done below?

类似,但没有. int *aa声明为指向int的指针. int b[5]为5个int分配空间,将b声明为指向int常量指针(int的数组)(在大多数情况下可以将其视为常量)指向int的指针),并将b定义为指向已分配空间的指针.因此,int b[5]的作用要大于int *a,这也意味着int *aint b[5]更具灵活性.例如,您可以递增a,但不能递增b.

Similar, but no. int *a declares a as pointer to int. int b[5] allocates space for 5 ints, declares b as constant pointer to int an array-of-int reference (which can in most cases be treated as a constant pointer to int), and defines b to be the pointer to the allocated space. Thus, int b[5] is doing way more than int *a, which also means int *a is more flexible than int b[5]. For example, you can increment a, but not b.

malloc在堆上分配内存. int b[5](如果是全局变量)将位于程序的数据段中(即,它实际上是编译为可执行文件).如果是本地的,它将在堆栈上分配.再次,相似,但不同.

malloc allocates memory on heap. int b[5], if a global variable, will be in the program's data segment (i.e. it is literally compiled into the executable). If local, it will be allocated on stack. Again, similar, but different.

方括号实际上是在做一些指针运算吗?

Are square brackets actually doing some pointer arithmetic?

在声明中,不.当您使用指针变量时,是的:x[y]*(x + y)相同.因此,a[1]*(a + 1)相同,与*(1 + a)相同,又与1[a]相同(但请不要使用最后一个).

In declaration, no. When you use pointer variables, yes: x[y] is identical to *(x + y). So a[1] is the same as *(a + 1), which is the same as *(1 + a), which is again the same as 1[a] (but please don't use this last one).

为什么将内存地址分配给a而不分配给*a?

一个厚脸皮的问题的厚脸皮答案:因为您写的是a = ...而不是*a = ....

Cheeky answer for a cheeky question: Because you wrote a = ... and not *a = ....

John Bode提供了一个有用的指针(heh):常量指针和数组引用不是同一回事,尽管它们非常相似.一方面,sizeof(...)会给出不同的结果.

John Bode gave a useful pointer (heh): constant pointers and array references are not the same thing, although they are pretty damn similar. For one thing, sizeof(...) will give a different result.

这篇关于方括号在C中如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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