PMCL88
PMCL88

Reputation: 21

Returning integer array values from a function in c

I am really struggling on an assignment i have. I have searched the internet and youtube but i am still none the wiser. The program will have 5 functions in total, but i am stuck on the first. The program should use a 1-D array to read a 4 digit code(must be 4 single digit numbers) entered by the user. My problem arises when i am trying to return that code from the function. All i am getting is the first number. I am aware that you cannot return an array from a function in c and that you have to use pass by reference, this is where i have a problem i do not completely understand how to do this. my code is below along with the output i recieve.

Any help you can give me would be much appreciated, as ive said before i am really struggling.

//program to enter a code and return the code to main

#include <stdio.h>
#include <stdlib.h>
#define CODE 4

//function prototypes
int enter_code(int* code_arr);

main()
{
    int code =0;
    int option;
    int exit1=0;


    do
    {

    //print the menu on screen
    printf("\t \t \t1 - Enter the access code\n");
    printf("\t \t \t2 - Encrypt code and verify\n");
    printf("\t \t \t3 - Exit the program \n");

    scanf("%d",& option);

    switch(option)
    {
        case 1:
            {
                //call enter_code function
                code= enter_code(&code);
                printf("\n The returned code is %d \n",code);

                break;
            }

        case 2:
            {
                break;

            }

        case 3:
                {
                    // prompt user to a key to exit
                    printf("\n You choose to exit the program.\n Press a key to exit\n "); 
                    getchar();
                    exit(0);

                    break;

                } 

        default:
            {
                printf("You must enter a number between 1-5\n");
            }


}

    }//end do()

    while(exit1!=5 & exit1 <6);


}//end main


int enter_code (int* code_arr)
{
    int password[CODE];

    int i;

    printf("Enter your 4 digit code \n");
    for(i=0;i<CODE;i++)
    {
        scanf("%d",&password[i]);
    }

    printf("The code entered is:");
    for(i=0;i<CODE;i++)
    {
        printf("%d",password[i]);
    }

    return(*password); //how do i return the full array

}

Upvotes: 0

Views: 74

Answers (1)

Weather Vane
Weather Vane

Reputation: 34585

Your function can return the code through the array passed as an argument, and use the function return value to indicate an error. You can pass that to another function too. Your simplified code:

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

#define CODE 4

int enter_code (int* code_arr)
{
    int i;
    printf("Enter your 4 digit code\n");
    for(i=0;i<CODE;i++)
        if (scanf("%d", &code_arr[i]) != 1)
            return 0;
    return 1;
}

int check_code (int* pass_code, int* user_code)
{
    int i;
    for(i=0;i<CODE;i++)
        if (pass_code[i] != user_code[i])
            return 0;
    return 1;
}

int main(void)
{
    int password[CODE] = {0}, passOK[CODE] = {42,24,0,12345678};
    if (!enter_code(password))
        printf ("Bad password entry\n");
    else {
        if (check_code(passOK, password))
            printf("You unlocked the vault\n");
        else
            printf("You don't know the passcode\n");
    }
    return 0;
}

Program output:

Enter your 4 digit code
42
24
0
12345678
You unlocked the vault

Upvotes: 1

Related Questions