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
}
malloc) and return the pointer. 2. pass array frommaintoenter_code. 3. make your arraystatic, but that's usually a bad choice. I'd say go with 2nd option, this way you can just pass address of local variable.