函数返回一个指向int数组的指针 [英] Function returning a pointer to an int array

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

问题描述

我正在从Primer 5th Edition学习C ++,我正在将指针返回数组.该函数的声明为:

I am learning C++ from Primer 5th edition and I am at Returning a Pointer to an Array. The declaration of this function is:

 int (*func(int i))[10]; 

,它期望返回一个指向数组的指针.

and it's expected to return a pointer to an array.

我编写了执行此操作的代码:

I wrote code that does this:

#include <iostream>
#include <string>
using namespace std;

int *func(){
  static int a[]={1,2,3};
  return a;
}
int main (){
  int *p=func();
  for(int i=0;i!=3;++i){
    cout<<*(p+i);
  }
}

它正在工作.但是我想知道我在这里所做的与

And it is working. But I want to know the difference between what I made here and

  int (*func(int i))[10]; 

如何使此函数调用起作用,因为在书中没有任何具体示例.

How I can make this function call work, because in the book, there isn't any concrete example.

推荐答案

阅读:什么的sizeof(& array)返回吗?以了解array nameaddress of array之间的差异.

Read: What does sizeof(&array) return? to understand diffrence between array name and address of array.

第一季度,我想知道两者之间的区别:

Q1 I want to know the difference between:  

在您的代码中:

  int *func(){
    static int a[]={1,2,3};
    return a;
  }

您正在返回第一个元素的地址.实际上,a的类型是int[3],它会衰减为int*.重要的是
您将地址存储在int* p中,并且可以将数组元素评估为p[i].

you are returning address of first element. Actually type of a is int[3] that decays into int*. Important is
You stores address into int* p and can assess elements of array as p[i].

如果您的函数为int int (*func())[3],则返回&a,然后分配给int(*p)[3]并可以访问(*p)[i].
注意:&a的类型是int(*)[3].

Whereas if your function would be int int (*func())[3] then you return &a, and assign to int(*p)[3] and can access (*p)[i].
Note: type of &a is int(*)[3].

第二季度,我如何使此函数调用起作用,因为在书中没有任何具体示例.

Q2 How i can make this function call work, because in the book, there isn't any concrete example.

like:

int (*func())[3]{
    static int a[]={1,2,3};
    return &a;
}

和main():

int main(){ 
 int i=0;    
 int(*p)[3] = func();
 for(i=0; i<3; i++)
   printf(" %d\n", (*p)[i]);
 return 0;
}

您可以检查工作ID为 Idone

You can check second version of code working id Ideone

第一季度,我想知道两者之间的区别:

Q1 I want to know the difference between:  

您有兴趣知道两者之间的区别,因此现在在两种版本的代码中比较p的两种不同声明:

As you are interested to know diffrence between two so now compare two different declarations of p in two versions of code:

1): int* p;,我们将数组元素以p[i]的形式访问,该元素等于*(p + i).

1) : int* p; and we access array elements as p[i] that is equals to *(p + i).

2): int (*p)[i],我们将数组元素以(*p)[i]的形式访问,该元素等于*((*p) + i)或只是= *(*p + i). (我在*p周围添加了()来访问数组元素,因为[]运算符的优先级高于*,因此简单的*p[i]意味着对数组元素的防御).

2) : int (*p)[i] and we access array elements as (*p)[i] that is equals to *((*p) + i) or just = *(*p + i). ( I added () around *p to access array element because precedence of [] operator is higher then * So simple *p[i] means defense to the array elements).

除返回类型外的其他信息:

An addition information other then return type:

在这两种函数中,我们返回的都是静态变量(数组)的地址,并且静态对象的生命周期直到程序不终止为止.因此,访问数组大号func()不是问题.

In both kind of functions we returns address that is of a static variable (array), and a static object life is till program not terminates. So access the array outsize func() is not a problem.

请考虑一下,如果返回的不是静态(且动态分配)的简单数组(或变量)的地址,则会在您的代码中引入未定义行为,从而导致崩溃.

Consider if you returns address of simple array (or variable) that is not static (and dynamically allocated) then it introduce as Undefined behavior in your code that can crash.

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

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