Thread: what is wrong with this simple prg??

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    8

    what is wrong with this simple prg??

    Hi, can anyone pointed out what did i do wrong, i was expecting a 10 and a 20 for the output..Thanks

    Code:
    //write in C this example
    
    #include <stdio.h>
    #include <stdlib.h>
    
    //function to double the value
    void doublemynum(int *num_pointer)
    {			
    	(*num_pointer) = (*num_pointer) * 2;
    	//return *num_pointer;
    }	
    
    int main(void)
    {
    	int num = 10;
    	doublemynum(&num);
    	
                    printf("The original value is %i\n",num);	
    	printf("The new doubled value is %i ",doublemynum);   
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Code:
    int main(void)
     {
     	int num = 10;	
             printf("The original value is %i\n", num);	
     	printf("The new doubled value is %i ", doublemynum(num));   
     	return 0;
     }
    Use this as your main function. It's just a problem with the way you were expecting to be able to store the result of a function in a variable of the same name as the function. IT doesn't work that way. You can either insert the functioncall exactly where you need the number (which is what I've done), or you can declare another int and set it equal to the function call. Then use that variable in place of "doublemynum" in your printf function.

    edit: In future, in addition to telling us what you expected as output, and what you actually got as output.

    edit: You'll probably also need to add another input statement before your return statement so that the program doesn't close the second you run it.
    Last edited by sean; 11-15-2004 at 09:39 AM.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    8
    Code:
    //write in C this example
    
    #include <stdio.h>
    #include <stdlib.h>
    
    //function to double the value
    void doublemynum(int *num_pointer)			//	(*num_pointer) gets the contents of num which is pointed to by the pointer
    {
    	(*num_pointer) = (*num_pointer) * 2;
    	//return *num_pointer;
    }	
    
    int main(void)
    {
    	int num = 10;
    	printf("The original value is %i\n",num);
    							
    	printf("The new doubled value is %i ",doublemynum(&num));   
    	return 0;
    }
    hi, thanks for the reply, but should it be
    Code:
    printf("The new doubled value is %i ", doublemynum(&num));   
    
    instead of 
    
    printf("The new doubled value is %i ", doublemynum(num));   
    
    because we need to reference the location for the variable num?
    But i have tried either way and there is a error saying "void value not ignored as it ought to be" why is this so?

    Lastly, do we have to put the return *num_pointer in the function??

    sorry i am getting used to the ideology of using pointers...

  4. #4
    </life>
    Join Date
    Oct 2004
    Posts
    83
    try

    Code:
    int doublemynum(int n) { return n*2; }
    Microsoft is merely an illusion, albeit a very persistant one.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    No. Pointers are simply variables that hold memory addresses. You pass that memory address to your function, your function directly changes what's at that location in memory, and returns void.

    I actually missed something - you can't use a function call in your printf statement because you're not returning any value. (Yes, you do need to pass &num to the function - you listed a pointer as a parameter, and & is pretty much the same as a pointer). What you need to do is input the number, call the function and list the address of that number as your response, than send the variable itself to the printf statement <- the function would've been able to change the variable itself because of pointers.

    dagdaryan: Yes, a better solution, but it sounds like the idea of this activity is to learn something about and get used to pointers, so that when you get to a situation that requires pointers, you know what to do.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    8

    thanks dagdarian

    Thanks mate,

    but i wanted to know to write using pointers method..thanks for ur advice anyway.

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Code:
    //write in C this example
    
    #include <stdio.h>
    #include <stdlib.h>
    
    //function to double the value
    void doublemynum(int *num_pointer)
    {			
    	(*num_pointer) = (*num_pointer) * 2;
    	//return *num_pointer;
    }	
    
    int main(void)
    {
    	int num = 10;
    	doublemynum(&num);
    	
                    printf("The original value is %i\n",num);	
    	printf("The new doubled value is %i ",doublemynum);   
    	return 0;
    }
    What you do:
    You set num= 10.
    You call doublemynum on the address of num.
    doublemynum has doubled the value in num to 20.
    num in main() is now 20.
    You print the content of num as original value (though it has already been doubled).
    >>> The original value is 20
    You print doublemynum. At least you try. Assuming you had written doublemynum(&num), you would still not print anything meaningful because doublemynum() has no return value.

    What you want to do
    You print num before calling doublemynum(&num).
    >>> The original value is 10 (\n)
    You call doublemynum(&num)
    and print num again
    >>> The new doubled value is 20 (and add a \n)
    Last edited by Nyda; 11-15-2004 at 10:04 AM.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  8. #8
    Registered User
    Join Date
    Nov 2004
    Posts
    8
    Ok i get it now, Thanks alot..

    Initially i thought that the contents of num in main will not be changed even after passing to the function.. but it did, i guess its because of the pointer stuff... i get it now..thanks ppl for all the help..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-31-2006, 01:54 AM
  2. simple 2d "putpixel" api for windows?
    By spiky in forum C Programming
    Replies: 2
    Last Post: 10-27-2005, 02:44 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Simple simple graphics
    By triplem in forum C Programming
    Replies: 2
    Last Post: 05-19-2003, 02:52 AM