return a value from a function

This is a discussion on return a value from a function within the C Programming forums, part of the General Programming Boards category; Hi all i am new to programming and after i learnt Pascal i try to learn C.I have working to ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    3

    return a value from a function

    Hi all i am new to programming and after i learnt Pascal i try to learn C.I have working to this program and from the pinakas function i want to return to main the first negative value of the array or if there isn't a negative value to return the first value of the array.Here is the code
    Code:
    #include<stdio.h>
    #define n  5
    int *pinakas(int *pin);
    main()
    { 
         int *ptr,pin[n],i;
    
        //fill the array
        printf("Dwse ta stoixeia toy pinaka :\n");
        for(i=0;i<n;i++)
             scanf("%d",&pin[i]);
        ptr=pinakas(pin);
        printf("%d\n",*ptr);
    }
    
    int *pinakas(int *pin)
    {
       int k;
    
       for (k=0;k<n;k++)
      {
           if(*(pin+k)<0)
                 return pin+k;
           else
                return pin;   
      }
    }
    This code return me always the first value of the array and not the first negative value if there is.
    Any help?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > return pin;
    Should be outside your for loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    At the moment, you return pin if the first element of the array is not negative. This means you always return the first element. You need to move the return pin to after the end of the loop when all the elements of the array have been checked.

    Code:
    int *pinakas(int *pin)
    {
       int k;
    
       for (k = 0;k < n;k++)
       {
           /* pin[k] is the much less ugly equivalent of *(pin+k) */
           if(pin[k] < 0)
                 return &pin[k];
       }
    
       /* Only return the first element once all elements have been checked. */
       return pin;   
    }

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    main() ???
    make it int main() ;
    main should return a value .
    also, inside the function, the code should be
    Code:
     int * foobar(int *pin)
    {
     int k;
     for (k=0;k<n;k++)
         {
                 if(*(pin+k)<0)
                 return pin+k;
         }
    return (pin) ;
    }
    what your function is doing is just checking once and returning the value. it should check the entire array.
    PING
    Last edited by PING; 01-21-2005 at 07:43 AM.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  5. #5
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    Someone will probably respond before i'm done, but here's my reply anyway :


    I think you should work work with subscripts in your program rather than pointers.

    I don't usually make programs for people who need help , but i had to remake your program to show you an easier way to do it (along with comments) :

    Code:
    #include<stdio.h>
    
    #define N 5   // use captial letters for #define constants
                         // to seperate them from the variables you declare
                        // within your program.
    
    int foo(const int *);   // use const to prevent altering your
                                     // array's contents
    
    int main(void) // main should ALWAYS return an int
    {
    	int arr[5]; // array
    	int i;  // subscript (or array index)
    
    	printf("Enter 5 numbers : \n");
    
    	for(i=0; i<N; i++)
    		scanf("%d", &arr[i]);
    
    	i = foo(arr);   // send the adress of the first element
                                         // of "arr" to foo
    
    	printf("\n%d\n", arr[i]); // print the value found (or first
                                                         // element if not found)
    
    
    	return 0; // this means program terminated sucessfuly
    }
    
    
    
    int foo(const int *arrP)
    {
    	int k; // another subscript
    
    // we treated our pointer just like an array.
    	for(k=0; k<N; k++)
    // if an element is less than zero , return its location
    		if(arrP[k] < 0)
    			return k;
    
    // or else return the location of the first element
    	return 0;
    }

    there you go.


    I didn't compile your program , but i guess the reason it returns a pointer to the first element is this piece of code :
    Code:
    if(*(pin+k)<0)
                 return pin+k;
           else
                return pin;
    this means if any element is NOT less than zero then return a pointer to the first element. So if your second element had a positive value for example, it would ignore the 'if' statement and go to the 'else' wich returns the first element's location. This is why i seperated the last 'return' from the loop.



    Hope this helps


    *EDIT* you guys were too fast
    Last edited by Brain Cell; 01-21-2005 at 07:10 AM.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21