DEV Community

Discussion on: Pointers In C - Functions

Collapse
 
pentacular profile image
pentacular

In the function call multiply(x,y), we passed the value of x and y ( of main()), which are actual parameters, to multiply().

A bit confused by this assertion.

x and y in main are not parameters -- they're variables.

In the call to multiply their values are passed as arguments (not parameters) to multiply.

Within multiply they initialize the parameters x and y.

Call by reference helps us achieve this. We pass the address of or the reference of the variables to the function which does not create a copy. Using the dereferencing operator *, we can access the value stored at those addresses.

There is no call by reference in C.

Here you are passing pointer values by value.

Similarly, we can give arrays as arguments using pointers.

You are not giving arrays as arguments -- it is not possible to produce an array typed value in C, and since C passes by value, it just can't happen.

An array in C evaluates to a pointer to its first element, and that's what you're passing.

You could pass a pointer to the array instead, which would look like this.

void foo(int (*p)[5]) {
  // *p is the array we passed, hurrah.
}

int main() {
  int a[5];
  foo(&a);
}

int* multiply(int *a, int *b){
int *c = *a * *b;
return c;
}

This example is illegal as you are trying to initialize an int * with an int.

A pointer to function or function pointer stores the address of the function. Though it doesn't point to any data. It points to the first instruction in the function.

It does not point to the first instruction in the function.

As you've noted it doesn't point to any data -- C has a segmented harvard memory model, where functions are opaque handles.

C also has no concept of instruction.

The syntax for declaring a pointer to function is -

You can make this much simpler by using typedef.

typedef int foo(char);

foo *bar; // A pointer to an int(char) type function.
Collapse
 
its_srijan profile image
Srijan • Edited

Huge thanks for pointing out the mistakes. 🙏

Collapse
 
pentacular profile image
pentacular

You're welcome. :)

C is often taught in a very confusing fashion.