I want to make a dynamic array. but when I view the array, I think there's something wrong with the index part. Please help. Thanks!
here's my code:
#include<stdio.h>
main(){
int* a;
int number;
int choice;
int index = 0;
int head;
while(choice!=4){
printf("\tMENU\t\n");
printf("[1] add number to array\n");
printf("[2] view array\n");
printf("[3] search using linear search\n");
printf("[4] quit program\n");
scanf("%d", &choice);
switch(choice){
case 1: head = add(&index, a);
break;
case 2:head = view(&index,a);
break;
//case 3:linsearch(&index,array[5]);
//break;
case 4: return;
break;
default: printf("invalid input!!!\n");
}
}
}
int add(int* index, int* a){
int number;
int i;
printf("enter number you want to enter in the array.\n");
scanf("%d", &number);
*a = (int) malloc (5*sizeof(int));
for(i = 0; i <= *index; i ++){
a[i] = number;
}
return *index;
}
view(int* index, int* a){
int i;
printf("\n");
for(i = 0; i <= *index; i++){
printf("index: %d, %d ", *index, a[i]);
}
printf("\n\n");
return a[5];
}