0

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

}
1
  • 1
    You can't return local variables (and arrays) from function, because they are allocated on stack, so they are in stack frame of this function. After leaving the function, the stack pointer is moving to previous function frame and data from your previous function is not valid. You have 3 choices how to get it workt: 1. allocate array dynamically in your function (malloc) and return the pointer. 2. pass array from main to enter_code. 3. make your array static, but that's usually a bad choice. I'd say go with 2nd option, this way you can just pass address of local variable. Commented Mar 5, 2015 at 19:19

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

8 Comments

Hi thank you for such a quick reply, it works perfectly. One more question can i the code entered to another function?
Can you "pass the code to another function?" Yes, answer updated.
Thanks so much for that. I have been pulling my hair out for the past week probably trying the same code over and over. I now understand where i was going wrong. And i certainly wont be makng the same mistakes agin. Lesson learned.
One follow up question. Is it possible to make the function call from a switch statment as the assignment asks for a menu driven programme. With 1. Enter the code 2. Encrypt the code 3. Exit the programme.
I have but when i have put in your code, it seems to call the function twice. As in it asks me to enter 4 digits twice. And my program has also stopped looping back to the menu once the user is done entering the code. Maybe i wasn't very clear with my question the user should enter a 4 digit code in one case and then check that code against a set 4 digit code in the second case. apologies if i am confusing you.
|

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.