Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Can someone explain this pgm? (http://www.go4expert.com/forums/explain-pgm-t26194/)

prakashkumar41 2Jul2011 13:34

Can someone explain this pgm?
 
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

xpi0t0s 3Jul2011 20:16

Re: Can someone explain this pgm?
 
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.

ManzZup 3Jul2011 20:35

Re: Can someone explain this pgm?
 
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


All times are GMT +5.5. The time now is 14:22.