Chinaunix首页 | 论坛 | 博客
  • 博客访问: 429042
  • 博文数量: 71
  • 博客积分: 26
  • 博客等级: 民兵
  • 技术积分: 1246
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-23 14:46
个人简介

linux --- 一切皆文件

文章分类

全部博文(71)

文章存档

2021年(1)

2019年(2)

2018年(4)

2017年(7)

2016年(11)

2015年(1)

2014年(2)

2013年(33)

2012年(10)

分类: C/C++

2013-05-20 22:17:06

1:寻找在t串中s串出现的位置,没有找到返回-1

#include

#define MAXLEN 10000
int mygetline(char line[],int maxlen);
int findstr(char line[],char old[]);

int main(void)
{
    int len,address,linenum = 1;
    char line[MAXLEN];
    char old[] = "old";
    while((len = mygetline(line,MAXLEN)) > 0){
        if((address = findstr(line,old)) > 0){
            printf("the first address find the str line:%d : %d \n",linenum,address);
            break;
        }
        linenum ++;
    }
    if(len <= 0)
        printf("can not find the string \n");

    return 0;
}

int mygetline(char line[],int maxlen)
{
    int i;
    int c;
    for(i = 0;i < maxlen - 2 && (c = getchar()) != EOF && c != '\n';i ++){
        line[i] = c;
    }

    if(c == '\n'){
        line[i] = c;
        i ++;
    }
    line[i] = '\0';
    return i;
}

int findstr(char line[],char old[])
{
    int i,j,k;
    for(i = 0;line[i] != '\n';i ++){
        for(j = i,k = 0;old[k] != '\0' && line[j] == old[k];k ++,j ++)
            ;
        if(k > 0 && old[k] == '\0'){
            return i;
            
        }
    }
    return -1;
}

2:static

(1)可以更改变量生存周期:
void fun(int i,int j)
{
    int count;        //这个count的作用域在这个函数中,在函数执行完之后count变量自动销毁
    static int count;        //加了static,这个count的作用域还是在这个函数中,但是count的生存周期不知是在这个函数中,
    //static int count的内存分配到堆上了,而不是放在栈中了
}

(2)修改函数作用域
    static void fun(int i,int j)
{
    …………
    return;
}
将这个函数的作用域限制在一个文件中,其他文件调用不到这个函数。

3:在函数外面定义的变量都是全局变量

全局变量可以被extern到别的文件里去。
比如:a.c 中有int a = 0;  b.c中要使用a这个变量 extern int a;(进行声明)

4:不要使用rigister,volatole在底层变成中也可能出问题。即使使用了编译器也会忽略掉,效果也不会很好。










阅读(1356) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~