Thread: Function returning pointers issues

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    33

    Function returning pointers issues

    Greeting
    I'm trying to write a single function that returns the average value and stores the count of the elements greater than the average value
    into the parameter named greater, as defined in the following prototype:

    float over_Avg(int v[], int n, int *greater);

    my code is the folowing
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    float avgVect(int v[], int n,int *greater)
    {
    int i;int count=0;
       int s=0;
       float average;
       greater=&count;
       for(i=0;i<n;++i)
        s=s+v[i];
        average =s/n;
         for(i=0;i<n;++i)
         {
             if (v[i]>average)
            count++;
    
    
         }
    
    
        return average;
    
    
    }
    int main ()
    {
      int v[] ={1,0,3};
    int *ptr;
      float resul;
      resul= avgVect(v,3,ptr);
      printf("%f %d",resul,(*ptr));
    }

    and as you might have understood it's not working so what's wrong?

    any hint would be highly appreciated!!

    Best

  2. #2
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    greater = &count.

    What's the scope of count??

    That's a hint. See if you can work it out.

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    33
    Thanks hobbit for your quick reply

    if i understood you well (please excuse my ignorance i'm still a newbie in C" there is no need for count, great should be enough

    here is my new code

    Code:
    float avgVect(int v[], int n,int *greater)
    {
    int i;
    *greater=0;
       int s=0;
       float average;
    
    
       for(i=0;i<n;++i)
        s=s+v[i];
        average =s/n;
         for(i=0;i<n;++i)
         {
             if (v[i]>average)
           {
               (*greater)++;
    
    
           }
         }
    
    
        return average;
    
    
    }
    int main ()
    {
      int v[] ={1,0,3};
    int *ptr;
      float resul;
      resul= avgVect(v,3,ptr);
      printf("%f %d",resul,(*ptr));
    }
    but it still doesn't work
    Last edited by amaelle; 12-05-2016 at 09:08 PM.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You are confused as to the idiom in question.
    Think about it.
    You passed in the address of greater ... and then overwrote it!
    You need to use the address of greater to store the value of count in that location.
    To write to the address you need to dereference it with the * operator:
    Code:
    *greater = count;
    Of course, that must go after you've determined what count is.

    Actually, it's worse than I originally thought. You need to call it like this:
    Code:
    int cnt;   // define an integer to hold the count
    float result = avgVect(v, 3, &cnt);   // pass in the address of cnt
    Last edited by algorism; 12-05-2016 at 09:09 PM.

  5. #5
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    OK let me explain a bit more.

    You have passed the address of a variable that no longer exists because it has gone out of scope back to main.

    Code:
    void func( int* ptr)
    {
       int count;
       ptr = &count;
    } // count dies here, it no longer exists, it has ceased to be, it has joined the choir invisible....

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Hobbit View Post
    OK let me explain a bit more.

    You have passed the address of a variable that no longer exists because it has gone out of scope back to main.

    Code:
    void func( int* ptr)
    {
       int count;
       ptr = &count;
    } // count dies here, it no longer exists, it has ceased to be, it has joined the choir invisible....
    He didn't "pass" anything at all. He simply set greater equal to the address of count. greater also stops existing at the end of the function.

  7. #7
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    Well yeah it's late. 3.15 am and upstairs is still partying. This is wrecking my job. Thank God I'm on forced holiday. No contract at the mo.

  8. #8
    Registered User
    Join Date
    Nov 2016
    Posts
    33
    Thank you Algorithm, i now got your point
    my actual now code looks like :

    Code:
    float avgVect(int v[], int n,int *greater)
    {
    int i;int count=0;
       int s=0;
       float average;
    
    
       for(i=0;i<n;++i)
        s=s+v[i];
        average =s/n;
         for(i=0;i<n;++i)
         {
             if (v[i]>average)
            count++;
         }
           *greater=count;
     return average;
    
    }
    int main ()
    {
      int v[] ={1,0,3};
    int count;
      float resul;
      resul= avgVect(v,3,&count);
      printf("%f %d",resul,count);
    }
    it works perfectly well!! thanks to you algorism as well as to you hobit!! your interventions are really awsome!!
    Last edited by amaelle; 12-05-2016 at 09:49 PM.

  9. #9
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    OK I took pity on you, you've had a go. Here's the fixed code.
    Code:
    #include <stdio.h>
    
    
    float avgVect(int v[], int n, int *greater)
    {
    	int i;
    	int s = 0;
    	float average;
    	*greater = 0;
    
    
    	for (i = 0; i<n; ++i)
    		s += v[i];
    	average = (float)s / n;
    	for (i = 0; i<n; ++i)
    	{
    		if (v[i]>average)
    			++*greater;
    	}
    	return average;
    
    
    }
    int main()
    {
    	int v[] = { 1,0,3 };
    	int count;
    	float resul;
    	resul = avgVect(v, 3, &count);
    	printf("%f %d", resul, count);
    	return 0;
    }
    count in your function was unnecessary, just zero *greater, and you were doing integer division so to avoid that we cast one of the numbers in the average calculation to a float. You were nearly there. The increment should have been ++*greater not ++count.

  10. #10
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    Oh you fixed it while I did and edited lol. Or did you? What's 4/3? Your code will say 1, mine will correctly say 1.333333
    Last edited by Hobbit; 12-05-2016 at 10:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function pointers with returning values
    By sammythesp3rmy in forum C Programming
    Replies: 4
    Last Post: 10-11-2013, 08:32 PM
  2. Returning results from a function using pointers
    By Magi in forum C Programming
    Replies: 4
    Last Post: 11-19-2012, 05:43 PM
  3. Function returning pointers
    By capricorn in forum C Programming
    Replies: 5
    Last Post: 12-16-2010, 08:04 AM
  4. Returning address of Function. Pointers to Functions.
    By edunia11 in forum C Programming
    Replies: 2
    Last Post: 12-04-2006, 12:28 PM
  5. returning pointers from a function
    By curlious in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2003, 11:37 PM

Tags for this Thread