Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C function help...

Options
  • 08-03-2006 8:55pm
    #1
    Registered Users Posts: 26,578 ✭✭✭✭


    how's it going lads, i'm having a little problem understanding the in's and out's of functions in C.

    basically what i want to do is...
    1. scan in 10 numbers into an array within a function
    2. return these ten values into main()
    3. store in another array of equally size in main().

    this is what i have and it's probably wrong...
    main()
    {
          int function(void) ;
          
          int array[10] ;
          int *pointer ; 
          int i ;
          
          pointer = array ; 
          
          for(i = 0 ; i < 10 ; i++);
          {
                 *pointer = function() ;
                 printf("%d ", *(pointer+i)) ; 
          }
          
    }
    
    int function (void)
    {
        int a[10] ; 
        int i ; 
        int *ptr ; 
        
        for (i = 0 ; i < 10 ; i++)        
        {
            scanf("%d", (ptr+i)) ;
        }
        
        for (i = 0 ; i < 10 ; i++)
        {
            return (*(ptr+i)) ; 
        }
    }
    

    any help would be greatly appreciated.


Comments

  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Ok. Theres so much wrong, i've just commented some of the major points. If you don't understand why i've changed something, ask.
    //prototypes declared outside of main function
    int function(void) ;
    
    main()
    {
          int array[10] ;
          int *pointer ; 
          int i ;
          pointer = array;
    
          function(array); // pass in the base address of the array you want to scan the data into.
    
          for(i = 0 ; i < 10 ; i++);
          {
                // *pointer = function() ;  WRONG :p you set pointer correctly a few lines above.
                 printf("%d ", *(pointer+i)) ; 
          }
    
    
    void function (int array[])// could also be: int function(int *array). same thing
    {
        int i ; 
        int *ptr ;
        ptr = array; //sets your  pointer pointing at the base of the array.    
    
        for (i = 0 ; i < 10 ; i++)
        {
            scanf("%d", (ptr+i)) ; //right
        }
        return; 
    }
    
    In your original for loop, you shouldn't be calling "function" each time. It should only be called once. And you should pass it the address of the array you want to store the information to. Then, you scanf the info into your array, and can just print it out as you were doing.
    for (i = 0 ; i < 10 ; i++)
        {
            return (*(ptr+i)) ; 
        }
    }
    
    You do realise that "return" means that function ends immediately. Therefore it would only ever return ONE integer value, the value at position 0 of the array.


  • Registered Users Posts: 26,578 ✭✭✭✭Creamy Goodness


    //prototypes declared outside of main function
    int function(void) ;
    

    i knew that but i had copied my file over as my lecturer for some reason wants functions declared inside main ???
    function(array); // pass in the base address of the array you want to scan the data into.
    

    so would that be like this...
    function (int &array); // or would it be function (int pointer)
    
    You do realise that "return" means that function ends immediately. Therefore it would only ever return ONE integer value, the value at position 0 of the array.

    kind of confused about this, so it so say i scan in the int 5 for the first number it will return 5 to a[0] and then exit out of the function so how do i get a[1], a[2], a[3] etc.

    these pointers are confusing as hell, but i gotta learn to get along with them :p


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Cremo wrote:
    i knew that but i had copied my file over as my lecturer for some reason wants functions declared inside main ???
    I'd be really surprised if it ever compiled... i know cygwin throws up a compile error if you try to declare a prototype within main. It just won't work.
    Cremo wrote:
    so would that be like this...
    function (int &array); // or would it be function (int pointer)
    
    Nope. An array is like a bookshelf. When you declare an array, you give it a size (i.e. int array[10], or you can dynamically assign space as needed using malloc). If you type array[1], or array[6] you are going to the integer at position 1, or position 6 ABOVE the base address of the array. I.e. when you type array[6], the compiler goes to the base address of the array, and counts up to the '6' position in the array (the seventh element).

    If you want to access the base address, you just write down the array's name. I.e. "array". The name of your array is like a pointer (in that it stores a memory address). If you want the address of one of its elements, you will need to write &array[1] or &array[7], as each position in the array is an INT. If you do add a printf to your code such as the following one, you'll see it'll print out the memory address of your array.
    printf("The base address of the array is: %d", array);// 'array' holds the base address
    printf("The address of position 0 is: %d", &array[1]);'array[1]' is an int, so to get the memory address of an int we need the ampersand sign.
    
    kind of confused about this, so it so say i scan in the int 5 for the first number it will return 5 to a[0] and then exit out of the function so how do i get a[1], a[2], a[3] etc.
    Right, the thing is you can't return the elements one by one like you wanted. What you want to do is return the entire array. I.e. you could make the function return an int*, and then return the address of the array. BUT this would throw up an error as you can't return local variables like that, as they get destroyed when the function ends.

    What i changed your code to do was the following:
    I made your code pass in the base address of your array (called "array") into "function", which then scanned the data into that array, so once that function finishes its' scanning, the data IS in your array (called "array"). Thats why i passed in your arrays memory address.

    Does that make sense to ya?


Advertisement