Thread: Input values into array using function

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    63

    Question Input values into array using function

    Original Instructions: "Write a C function that will input values into an array of type int. Have the array and the number of elements in the array passed as parameters to your function. Use a local pointer variable to traverse the array. "

    So this is what I've come up with, was wondering if someone could have a look at it because I wasn't quite clear on the instructions exactly. Maybe if you've dealt with this sort of problem before you can tell me if I'm on the right track, the second part of the problem will be to then write a function that outputs the contents of the array using a function.

    Code:
    /*
    
    My name is Jack Trocinski
    
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    void input(int *ptr, int j);
    
    int main()
    {
    	int j; // number of elements in array
    	int *ptr; // memory allocation
    	
    	printf("Please enter the amount of elements you wish to have in the array: ");
    	scanf("%d", &j);
    	
    	if (j > 0) {
    		input (ptr, j);
    	}
    	
    	else printf("Sorry, you have entered an invalid number.\n");
    	
    	system("pause");
    	return 0;
    }
    
    void input(int *ptr, int j) {
    
    int i; // used in for loop
    
    ptr = malloc( j * sizeof(*ptr) );
    		for ( i = 0; i < j; ++i) {
    			printf("Input a value to the array and press enter: ");
    			scanf("%d", &ptr[i]);
    			}
    }
    Everything compiles nicely, my problem is strictly did I get the right concept writing this type of code.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Looks alright. Except that the array is not accessible once you leave the input() function. Say you want to continue processing by calling another function after that. The input() function did not communicate the malloc'ed address back. Otherwise the code does exactly what was asked.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    I see so it needs to return a value. Ok, I'll will play around with this.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Either return the value, or make the passed parameter such that the function can pass it back through there, i.e. address-of. This may be more advanced than what you've learned... maybe not.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Code review
    By bennywhere in forum C Programming
    Replies: 16
    Last Post: 10-20-2009, 09:00 PM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM