Thread: I have questions about returning pointer

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    45

    I have questions about returning pointer

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    float calculate(int *);
    
    
    int main(void) {
    	float result;
    	int number;
    	scanf("%d",&number);
    	result=calculate(number);
    	printf("%f",result);
    	return 0;
    }
    
    
    float calculate(int *rakam)
    {
    	int i;
    	float average=0;
    	int top=0;
    	if(*rakam>1000 && *rakam<9999)
    	{
    		while(rakam!=0)
    		{
    			i=*rakam%10;
    			*rakam=*rakam/10;
    			top=top+i;
    		}
    		average=top/4;
    		return average;
    	}
    	else
    	{
    		return *rakam;
    	}
    }
    It doesn't work. I don't understand.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why did rakam a pointer to begin with? It could just have been an int. Anyway, the problem is probably because you wrote:
    Code:
    while(rakam!=0)
    instead of:
    Code:
    while(*rakam!=0)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2014
    Posts
    45
    It's carelessness. Thank you.

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Your function 'calculate()' expects an pointer to an integer as argument.
    But on line 11 you pass an integer as parameter.
    Do you don't receive a warning from your compiler?
    You must use the 'address-of' operator to pass the address of that integer.
    Code:
    …
        result = calculate(&number);
    …
    Now, i see you return the integer 'rakam' on line 35 if the value don't comply your condition, but the function returns a float per definition.
    I think you should cast the integer to float.
    Code:
    …
        else return (float) *rakam;
    …
    Last edited by WoodSTokk; 01-15-2015 at 04:59 PM.
    Other have classes, we are class

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning a pointer
    By Canadian0469 in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2008, 04:06 AM
  2. returning this pointer
    By chottachatri in forum C++ Programming
    Replies: 4
    Last Post: 03-30-2008, 08:35 AM
  3. Pointer returning too big value
    By emanresu in forum C Programming
    Replies: 7
    Last Post: 01-08-2007, 09:54 AM
  4. returning a pointer
    By starX in forum C Programming
    Replies: 18
    Last Post: 08-23-2002, 05:35 AM
  5. returning a pointer
    By uler in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2001, 01:59 PM