Chinaunix首页 | 论坛 | 博客
  • 博客访问: 428103
  • 博文数量: 103
  • 博客积分: 5010
  • 博客等级: 大校
  • 技术积分: 971
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-11 17:22
文章分类
文章存档

2008年(77)

2007年(26)

我的朋友

分类: C/C++

2008-05-30 10:34:04


1.声明变长数组
在VC中,对于数组的声明,只能是常量,而不能够是变量.而在gcc中如下代码却是可以成功运行的
代码:

#include <stdio.h>

int main(int argc, char **argv)
{
        int i, j;
//int a[i][j];----在scanf前定义是不可以的

        scanf("%d%d", &i, &j);
int a[i][j];
        printf("%d\n", sizeof(a));

        return 0;
}


2.为数组赋值
如果声明了一个100000长度的整形数组,需要将这个数组的最后一位赋为10,而其余位全部赋为0,如果在VC中,则需要使用循环的方式将前99999赋为0,然后再将最后一位赋为10,而使用gcc则可以很好地解决这个问题. 如下:

#include <stdio.h>

int main(void)
{
        int a[100] = {[98] = 99, [2] = 2};
        int i;

        for(i = 0; i < 100; i++){
                printf("%d ", a[i]);
        }
        printf("\n");

        return 0;
}

运行结果:

[xxxx@localhost test]$ ./a.out
0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 99 0


3.在C程序中在main()函数之前运行的函数
通过gcc的函数的属性,可以设置某些函数在main()函数之前运行.如:

#include <stdio.h>
void first() __attribute__((constructor));
void first()
{
        printf("this is function %s\n", __FUNCTION__);
        main();
        return;
}

int main(int argc, char **argv)
{
        printf("this is function %s\n", __FUNCTION__);
        return 0;
}

运行结果:

this is function first
this is function main
this is function main


通过设置函数属性为constuctor, 可使其在main()之前运行.
阅读(1119) | 评论(0) | 转发(0) |
0

上一篇:Poj 2159 计数排序

下一篇:scanf详解

给主人留下些什么吧!~~