Pass pointer as parameter in C function


Frank 26

I read all the answer questions on this topic and none of them answer my question...

I'm still reading about pointers in C, and now I'm trying to understand how to pass pointers through functions. However, the following code (taken from tutorialspoint.com) has something I don't understand:

#include <stdio.h>

/* function declaration */
double getAverage(int *arr, int size);

int main () {

   /* an int array with 5 elements */
   int balance[5] = {1000, 2, 3, 17, 50};
   double avg;

   /* pass pointer to the array as an argument */
   avg = getAverage( balance, 5 ) ;

   /* output the returned value  */
   printf("Average value is: %f\n", avg );
   return 0;
}

double getAverage(int *arr, int size) {

   int  i, sum = 0;       
   double avg;          

   for (i = 0; i < size; ++i) {
      sum += arr[i];
   }

   avg = (double)sum / size;
   return avg;
}

When calling getAverage(balance, 5), I pass in a pointer to the first element of the balance array (as I learned in the previous question). But how do we access the actual content of the array (sum += arr[i]) in the for loop in getAverage?

I read the following resource: http://c-faq.com/aryptr/aryptrequiv.html and I 'm pretty sure the sentence after the second footnote explains this. But I still don't get it.

Any help is appreciated!

tom kazis

As described in getAverage, arrthe first element pointed to by balance. Therefore, *arrit can be used like a balance[0]caller to access the first element (read or write) .

To access the second element, you can add one before dereferencing the pointer, ie *(arr + 1), and so on. This is actually equivalent to arr[1]. Especially arr[i]equivalent to *(arr + i). It adds the offset ito the address arr, scales by the size pointed to, and dereferences the pointer.

Related


C pass pointer as parameter in function

