#include <iostream>
using namespace std;
void swap1(int &p, int &q)
{// 引用数值交换
int temp;
temp = p;
p = q;
q = temp;
}
void swap2(int *p, int *q)
{// 指针数值交换
int temp;
temp = *p;
*p = *q;
*q = temp;
}
void Getmemory1(char **p, int num)
{
*p = (char *)malloc(sizeof(char) * num);
}
char *Getmemory2(char *p, int num)
{
p = (char *)malloc(sizeof(char) * num);
return p;
}
void Getmemory(int *z)
{
*z = 5201314;
}
const char* strA()
{
char *str = "Hello John!"; //分配一个全局数组,数据不可修改
return str;
}
const char* strA1()
{
static char str[] = "Hi, John!";//分配一个局部数组
return str;
}
int main(int argc, char *argv[])
{
int iv = 520;
int &ivv = iv; // 声明引用的同时需要进行初始化
const int iv1 = 1314; // const 常量赋值时,必须同时初始化
cout << "引用 : " << ivv << "," << iv << endl;
cout << "const 常量 : " << iv1 << endl << endl;
int a = 1, b = 2;
cout << "原始数值 : " << " a = " << a << "," << " b = " << b << endl;
swap1(a, b);
cout << "引用数值交换 : " << " a = " << a << "," << " b = " << b << endl;
swap2(&a, &b);
cout << "指针数值交换 : " << " a = " << a << "," << " b = " << b << endl;
cout << endl;
char *str1 = NULL;
Getmemory1(&str1, 100);
strcpy(str1, "John");
char *str2 = NULL;
str2 = Getmemory2(str2, 100);
strcpy(str2, "tian");
cout << "指针的指针分配内存空间 : " << str1 << endl;
cout << "返回的指针分配内存空间 : " << str2 << endl;
int v = 0;
Getmemory(&v);
cout << "整数的指针分配内存空间 : " << v << endl;
cout << endl;
const char * s1 = strA();
const char * s2 = strA1();
cout << "全局数组 : " << s1 << endl;
cout << "局部数组 : " << s2 << endl;
cout << endl;
return 0;
}

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
C/C++指针 引用
呵呵
初始化 指针变量 取地址 -
C++引用优于指针
C++引用优于指针(转载请注明来源于金庆的专栏)在KOK3服务器的崩溃错误
c++ 服务器 null 语言 c -
C++重载&指针&引用
函数重载解决了相同函数功能,函数名却不能相同的问题……
C++ 指针 重载 -
C++指针参数引用
粘个代码占位置,以后有时间把指针函数,函数指针都补上
出栈 ios #include 指针函数 临时对象 -
C++指针和引用简介
摘要 本文介绍C++指针和概念引用这是一个指针 指针的类型 指针所指向的类型 指针表达式 指针与函数 什么是引用 指针引
数组 值传递 局部变量 字符串 内存空间