#include<stdio.h>
#include<stdlib.h>
#define MAX(X,Y) ((X)>(Y)?(X):(Y))
int main()
{
int a = 10;
int b = 20;
int ret = MAX(a++,b++);//int ret = MAX(a++,b++) ((a++)>(b++)?(a++):(b++))
printf("a=%d,b=%d,ret=%d\n",a,b,ret);
system("pause");
return 0;
}
int main()
{
int *p=(int *)malloc(10*sizeof(int));
int i=0;
for(; i<10; i++)
{
p[i]=i;
}
for(i=0; i<10; i++)
{
printf("%d ",p[i]);
}
free(p);
system("pause");
return 0;
}
int i=0;
int *p=(int *)malloc(10*sizeof(int));
if(p == NULL)
{
printf("put of memory!\n");
exit(1); //结束程序
}
而利用宏来开辟内存就会省掉这些麻烦:
#define MALLOC(COUNT,TYPE) (TYPE*)alloc(COUNT*sizeof(TYPE))
void *alloc(int sz)
{
void *p=malloc(sz);
if(p == NULL)
{
printf("out of memory!\n");
}
return p;
}
int main()
{
int *p=MALLOC(10,int);
int i=0;
for(; i<10; i++)
{
p[i]=i;
}
for(i=0; i<10; i++)
{
printf("%d ",p[i]);
}
free(p); //动态开辟后一定要释放这些内存,否则会发生内存泄漏
system("pause");
return 0;
}
#define MAX(X,Y) ((X)>(Y)?(X):(Y))
#define M 100
int main()
{
int ret = MAX(MAX(M,20),200); //这是嵌套使用,不是递归
return 0;
}