• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

Functions And Pointers in C Language

Buster

Slice-of-Life Aficionado
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
159
Likes
113
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2 1000 XP
Functions and Pointers

In this part you will learn:
1. C syntax
2. Pointers
3. Pointers declaration
4. Function arguments as pointers
5. Showing output

In this tutorial we will use pointers with functions. Using functions with pointers is same as using functions with any normal variable. The only difference comes in the parameter list and sometimes in the return type as well when we return a pointer or an address of a variable.
Calculating Average
Open Dev C++ then File > new > source file and start writing the code below.
  1. #include<stdio.h>
  2. #include<conio.h>

These two are the most common and basic header files. The first one stdio.h is used to grant us most of the basic functions of C like Input and Output functions. The second one conio.h provides us functions like getch() which is used to pause our screen until we press a button.

  1. double

    getAverage(

    int

    *

    arr,

    int

    size)

    ;
  2. int

    main(

    )
  3. {


  4. int

    list[

    5

    ]

    ;
  5. int

    size=

    5

    ;



  6. printf

    (

    "Enter the elements of array:\n

    "

    )

    ;

  7. int

    i;
  8. for

    (

    i=

    0

    ;

    i<

    size;

    i++

    )
  9. {
  10. scanf

    (

    "%d"

    ,&

    list[

    i]

    )

    ;
  11. }

  12. double

    avg;

  13. avg =

    getAverage(

    list,

    size )

    ;

  14. printf

    (

    "Average value is: %f\n

    "

    ,

    avg )

    ;

  15. return

    0

    ;
  16. }

In this program we will make an array and pass it on to a function which has pointers as arguments. First of all we take input from the user in an integer array. As for the start we have set the array size to 5. Then using a for loop we took input in the array.
After that we declared a variable with data type as double. We named the variable with average because our ultimate goal in this program is to calculate the average of all the numbers entered in the array.
Then we called our function getAverage() which calculated the average of the numbers entered in the array. We passed on our array containing numbers and the size of the array. Here you have to know one thing that the name of an array is the base address of its first index. So that is why when we pass array to a function that has pointers in its argument we just write the name of the array. If there was any normal variable we would have placed a ampersand sign with the name of the variable because we need to pass the address of the variable to the pointer in the function parameter.
Then we printed the average on the screen and the programs ends.

  1. double

    getAverage(

    int

    *

    arr,

    int

    size)
  2. {
  3. int

    i,

    sum =

    0

    ;
  4. double

    avg;

  5. for

    (

    i =

    0

    ;

    i <

    size;

    ++

    i)
  6. {
  7. sum =

    sum+

    arr[

    i]

    ;
  8. }

  9. avg =

    (

    double

    )

    sum /

    size;

  10. return

    avg;
  11. }

Now see in this function we caught the array in a pointer called arr . In the body of the function we declared some variables to store the average and sum of the numbers of the array. Then we wrote a for loop to add all the numbers of the array in a single variable.
After that we divided the sum of the numbers with the size of the array which was basically total number of elements in the array. Then we stored the average in the avg variable. And lastly we returned the average.
Execute > compile
then Execute > run
Output
pointerarray.png


Book traversal links for Functions And Pointers in C Language

  • ‹ Introduction to Pointers
  • Up
  • Structures in C ›


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,499

349,821

349,831

Top