Thread: Arrays and functions

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    4

    Arrays and functions

    Hello, im quite new to C so sorry if this seems tedious.

    Im trying to pass an array from a function back to main but it doesnt seem to work, it displays just zeros. Would be really grateful if someone could point me in the right direction.
    Thanks
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    
    float  get_values (void);
    float sum(float );
    void mean (void);
    void maximum_value(void);
    int i;
    main(void)
    {
          
         float v;
    
        v=  get_values();
        
         for(i = 1; i<=5; i++){
         printf("\tnumber %d = %f\n",i, v);
         }
          
          system("pause");
          
          return(0);
          
    }
    
    float  get_values(void)
    {
             
             float values[5];
             	
              printf("Enter 5 numbers\n");
              for( i = 1; i <= 5; ++i ){
    	scanf("%f", &values[i] );
    			
          }	
    	return values[i];
    		
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    In C, arrays are 0-based. That is, int array[5] would be indexed from array[0] to array[4]. Your function returns a value that is off the end of the array, not the whole array -- and not that you can return an array from a function anyway.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    You cannot return an array from a function, especially a local array, but then again, you're not attempting to in the code.

    I see several problems with your get_values() function; the reason that you're getting 0 all the time is that, when the for loop completes, the variable i has a value of 6, so you're attempting to return values[6], which is an error in itself since the array has only 5 elements, numbered from 0 through 4. Since the compiler is not inserting code to range check the array access, and since the array is declared as an "automatic", and is therefore on the stack, you're not getting an exception, but you're storing a value over top of whatever happens to have been on the stack following the last element of your array, and returning a float sized bit of data from the stack following that.

    I hope this helps put you on the right path.
    Last edited by filker0; 12-19-2005 at 03:26 PM. Reason: Clarification, fix grammar
    Insert obnoxious but pithy remark here

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    4
    cheers guys, so mistakes aside, it isnt possible to return the array anyway?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes and no. No, you can't return an array. Yes, you can return a pointer to a static array. You can also wrap the array in a structure, use that, and return that. Or, you could dynamically allocate an "array" and return it. But no, you can't just:
    Code:
    int [] function( void )
    {
        int arrray[ SOMESIZE ];
        ...
        return array;
    }
    Can't do that.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    4
    Thanks, i shall go and investigate pointers.

    mike

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by mikefountain
    cheers guys, so mistakes aside, it isnt possible to return the array anyway?
    Usually you pass a pointer to the first element and the size of the array.
    Code:
    float *get_values(float *values, int size)
    {
       int i;
       for ( i = 0; i < size; ++i )
       {
          /* ... */
       }
       return values;
    }
    Whether or not you choose to return anything is up to you.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    4
    Hi, im struggling to get my head around pointers - if anyone can give any more advice it would be most helpful, i really am lost

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Some discussion about it happened here -> http://cboard.cprogramming.com/showt...7&page=2&pp=15
    Do a search at the board, pointers can be a head ache but they are surely the most important feature of C everything is based on the way they work. Get a book of the C programming language if you can by Kernighan and Ritchie.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    One of the better tutorials on the net
    http://pw1.netcom.com/~tjensen/ptr/pointers.htm

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Salem
    One of the better tutorials on the net
    http://pw1.netcom.com/~tjensen/ptr/pointers.htm

    Great link Salem thanks a lot ive read it did the exercises and added to favorites.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays, functions, HELP
    By beginner1 in forum C Programming
    Replies: 4
    Last Post: 05-20-2009, 03:29 PM
  2. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 02:24 AM
  3. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  4. Arrays and Functions
    By KunoNoOni in forum Game Programming
    Replies: 12
    Last Post: 10-04-2005, 09:41 PM
  5. Arrays out-of-bounds in functions only?
    By KneeLess in forum C Programming
    Replies: 5
    Last Post: 11-03-2004, 06:46 PM