Originally Posted by
san12345
i have given problem to solve area of circle using pointers.
but i dont know anything about pointers. i just know that we can declare pointer as *ptr and it stores the address of another variable
If you don't understand a concept, then you will not be able to utilize it. You should read more about pointers. Also, this assignment sounds like it's intended to give you practice in using pointers, so you should be trying it out for yourself.
It sounds like you know the basic definition of a pointer - that is a start.
Originally Posted by
san12345
i have found code for finding area of circle but i dont understand what exactly done.
can someone explain me every step>?
"Finding" code will not help you learn. Scrap this and start writing your own code. The difficulties you face in doing so is part of the learning process.
Anyway, I don't think anybody is interested in describing this program to you step by step. Even if someone did, you probably wouldn't learn as much from it.
Here's a basic explanation of what's going on. If you pass a variable to a function, the function operates on a copy of that variable, so the value in the calling function remains unchanged. Compile and run this, and see what the output is:
Code:
#include <stdio.h>
void function(int x);
int main(void)
{
int x = 1;
printf("Value of x before function call: %d\n", x);
function(x);
printf("Value of x after function call: %d\n", x);
return 0;
}
void function(int x)
{
x = 2;
printf("Inside function, x = %d\n", x);
}
Be sure to carefully read through the code to see what is happening.
If you want the function to actually update the value in the caller (i.e. not work on a copy of that variable), you need to pass a pointer to that variable. The function then uses the address of the variable in question to update its value, so the change is seen in the calling function.
Second example - compile and run this, and see what the output is:
Code:
#include <stdio.h>
void function(int *x);
int main(void)
{
int x = 1;
printf("Value of x before function call: %d\n", x);
function(&x);
printf("Value of x after function call: %d\n", x);
return 0;
}
void function(int *x)
{
*x = 2;
printf("Inside function, x = %d\n", *x);
}
Some terms for you to look up:
- Address-of Operator
- Dereference