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:



	int* a;
	int number;
	int choice;
	int index = 0;
	int head;
		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);
			case 1: head = add(&index, a);
			case 2:head = view(&index,a);
			//case 3:linsearch(&index,array[5]);
			case 4: return;
			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; 
		for(i = 0; i <= *index; i++){
			printf("index: %d, %d ", *index, a[i]);
		return a[5];

Function add() -- second parameter must also be passed by reference. You are just passing the pointer by value, which does nothing in main()'s copy of the pointer. Note the second parameter should have two stars, not one.

int add(int* index, int **a)


lint 49: The typecast is wrong so you might as well just remove it because malloc() does not need to be typecast in C programs.

Edited 7 Years Ago by Ancient Dragon: n/a

thanks a lot!
my program finally worked.

i changed the ad part to:

if(*index < 5){
			printf("enter number you want to enter in the array.\n");
			scanf("%d", &a[*index]);
This question has already been answered. Start a new discussion instead.