编译环境:VS2010+QT4.8.6, WIN32平台
一、当传递的值为整数时:
1. 值传递(难度:简单)
#include "stdafx.h"
#include "iostream"
void funct(int p)
{
printf(" 3.在调用的函数中 = %x \n",&p);
printf(" 4.在调用的函数中 =%x",p);
p=0xFF;
}
int _tmain(int argc, _TCHAR* argv[])
{
int a=0x10;
printf("\n 1. = %x",&a);//取变量a的地址:43fd20 (注:每次运行程序该a地址变量为内存随机提供)
printf("\n 2. = %x \n",a);//将变量a的数值打印:10
funct(a);// 结果分别为:43fc4c ,10
printf("\n 5. = %x \n",a);//在调用函数funct()后,改变p的变量,仍然无法改变p的数值
system("pause"); //暂停
return 0;
}
二、当传递的值为字符串时:
#include "stdafx.h"
#include "iostream"
#include <string.h>
void printString(char* myString)
{
std::printf("字符串等于 %s \t\n",myString);
}
int _tmain(int argc, _TCHAR* argv[])
{
char str[10];
strcpy(str,"helloworld");
printString(str);
system("pause");
return 0;
}
三、当传递的值为二维数组时:
#include "stdafx.h"
#include "iostream"
#include <string.h>
using namespace std;
void _func(int *array,const int m,const int n)
{
int arrayList[10][10] = {0};
for (int i =0;i<m;i++)//行
{
for (int j = 0;j<n;j++)//列
{
arrayList[i][j] = *(array+n*i+j);
cout<<' '<<arrayList[i][j];
}
cout<<endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[5][4] = {{1,1,1,1},{2,2,2,2},{3,3,3,3},{4,4,4,4},{5,5,5,5}};
_func((int*)a,5,4);
system("pause");
return 0;
}