Dom*_*ski 1 c linux compare strcmp
我正在为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(¤t_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;
}
Run Code Online (Sandbox Code Playgroud)
我想这样做(正确运作):
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
Run Code Online (Sandbox Code Playgroud)
但是,执行后我得到这样的东西:
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
Run Code Online (Sandbox Code Playgroud)
.
可能有什么不对?问候K.
没有读过你的所有代码,但这让我感到困惑:
while(*array != '\0') {
Run Code Online (Sandbox Code Playgroud)
数组是一个**char.这意味着*数组是一个*char.但是你将它与char('\0')进行比较.你可能意味着NULL?
现在你的错误:
*array=str;
Run Code Online (Sandbox Code Playgroud)
这是坏线.你没有复制字符串!你只是指着内存中的同一个地方!
这意味着如果更改指向str的字符串,指向的字符串*array也会改变!
所以一旦你添加一个字符串ut_name然后再改变ut_name它并再添加它 - 它已经找到了它,因为你拥有相同的内存ut_name和*array.
无论如何 - 使用strcpy(分配内存!!!)而不是那条线*array=str,一切都会正常工作.