Can someone explain this pgm?

Discussion in 'C' started by prakashkumar41, Jul 2, 2011.

  1. prakashkumar41

    prakashkumar41 New Member

    Joined:
    Sep 29, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include <stdio.h>
    
    int main ()
    {
    	 int x=15;
    	 int y=7;
    	 int z=8;
    	int newnum;
    	newnum = func( &x, &y, &z);
    	printf("NewNumber:%d X:%d Y:%d Z:%d\n",newnum,x,y,z);
    	getchar();
    	return 0;
    	
    }
    
    int func(int *x,int *y,int *z)
    {
    	int number;
    	*x = 15 + *x;
    	*y = 28 - *y;
    	*z = 8 *  *z;
    	number = *x + *y + *z;
    	return number;
    }
    
    int this program it returns the value after computing the operations which i declared inside the function.what if i am not using the pointers?...i got only the actual value declared in main function if i am not using the pointers.but logically if i call the function it should return the values which is inside the function blocks.....right?..

    Can someone please explain how it is been executed?...


    thanks in advance
     
    Last edited by a moderator: Jul 2, 2011
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    I'm not clear exactly what you want to know. What output do you get and what are you expecting? If you don't use pointers, what output do you get and what output did you expect?

    Don't forget the difference between call by value and call by reference. C is always "call by value", which is why pointers are needed when you want a function to modify something that is passed to it.
     
  3. ManzZup

    ManzZup New Member

    Joined:
    May 9, 2009
    Messages:
    278
    Likes Received:
    43
    Trophy Points:
    0
    Occupation:
    Production Manager:Software @ ZONTEK
    Location:
    Sri Lanka
    Home Page:
    http://zontek.zzl.org
    basically is it a simple program where the function "func" takes 3 arguments x,y,z, but rather than taking the values it take reference to the pointers of the value

    the difference is when you pass a value is c/c++ the porgram makes a copy of the variable in a new memory location and then do every stuff

    but im this case the program just passes a pointer that points to the memory address of the variables and any change done to the variables though the function would affect the actual variables

    the logic of the function is quite simple as it just add up the things

    but the function getchars is not given
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice