1

我正在为 Linux 编写一个简单的程序。我有一个功能“addonlyonce”的问题。它比较字符串并添加到没有重复的数组中(如果字符串不存在于数组中)。该函数运行良好,但不适用于 utmp 结构。比较 utmp 结构的每个字符串时,总是返回 1(这意味着该字符串已经存在于数组中 - 这是错误的 :( )。您可以通过以下方式编译此代码gcc thiscode.c -o test(它仅适用于 Linux)。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <utmp.h>

int addonlyonce(char **array, char *str) {
    int i=0;
    printf("I got %s\n",str);
    while(*array != '\0') {
        if(strcmp(*array,str)==0) {
            printf("Already exists!\n");
            return 1;
        }
        i++;
        array++;
    }
    *array=str;
    printf("Sit down on the number: %d\n",i);
    return 0;
}

void printarray(char **array) {
    printf("Array looks like: ");
    while(*array != '\0') {
        printf("%s ",*array);
        array++;
    }
    printf("\n");
}

int main(void) {
    char *users[20]={0};
        char exuser2[]="exuser2";
    struct utmp current_record;
    FILE *fp = fopen(UTMP_FILE, "r");

    addonlyonce(users,"exuser1");
    printarray(users);
    addonlyonce(users,exuser2);
    printarray(users);
    addonlyonce(users,"exuser1");
    printarray(users);
    addonlyonce(users,"exuser3");
    printarray(users);
    while (fread(&current_record, sizeof(struct utmp), 1, fp) == 1) {
        addonlyonce(users,current_record.ut_name); //HERE DON'T WORK
        printarray(users);
    }
    addonlyonce(users,"exuser4");
    printarray(users);
    fclose(fp);
    return 0;
}

我想这样做(正确运行):

I got exuser1
Sit down on the number: 0
Array looks like: exuser1 
I got exuser2
Sit down on the number: 1
Array looks like: exuser1 exuser2 
I got exuser1
Already exists!
Array looks like: exuser1 exuser2 
I got exuser3
Sit down on the number: 2
Array looks like: exuser1 exuser2 exuser3 
I got reboot
Sit down on the number: 3
Array looks like: exuser1 exuser2 exuser3 reboot 
I got kelloco2
Sit down on the number: 4
Array looks like: exuser1 exuser2 exuser3 reboot kelloco2 
I got przemek
Sit down on the number: 5
Array looks like: exuser1 exuser2 exuser3 reboot kelloco2 przemek
I got guest
Sit down on the number: 6
Array looks like: exuser1 exuser2 exuser3 reboot kelloco2 przemek guest 
I got exuser4
Sit down on the number: 7
Array looks like: exuser1 exuser2 exuser3 reboot kelloco2 przemek guest exuser4

但是,执行后我得到这样的东西:

I got exuser1
Sit down on the number: 0
Array looks like: exuser1 
I got exuser2
Sit down on the number: 1
Array looks like: exuser1 exuser2 
I got exuser1
Already exists!
Array looks like: exuser1 exuser2 
I got exuser3
Sit down on the number: 2
Array looks like: exuser1 exuser2 exuser3 
I got reboot
Sit down on the number: 3
Array looks like: exuser1 exuser2 exuser3 reboot 
I got kelloco2
Already exists!   //Here a problem arises
Array looks like: exuser1 exuser2 exuser3 kelloco2 
I got przemek
Already exists!
Array looks like: exuser1 exuser2 exuser3 przemek 
I got guest
Already exists!
Array looks like: exuser1 exuser2 exuser3 guest 
I got exuser4
Sit down on the number: 4
Array looks like: exuser1 exuser2 exuser3 guest exuser4 

.在此处输入图像描述

有什么问题?问候 K。

4

1 回答 1

5

还没有阅读你所有的代码,但这让我很烦恼:

while(*array != '\0') {

数组是一个**char. 这意味着 *array 是一个*char. 但是您将其与 char ( '\0') 进行比较。你大概是说NULL

现在到你的错误:

*array=str;

这是坏线。你不是在复制字符串!你只是指向内存中的同一个地方!

这意味着如果你改变 指向的str字符串, 指向的字符串*array也会改变!

因此,一旦您使用添加一个字符串ut_name,然后更改ut_name并再次添加它 - 它已经找到它,因为您对ut_nameand具有相同的内存*array

无论如何 - 使用strcpy(在分配内存之后!!!)而不是那条线*array=str,一切都会正常工作。

于 2013-10-27T20:55:50.307 回答