您的位置:首页 > 其它

联合、枚举、双指针、void*、函数指针

2016-08-25 20:55 155 查看
1、联合

也是程序员自己创建的一种数据类型

#import <Foundation/Foundation.h>

union U
{
int x;
int y;
int z;
};

typedef union
{
char ch[4];
int x;
}Data;

int main(int argc, const char * argv[]) {
@autoreleasepool {
//联合中的所有成员变量共用一块存储空间
union U u1;
NSLog(@"%lu",sizeof(u1));
u1.x = 10;
NSLog(@"%d %d %d",u1.x,u1.y,u1.z);
u1.y = 20;
NSLog(@"%d %d %d",u1.x,u1.y,u1.z);

Data d;
d.x = 0x41424344;//小端存储
for (int i = 0; i< 4;i ++) {
printf("%c ",d.ch[i]);
}
printf("\n");
}
return 0;
}


2、枚举

也是程序员自己创建的一种数据类型

#import <Foundation/Foundation.h>

enum week
{
monday=1,tuesday,wedesday,thurday,friday,saturday,sunday
};

void show(enum week type)
{
switch (type) {
case monday:
NSLog(@"星期一");
break;

default:
break;
}
}

int main()
{
@autoreleasepool {
NSLog(@"%d",monday);
NSLog(@"%d",tuesday);
NSLog(@"%d",wedesday);
NSLog(@"%d",thurday);
NSLog(@"%d",friday);
NSLog(@"%d",saturday);
NSLog(@"%d",sunday);
}
return 0;
}


3、双指针

指针的指针

值传递的本质是在函数体内修改形参本身

地址传递的本质是在函数中修改形参指向的变量

#import <Foundation/Foundation.h>

void get (char *str)
{
str = "Hello World!";

}

void get1(char **str)
{
*str = "Hello World!";
}

int main()
{
@autoreleasepool {
int x = 10;
int* px = &x;
int** ppx = &px;
**ppx = 20;
NSLog(@"%d",x);

char *msg = nil;
get(msg);
printf("%s\n",msg);

get1(&msg);
printf("%s\n",msg);
}
return 0;
}


4、void*

万能指针,可以指向任意类型的变量

在万能指针引用其指向的变量时,需现将其转换回指向的变量类型

#import <Foundation/Foundation.h>
int main()
{
@autoreleasepool {
int x = 10;
int *px = &x;//同一类型的指针只能指向同一类型的变量
NSLog(@"%d",*px);

double *pd = &x;//不同类型的指针不能指向不同类型的变量
NSLog(@"%lg",*pd);

void *pv = &x;//void*被称为万能指针
double d = 3.14;
pv = &d;//void*类型的指针可以指向任意类型和的变量
NSLog(@"%lg",*(double*)pv);//万能指针在引用其指向的变量时需先将其转换回指向的变量的类型
}
return 0;
}


5、函数指针

函数名是函数第一条语句的(常量)地址

函数指针(变量)中保存的是函数名

可以用函数指针直接调用其所指向的函数

#import <Foundation/Foundation.h>

void fa()
{
NSLog(@"in function fa");
}

void fb()
{
NSLog(@"in function fb");
}

void invoke(void(*f)())
{
//...
f();//回调
//...
}

void fc(int x)
{
NSLog(@"in fc x = %d",x);
}

double fd(int x,double y)
{
return x + y;
}

double fe(int x,double y)
{
return x*y;
}

void invoke1(double(*f)(int,double))
{
NSLog(@"%lg",f(10, 3.14));
}

void invoke2(double(*f)(int,double),int x,double y)
{
NSLog(@"%lg",f(x, y));
}

int main()
{
@autoreleasepool {
NSLog(@"%p",fa);
void(*pf)();//定义一个函数指针pf,其数据类型是void(*)(),即pf指向没有返回值也没有形参的函数
pf = fa;
fa();//标准函数调用方法
pf();//使用指针调用函数
pf = fb;
pf();

invoke(fa);
invoke(fb);

void(*pc)(int);
pc = fc;
pc(3000000);

double(*pd)(int,double);
pd = fd;
NSLog(@"%lg",pd(2,3.2));

invoke2(fe,10,3.14);
invoke2(fd, 10, 3.14);
}
return 0;
}


6、堆内存管理

malloc函数:向堆空间申请变量或数组空间里面全部是垃圾值

free函数:将申请的变量或数组还回给堆

calloc函数:申请的堆空间会自动请零,其余和malloc全部一样

realloc函数

#import <Foundation/Foundation.h>
#define SIZE 5
#define SIZE2 3

typedef struct{
char name[10];
BOOL gender;
int age;
}Student;

int main()
{
@autoreleasepool {
int a = 10;
int* p = &a;
NSLog(@"%d",*p);

p = (int*)malloc(sizeof(int));//malloc函数向堆上申请一个整型变量
*p = 20;
NSLog(@"%d",*p);

free(p);//将申请的变量还回给堆,不会造成内存泄漏
float *pf;
double *pd;
char *pc;
pf = (float*)malloc(sizeof(float));
*pf = 3.14;
pd = (double*)malloc(sizeof(double));
*pd = 3.33333333333;
pc = (char*)malloc(sizeof(char));
*pc = 'A';
NSLog(@"*pf = %g *pd = %lg *pc = %c",*pf,*pd,*pc);
free(pf);
free(pd);
free(pc);

Student *pStu;
pStu = (Student*)malloc(sizeof(Student));
strcpy(pStu->name, "张三");
pStu->gender = YES;
pStu->age = 18;
printf("%s %s %d\n",pStu->name,pStu->gender?"男":"女",pStu->age);
free(pStu);

p = (int*)malloc(sizeof(int) * 5);//申请了一个有5个元素的数组
for (int i = 0; i < SIZE; i ++) {
p[i] = (i + 1) * 10;
}
for (int i = 0; i < SIZE; i++) {
printf("%d ",p[i]);
}
printf("\n");
free(p);

Student *ps;
ps = (Student*)malloc(sizeof(Student) * 5);
free(ps);

}
return 0;
}


思考练习:

从键盘任意输入字符,字符个数任意。

|

|

|

|

|

|

|

|

|

|

|

|

|

|

|

|

|

|

解析:

#import <Foundation/Foundation.h>
int main()
{
@autoreleasepool {
int *p = (int *)malloc(sizeof(int) * 5);
for (int i = 0; i < 5; i ++) {
p[i] = (i + 1) * 10;
}
p = (int*)realloc(p, sizeof(int)*10);
for (int i = 5; i < 10; i ++) {
p[i] = (i + 1) * 10;
}
for (int i = 0; i < 10; i ++) {
printf("%d ",p[i]);
}
printf("\n");
free(p);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息