码迷,mamicode.com
首页 > 编程语言 > 详细

C++指针函数;函数指针; 函数参数为数组;函数参数为指针;区分

时间:2020-07-29 00:42:04      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:局部变量   nbsp   函数的参数   pac   空间   int   i++   end   new   

include<iostream>
using namespace std;

int* Array()
{  
 int *a;
 a=new int [10];
 for(int i=0;i<10;i++)
{
 a[i]=i+1;
 cout<<a[i]<<" "; 
}
 cout<<endl;
 return a;
 sizeof(a);
}

int main()
{
 int *b;
 b=Array();
 for(int i=0;i<10;i++)
  {
  cout<<b[i]<<" ";
  cout<<endl;             //sizeof(b);
  }
  return 0;
}

指针函数是一个返回值为指针的函数所以函数内部需要有一个指针作为返回值,调用时外部需要有一个指针去接收;

#include<iostream>
using namespace std;

int add(int x,int y)
{
  return x+y;
}
 
int main() 
{
  int (*fun)(int x,int y);//函数指针

  fun = add;
  cout << "(*fun)(1,1) = " << (*fun)(1,1)<<endl;
  return 0;
}

函数指针是一个指向函数的指针;它本质是一个指针;

#include<iostream>
using namespace std;
void
display(int arr[2]); int main() { int arr[2]={1,2};//被编译器释放了//加了=后由局部变量 变成了一个常量 从栈区变成了全局区 display(arr);return 0; } void display(int arr[2]) { for ( int i = 0; i < 2; i++) {
arr[i]=3;
cout<<arr[i]<<endl; } }

函数的参数是一个数组,调用只需要数组名即可;调用的参数实质是数组的首地址,由于是地址传递,不开辟新的内存空间,修改是直接在实参上修改;

#include<iostream>
using namespace std;

void display(int *p);
int main() { int arr[]={1,2};//被编译器释放了//加了=后由局部变量 变成了一个常量 从栈区变成了全局区 //int *p=arr; display(arr); return 0; } void display(int *p) { for ( int i = 0; i < 2; i++) {
*p=3; cout
<<*p<<endl; p++; } }

函数的参数是指针,存放的是地址,形参不额外开辟内存空间,修改是直接在实参上修改;

 

C++指针函数;函数指针; 函数参数为数组;函数参数为指针;区分

标签:局部变量   nbsp   函数的参数   pac   空间   int   i++   end   new   

原文地址:https://www.cnblogs.com/hyby/p/13394573.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!