指针数组C ++ [英] pointer to array c++

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

问题描述

什么是以下code干什么?

  INT政[] = {9,8};
INT(* j)条=克;

从我的理解它创建一个指向2整型数组。
但后来为什么这项工作:

  INT X =Ĵ[0];

这不工作:

  INT X =(* j)条[0];


解决方案

括号是在你的例子是多余的。指针不关心是否有涉及到一个数组 - 只知道它指向一个 INT

  INT政[] = {9,8};
  INT(* j)条=克;

也可以被改写为

  INT政[] = {9,8};
  为int * J =克;

这也可被改写为

  INT政[] = {9,8};
  为int * J =&放克[0];

一个指针到一个数组看起来像

  INT政[] = {9,8};
  INT(* j)条[2] =&放大器;克;  //提领'J'和访问数组元素为零
  INT N =(* j)条[0];

有一个关于指针声明(以及如何神交他们)在这里这个链接一个良好的阅读:<一href=\"http://www.$c$cproject.com/Articles/7042/How-to-inter$p$pt-complex-C-C-declarations\">http://www.$c$cproject.com/Articles/7042/How-to-inter$p$pt-complex-C-C-declarations

What is the following code doing?

int g[] = {9,8};
int (*j) = g;

From my understanding its creating a pointer to an array of 2 ints. But then why does this work:

int x = j[0];

and this not work:

int x = (*j)[0];

解决方案

The parenthesis are superfluous in your example. The pointer doesn't care whether there's an array involved - it only knows that its pointing to an int

  int g[] = {9,8};
  int (*j) = g;

could also be rewritten as

  int g[] = {9,8};
  int *j = g;

which could also be rewritten as

  int g[] = {9,8};
  int *j = &g[0];

a pointer-to-an-array would look like

  int g[] = {9,8};
  int (*j)[2] = &g;

  //Dereference 'j' and access array element zero
  int n = (*j)[0];

There's a good read on pointer declarations (and how to grok them) at this link here: http://www.codeproject.com/Articles/7042/How-to-interpret-complex-C-C-declarations

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

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