Zach I come across the following code: int H3I_hook(int (*progress_fn)(int*), int *id) { ... } I don't understand the purpose of (int*)the end of the first argument ? I'm Uncover the mystery: int (*progress_fn)(int*) It can be explained as follows: int (*pro

Pass pointer as parameter in C function

Frank 26 I read all the answer questions on this topic and none of them answer my question... I'm still reading about pointers in C, and now I'm trying to understand how to pass pointers through functions. However, the following code (taken from tutorialspoint

C pass pointer as parameter in function

Zach I come across the following code: int H3I_hook(int (*progress_fn)(int*), int *id) { ... } I don't understand the purpose of (int*)the end of the first argument ? I'm Uncover the mystery: int (*progress_fn)(int*) It can be explained as follows: int (*pro

C pass pointer as parameter in function

Zach I come across the following code: int H3I_hook(int (*progress_fn)(int*), int *id) { ... } I don't understand the purpose of (int*)the end of the first argument ? I'm Uncover the mystery: int (*progress_fn)(int*) It can be explained as follows: int (*pro

Pass pointer as parameter in C function

Frank 26 I read all the answer questions on this topic and none of them answer my question... I'm still reading about pointers in C, and now I'm trying to understand how to pass pointers through functions. However, the following code (taken from tutorialspoint

C pass pointer as parameter in function

Zach I come across the following code: int H3I_hook(int (*progress_fn)(int*), int *id) { ... } I don't understand the purpose of (int*)the end of the first argument ? I'm Uncover the mystery: int (*progress_fn)(int*) It can be explained as follows: int (*pro

C pass pointer as parameter in function

Zach I come across the following code: int H3I_hook(int (*progress_fn)(int*), int *id) { ... } I don't understand the purpose of (int*)the end of the first argument ? I'm Uncover the mystery: int (*progress_fn)(int*) It can be explained as follows: int (*pro

C pass pointer as parameter in function

Zach I come across the following code: int H3I_hook(int (*progress_fn)(int*), int *id) { ... } I don't understand the purpose of (int*)the end of the first argument ? I'm Uncover the mystery: int (*progress_fn)(int*) It can be explained as follows: int (*pro

Pass pointer as parameter in C function

Frank 26 I read all the answer questions on this topic and none of them answer my question... I'm still reading about pointers in C, and now I'm trying to understand how to pass pointers through functions. However, the following code (taken from tutorialspoint

pass function pointer in c using pointer parameter

Pavigilti Here I have defined two function pointers func1and func2, which I guess is a way to use function pointers in c. But my problem is when I call executor(func1); executor(func2); How do I allocate memory uint8_t *and pass it as an argument to either fu

Pass pointer to char array as parameter to function - C

Macaque In the function declaration below, the first parameter is a String, specifically an array of chars, and the third parameter is a pointer to an integer. Is the second parameter a pointer to an array of characters? In other words, a pointer to a pointer?

When to pass a pointer as a parameter to a function in C++?

Nejat Here I read an article about when to pass a pointer as a function parameter. But I'm wondering about some cases where you should pass a pointer to a pointer as a parameter to a function. To be more clear, I'd like to know when I should use something like

Pass pointer to char array as parameter to function - C

Macaque In the function declaration below, the first parameter is a String, specifically an array of chars, and the third parameter is a pointer to an integer. Is the second parameter a pointer to an array of characters? In other words, a pointer to a pointer?

Pass parameter value by pointer in c function

Li Bo I am new to learning c.....so I am trying to create a tree structure from an input like this: (2, 50) (4, 30) (9, 30) (10, 400) (-5, -40) (7, 20) (19, 200) (20, 50) (-18, -200) (-2, 29) (2, 67) (4, 35) (9, 45) (-18, 100) First, I define some data struc

Pass pointer to char array as parameter to function - C

Macaque In the function declaration below, the first parameter is a String, specifically an array of chars, and the third parameter is a pointer to an integer. Is the second parameter a pointer to an array of characters? In other words, a pointer to a pointer?

Pass pointer to char array as parameter to function - C

Macaque In the function declaration below, the first parameter is a String, specifically an array of chars, and the third parameter is a pointer to an integer. Is the second parameter a pointer to an array of characters? In other words, a pointer to a pointer?

Pass a pointer as a function parameter

Shubham Chhabra: I'm passing a pointer to a function with the intention of modifying the data held at the original address. #include<bits/stdc++.h> using namespace std; void square(int **x) { **x = **x + 2; cout<<**x<<" "; } int main() { int y = 5;

Pass a pointer as a function parameter

Shubham Chhabra: I'm passing a pointer to a function with the intention of modifying the data held at the original address. #include<bits/stdc++.h> using namespace std; void square(int **x) { **x = **x + 2; cout<<**x<<" "; } int main() { int y = 5;

Pass double pointer as function parameter

username I just want to assign one pointer to another through a function (same memory address). My code is as follows: #include <stdio.h> void d(int** a) { int* val_ptr = malloc(1); *val_ptr = 5; printf("%d\n", *val_ptr); a = &val_ptr; } i

Pass char pointer as parameter to function

unlimited This item table prints different permutations of strings. If I declare the string as a char array in main and pass the array name in the printAnagram function it works fine. But if I declare string as char *s="hello" and pass 's' it crashes. Why? #in

Pass char pointer as parameter to function

unlimited This item table prints different permutations of strings. If I declare the string as a char array in main and pass the array name in the printAnagram function it works fine. But if I declare string as char *s="hello" and pass 's' it crashes. Why? #in

Pass double pointer as function parameter

username I just want to assign one pointer to another through a function (same memory address). My code is as follows: #include <stdio.h> void d(int** a) { int* val_ptr = malloc(1); *val_ptr = 5; printf("%d\n", *val_ptr); a = &val_ptr; } i

Pass pointer to member function as parameter using C++11

N08 In an effort to optimize my code below, it seems to me that it would be beneficial if I could pass a pointer to one of the member functions str1and str2pass it as a parameter fill_vecwithout having to do two explicit loops fill_vec. Is there a preferred wa