Thread: what is pointer? help me to find area of circle using pointers

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    75

    what is pointer? help me to find area of circle using pointers

    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
    but its difficult to find area of circle using pointers.i have found code for finding area of circle but i dont understand what exactly done.
    can someone explain me every step>?

    Code:
    main( ){
    int radius ;
    float area, perimeter ;
    printf ( "\nEnter radius of a circle " ) ;
    scanf ( "%d", &radius ) ;
    areaperi ( radius, &area, &perimeter ) ;
    printf ( "Area = %f", area ) ;
    printf ( "\nPerimeter = %f", perimeter ) ;
    }
    areaperi ( int r, float *a, float *p )
    {
    *a = 3.14 * r * r ;
    *p = 2 * 3.14 * r ;
    }
    Last edited by san12345; 12-09-2015 at 05:57 AM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by san12345 View Post
    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.

    Quote Originally Posted by san12345 View Post
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic C++ Circle Area
    By GrafixFairy in forum C++ Programming
    Replies: 0
    Last Post: 04-27-2014, 07:49 AM
  2. Area of Circle Without Pi
    By Jhernandez860 in forum C++ Programming
    Replies: 7
    Last Post: 04-08-2013, 01:24 PM
  3. area of a circle
    By wise_ron in forum C Programming
    Replies: 2
    Last Post: 10-02-2006, 03:15 PM
  4. Circle Area
    By Zophixan in forum C++ Programming
    Replies: 3
    Last Post: 11-05-2002, 12:50 PM
  5. Whats wrong with my find Circle Area program?[compiles fine]
    By Golden Bunny in forum C++ Programming
    Replies: 22
    Last Post: 06-16-2002, 02:49 PM