0

My function passwords takes char** as input. I need the function to place a specific char at a location. My program crashes and I'm not sure what I'm doing wrong but I've narrowed it down to the pointer:

// 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];
}

Here is my call from 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));

I'm not very experience with pointers, could someone please assist?

Main:

#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;
}

The Entire Function:

//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]);
    }
}
5
  • Can you post your entire program? It's impossible to see what's wrong with just what you've posted here. Commented Feb 24, 2017 at 4:33
  • Did you use any debugger like gdb ? Please post more of your code. Commented Feb 24, 2017 at 4:44
  • What is the input for which you are getting a crash ? Your program has a compilation error as well, perhaps you meant password[location] instead of *password[location]. Commented Feb 24, 2017 at 4:56
  • password = malloc(length * sizeof(char)); -> password = malloc(length * sizeof(char*)); in case this is supposed to be an array of strings(?). A good rule to follow generally, is to never use any language mechanism unless you understand how it works and why it is needed. Do you understand why there is a need of pointer-to-pointers in your program? Commented Feb 24, 2017 at 9:04
  • Why is your code "here is my call from main" different than the code in your "main"? Plenty of bugs there. Which of the 2 buggy versions are you asking about? Commented Feb 24, 2017 at 9:06

1 Answer 1

1

Change this line

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

to

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

Since your passwords function returns an integer, not a string (char *). The same is evident from the compiler warning as well.

Sign up to request clarification or add additional context in comments.

1 Comment

He should also most definitely not pass a pointer to a pointer-to-pointer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.