Reputation: 1
I'm very new to using pointers and arrays, and I'm trying to write a program that, depending on your inputted choice, would execute a function that allows me to either define the array size and content, or display my array if there is one. But currently I can't seem to run my display function. When I try to execute case 2, it just immediately print out "invalid" and return to my menu. It doesn't seem like it's updating my array size and I don't know how to fix it.
#include <stdio.h>
#define MAX 100
void input(int arr[], int* size);
void show(int arr[], int size);
int i = 0
int main()
{
int arr[MAX], size = 0, choice = 0;
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
input(&arr, &size);
break;
case 2:
if (size > 0)
{
show(arr, size);
}
else {
printf("invalid");
}
break;
}
}
void input(int arr[], int *size)
{
int n;
do {
scanf("%d", &n);
if (n < 0 || n > MAX)
{
printf("Invalid size, try again.\n");
scanf("%d", &n);
}
}while (n < 0 || n > MAX);
size = &n;
for (i; i < n; i++)
{
printf("Input value for array %d: \n", i + 1);
scanf("%d", &arr[i]);
}
}
void show(int arr[], int size)
{
for (i; i < size; i++)
{
printf("%d", arr[i]);
}
}
Upvotes: 0
Views: 80