'C program How to get return value from other function by using switch statement

So I am trying to do a program where it shows a menu and asks what will the user do. I am stuck in where I cant get the return value in case 1 and use it in case 5. I am only a beginner and need help.

This is what I have in my program.

#include <stdio.h>

int choice, arrayval, val;
char reply, y, n;

void menu()
{
printf("\n\n");
printf("MAIN MENU");
printf("\n\n");
printf("[1] - Store/Fill Array\n");
printf("[2] - Find and Replace\n");
printf("[3] - Display Frequency\n");
printf("[4] - Unique\n");
printf("[5] - Print\n");
printf("[0] - Print\n");
printf("Enter your choice: ");
scanf(" %s", &choice);
}


int storearray()
{
int arrayval[10];
int i;

for(i=0;i<10;i++)
{
    printf("Enter Value for Array[%d]", i);
    scanf("%d", &arrayval[i]);
}

return arrayval;            
}

void printarr(arrayval)
{
int i;
for(i=0;i<10;i++) 
{
    printf("Array[%d] %d\n", i, arrayval);
}
}

int main()
{
do
{
menu();
switch(choice)
{
case '1':
    storearray();
    main();         
    break;
case '5':
    val = storearray();
    printarr(val);
    return 0;
    break;

default:
    printf("Invalid");
}

}
while (choice != 1,2,3,4,5);

printf("End of Program!");
return 0;
}

need help on how to get the value from storearray() and use it to print in printarr()

c


Solution 1:[1]

Here's a modified version of your code with the fixes mentioned in the comments of your question:

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

char choice;
int *val;
char reply, y, n;

void menu()
{
    printf("\n\n");
    printf("MAIN MENU");
    printf("\n\n");
    printf("[1] - Store/Fill Array\n");
    printf("[2] - Find and Replace\n");
    printf("[3] - Display Frequency\n");
    printf("[4] - Unique\n");
    printf("[5] - Print\n");
    printf("[0] - Print\n");
    printf("Enter your choice: ");
    scanf(" %c", &choice);
}

int* storearray()
{
    int *arrayval=malloc(10*sizeof(int));
    int i;

    for (i = 0; i < 10; i++)
    {
        printf("Enter Value for Array[%d]", i);
        scanf("%d", &arrayval[i]);
    }

    return arrayval;
}

void printarr(int *arrayval)
{
    int i;
    for (i = 0; i < 10; i++)
    {
        printf("Array[%d] %d\n", i, arrayval[i]);
    }
}

int main(void){
    val=NULL;
    int inv=1;
    do
    {
        menu();
        switch (choice)
        {
        case '1':
            val=storearray(); //be sure to `free(val)` (if `val` is pointing to a malloc'ed memory) before you do `val=storearray()` or you'll lose the pointer and it'll result in a memory leak
            break;
        case '5':
            //I'd also recommend a check here to ensure `val` isn't NULL
            printarr(val);
            inv=0;
            break;
        default:
            printf("Invalid");
            inv=0;
            break;
        }
    } while (inv);
    printf("End of Program!");
    free(val);
    return 0;
}

Also, as chux mentioned in the comment:

"I am only a beginner and need help." --> Simple tip: enable all compiler warnings. Faster feedback than posting stack overflow.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1