学习一下,保证自己的基础,边学边记录是个很好的习惯,以后回头来看看,就马上能了解了
第一个程序是:C语言结构体指针用法,包括malloc和free的用法
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
struct People{
int age;
char *c;
};
int main()
{
//malloc函数返回的是一个void指针,如果要返回结构体指针,
//在malloc前面一定要加上(People*),这样是强制转换的意思
struct People *p = (People*)malloc(sizeof(struct People));
p->age = 10;
p->c = "C yu yan";
struct People *p1 = p; //p1指针是指向p的地址,这两个指针是
//指向同一块内存
p->age =12;
printf("%d\n",p1->age);
printf("%s\n",p1->c);
free(p); //一个内存地址只要释放一次就行了
system("pause"); //在控制台上停止运行
return 0; //一个函数遇到return 下面的语句就不运行了
}
#include "stdafx.h"
#include <iostream>
using namespace std;
void sayHello(int aa)
{
printf("Hello C %d\n",aa);
}
int main()
{
void (*p)(int); //函数指针的用法
p = sayHello;
p(100);
system("pause"); //在控制台上停止运行
return 0;
}