Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么在我输入值时,我的二维数组在索引处显示正确的值,而不是我稍后尝试访问的值?(C)_C_Arrays_Pointers_Dynamic Memory Allocation - Fatal编程技术网

为什么在我输入值时,我的二维数组在索引处显示正确的值,而不是我稍后尝试访问的值?(C)

为什么在我输入值时,我的二维数组在索引处显示正确的值,而不是我稍后尝试访问的值?(C),c,arrays,pointers,dynamic-memory-allocation,C,Arrays,Pointers,Dynamic Memory Allocation,我正在编写一个程序,它读取一个linux影子文件,将每一行划分为一个ID、一个salt和一个加密密码的哈希。我为这些值中的每一个创建了一个2-d字符数组,并在遍历每一行时用这些值填充它。当我在数组中输入这些值时,我将其打印出来,以显示它正在输入正确的值,如下所示: printf(id:%s,salt:%s),id[num\u账户],salt[num\u账户]; 结果如下: id: root, salt: NrF46O1p id: seed, salt: wDRrWCQz id: user1, s

我正在编写一个程序,它读取一个linux影子文件,将每一行划分为一个ID、一个salt和一个加密密码的哈希。我为这些值中的每一个创建了一个2-d字符数组,并在遍历每一行时用这些值填充它。当我在数组中输入这些值时,我将其打印出来,以显示它正在输入正确的值,如下所示:

printf(id:%s,salt:%s),id[num\u账户],salt[num\u账户];

结果如下:

id: root, salt: NrF46O1p
id: seed, salt: wDRrWCQz
id: user1, salt: LGOwUL7Q
id: user2, salt: CL5Fr2bN
id: user3, salt: Un/lqxkl
id: user4, salt: Lx2zrG31
id: user5, salt: 6R1eYOtL
salt 0:
salt 1:
salt 2: 6R1eYOtL
salt 3: 6R1eYOtL
salt 4: 6R1eYOtL
salt 5: 6R1eYOtL
salt 6: 6R1eYOtL
salt 7: (null)
这是在输入每个值时完成的。输入所有值并且程序退出while循环后,将有另一个for循环打印出所有值(用于调试目的)

以下是我分配二维阵列并填充阵列的代码:

char **ids;
char **hashes;
char **salts;

hashes = malloc((num_accounts + 1) * sizeof(char*));
salts = malloc((num_accounts + 1) * sizeof(char*));
ids = malloc((num_accounts + 1) * sizeof(char*));

for(int i = 0; i < num_accounts; i++){
     hashes[i] = malloc(88);
     salts[i] = malloc(8);
     ids[i] = malloc(15);
}

shadow = fopen("shadow", "r");
num_accounts = 0;
while(fgets(shdw_line, SHDW_LINE_LEN, shadow)!=NULL){
     char *token = strtok(shdw_line, ":");
     char *shdw_hash = strtok(NULL, ":");
     if(strcmp(shdw_hash, "*")!=0 && strcmp(shdw_hash, "!")!=0){
        ids[num_accounts] = token;
        token = strtok(shdw_hash, "$");
        token = strtok(NULL, "$");
        salts[num_accounts] = token;
        token = strtok(NULL, "$");
        hashes[num_accounts] = token;
        printf("%d: id: %s, salt: %s\n", num_accounts, ids[num_accounts], salts[num_accounts]);
        num_accounts++;
     }
}

char**id;
字符**散列;
炭**盐;
哈希=malloc((num_accounts+1)*sizeof(char*);
盐类=malloc((账户数量+1)*sizeof(char*);
ids=malloc((num_账户+1)*sizeof(char*);
对于(int i=0;i
我想知道为什么数组会被最后输入的值覆盖?

salts[I]=malloc(8)

改为

salts[i]=malloc(9);//1表示字符串的标志结尾及其基本背景 知识


salts[num_accounts]=token;//将为您提供源字符串的指针,这样您将得到相同的结果

改为


strcpy(salts[num_accounts],token);

谢谢。如果在旁边有正确的实现,那么它看起来很明显。
char **ids;
char **hashes;
char **salts;

hashes = malloc((num_accounts + 1) * sizeof(char*));
salts = malloc((num_accounts + 1) * sizeof(char*));
ids = malloc((num_accounts + 1) * sizeof(char*));

for(int i = 0; i < num_accounts; i++){
     hashes[i] = malloc(88);
     salts[i] = malloc(8);
     ids[i] = malloc(15);
}

shadow = fopen("shadow", "r");
num_accounts = 0;
while(fgets(shdw_line, SHDW_LINE_LEN, shadow)!=NULL){
     char *token = strtok(shdw_line, ":");
     char *shdw_hash = strtok(NULL, ":");
     if(strcmp(shdw_hash, "*")!=0 && strcmp(shdw_hash, "!")!=0){
        ids[num_accounts] = token;
        token = strtok(shdw_hash, "$");
        token = strtok(NULL, "$");
        salts[num_accounts] = token;
        token = strtok(NULL, "$");
        hashes[num_accounts] = token;
        printf("%d: id: %s, salt: %s\n", num_accounts, ids[num_accounts], salts[num_accounts]);
        num_accounts++;
     }
}