c - 指向字符数组指针的指针

标签 c arrays pointers char

我的函数passwords采用char**作为输入。我需要该函数将特定字符放置在某个位置。我的程序崩溃了,我不确定我做错了什么,但我已经将范围缩小到指针:

// The function
int passwords(int num, int line, char** letters, char** ptrPassword, int length) {
    char* password = *ptrPassword;

    // Later in the code
    password[location] = letters[line][0];
}

这是我从 main 发出的电话:

char** password;
password = malloc(length * sizeof(char));
location = length;

//call function
printf("Password at %d is %s \n", rank, passwords(rank, 0, letters, password, length));

我对指针不太有经验,有人可以帮忙吗?

主要:

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

int passwords(int num, int line, char** letters, char** ptrPassword, int length);
int lengthOfString(char* string);
void print(char** letters, int length);

int location;

int main(){
    int numCase;
    scanf("%d", &numCase);

    int i, length;
    for(i = 0; i < numCase; i++){
        scanf("%d", &length);
        int j;
        char** letters = malloc(length*sizeof(char*));

        for(j = 0; j < length; j++){
            letters[j] = calloc(26, sizeof(char));
            scanf("%s", letters[j]);
        }
        int rank;
        scanf("%d", &rank);

        //print(letters, j);


        char* password;
        password = malloc(length * sizeof(char));
        location = length;

        //call recursion
        printf("Password at %d is %s \n", rank, passwords(rank, 0, letters, &password, length));
    }

    return 0;
}

整个函数:

//recursive function
int passwords(int num, int line, char** letters, char** ptrPassword, int length){
    char* password = *ptrPassword;
    printf("Recursion #%d \n", line);
    if(line == length-1){
        printf("Line is equal to length \n");
        if(num > lengthOfString(letters[line])){
            printf("IF \n");
            if(num % lengthOfString(letters[line]) == 0){
                password[location] = letters[line][lengthOfString(letters[line])];
            }
            else{
                password[location] = letters[line][num % lengthOfString(letters[line]) - 1];
            }
            printf("location: %d \n", location);
            location--;
            printf("Password is: %s \n", password);
        }
        else{
            printf("ELSE \n");
            if(num / lengthOfString(letters[line]) == 1){
                *password[location] = letters[line][0];
            }
            else{
                printf("Alocation: %d \n", location);
                password[location] = letters[line][num / lengthOfString(letters[line])];
            }
            printf("Blocation: %d \n", location);
            location--;
            printf("Password is: %s \n", password);
        }
        return lengthOfString(letters[line]);
    }
    else{
        printf("Line is not equal to length \n");
        int scalar = passwords(num, ++line, letters, ptrPassword, length);
        if (num > scalar){
            if(num % scalar == 0){
                password[location] = letters[line][lengthOfString(letters[line])];
            }
            else{
                password[location] = letters[line][num % scalar - 1];
            }
            location--;
        }
        else{
            if(num / scalar == 1){
                password[location] = letters[line][0];
            }
            else{
                password[location] = letters[line][num / lengthOfString(letters[line])];
            }
            location--;
        }
        return scalar * lengthOfString(letters[line]);
    }
}

最佳答案

更改此行

printf("Password at %d is %s \n", rank, passwords(rank, 0, letters, &password, length));

printf("Password at %d is %d \n", rank, passwords(rank, 0, letters, &password, length));

因为您的 passwords 函数返回一个整数,而不是字符串 (char *)。从编译器警告中也可以明显看出这一点。

关于c - 指向字符数组指针的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42430815/

相关文章:

c - 如何使用 Avro C 库解码从 Kafka 读取的二进制文件?

c - 如何从 Newlib 在 GCC 中实现 printf?

c - 使用 C 返回数组

c - 将指针移动 1 个结构 block

c++ - 具有模板化成员的类的地址出现奇怪的错误

c - 这个小 for 循环中的内存泄漏和 valgrind 错误?

c - 从 POSIX 队列接收消息时如何节省内存?

javascript - 如何循环遍历数字数组以查看哪些数字与另一个数字相关?

php - 需要 php pdo 内爆数组并在 mysql 中插入多行

c - 如何用指针交换项目