我必须传递给函数myfunk的论点是什么? [英] What argument I have to pass to the function myfunk?

查看:61
本文介绍了我必须传递给函数myfunk的论点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在不改变定义的情况下,我必须传递给函数Myfunk,c的函数参数是什么?



 void MyFunc(int *& data){data ++;} 
void main(){
int a = 5;
MyFunc(& a);
cout<<一个;
}





我的尝试:



我已经尝试了& a的地址存储器。

解决方案

它需要对指针的引用(到 int )。

尝试,例如:

  #include   <   iostream  >  
使用 namespace std;

void MyFunc( int *& data){data ++;}

int main()
{

int a [] = { 12 15 27 42 };

int * p =& a [ 2 ];

cout<< * p<< ENDL;

MyFunc(p);

cout<< * p<< ENDL;
}


您必须将引用传递给 int *

  int  someVal =  123 ; 
int * pSomeVal =& someVal;
MyFunc(pSomeVal);

因为引用必须是左值,所以必须创建一个变量,而不是只传递一个不是左值的变量的地址。



因为你的函数正在递增指针,所以使用数组更有意义(避免越界访问):

  int  someVals [] = { 1  2  3 }; 
int * pSomeVal = someVals;
MyFunc(pSomeVal);
cout<< * pSomeVal;


What function argument i have to pass to function Myfunk,c without changing the defition?


void MyFunc(int * & data) {data++;}
void main () {
 int a = 5;
 MyFunc(&a);
 cout << a;
}



What I have tried:

I have tried the address memory of the &a.

解决方案

It takes a reference to a pointer (to int).
Try, for instance:

#include <iostream>
using namespace std;

void MyFunc(int * & data) {data++;}

int main ()
{

  int a [] = { 12, 15, 27, 42 };

  int * p = &a[2];

  cout << *p << endl;

  MyFunc( p );

  cout << *p << endl;
}


You have to pass a reference to an int*:

int someVal = 123;
int *pSomeVal = &someVal;
MyFunc(pSomeVal);

Because a reference must be an lvalue, you must create a variable rather than just passing the address of a variable which is not an lvalue.

Because your function is incrementing the pointer, using an array makes more sense (avoid out-of-bounds access):

int someVals[] = { 1, 2, 3 };
int *pSomeVal = someVals;
MyFunc(pSomeVal);
cout << *pSomeVal;


这篇关于我必须传递给函数myfunk的论点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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