Looking for a hosted bug/issue tracker? Try our hosted development tools.

Beginner C/C++

Moderators: Lundin
Number of threads: 4916
Number of posts: 15971

This Forum Only
Post New Thread

Report
dynamic array Posted by cmsc on 22 Nov 2009 at 3:14 AM
I don't know what's with the index part. Please help. Thanks!
#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];}#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];
}

Report
Re: dynamic array Posted by AsmGuru62 on 22 Nov 2009 at 6:50 AM
This code will not even compile! What exactly are you trying to do here? Way too many problems in code. What is "return a[5];" in a function which does not declare a return value type? And what are these mallocs() inside the add()?

If you need an array with exactly 5 numbers - simply declare it: "int a[5];" and only after that begin working with menus.

If you want to return allocated pointer from a function - it is done in two ways:

1. Simply return it as a return code:

int* NewArray (int size);

int* a = NewArray (5);

int* NewArray (int size)
{
    return (int*) malloc (size * sizeof (int));
}


2. Return it using pointer to a pointer:

int NewArray (int size, int** parray);

int* a = NULL;
int ok = NewArray (5, &a);

int NewArray (int size, int** parray)
{
    *parray = (int*) malloc (size * sizeof (int));
    return (*parray != NULL);
}



 
Popular resources and forums for programmers on Programmersheaven.com
Assembly, Basic, C, C#, C++, Delphi, Java, JavaScript, Pascal, Perl, PHP, Python, Ruby, Visual Basic
© Copyright 2009 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Publisher: Lars Hagelin. Read the latest words from the publisher here.
Be the first to sign up for Lars Hagelin’s In-depth Outsourcing Newsletter here.
bootstrapLabs Logo A BootstrapLabs project.