指针的错误赋值

来源:互联网 发布:蛮牛unity3d 编辑:程序博客网 时间:2024/05/17 23:23
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main()
{
    FILE * fp;
    char *line = NULL;
    char *dst = "kernel";
    char *word[8192];
    int rownum = 0;
    int wordid = 0;
    int status = 0;
    int i, j;
    size_t len = 0;
    fp = fopen("./cai.txt", "r");
    //fp = fopen("/var/log/messages", "r");
    for (i = 0; i <= 255; i++)
        word[i] = NULL;
    if (fp == NULL) {
        printf("打开文件\"/var/log/messages\"不存在或无权访问!\n");
        return 1;
    }
    while (getline(&line, &len, fp) != -1) {
        rownum++;
        wordid++;
        if(strstr(line, dst) != NULL) {
            for (j = 0; j <= wordid; j++) {
                if (line == word[j]) {
                    status++;
                }
            }
            if (status == 0) {
                word[wordid] = line;    //此处两个值都是指针 如果这么赋值的话 word[wordid]和line是同一个指针,如果将a指针的值付给b指针应该是*a=*b这样
                printf("3#This line is new:%d \t %s", rownum, line);
            }
            status = 0;
        }
    }
    return 0;
}
原创粉丝点击