gfdsal
gfdsal

Reputation: 721

Python-like Coding in C for Pointers

I am transitioning from Python to C, so my question might appear naive. I am reading tutorial on Python-C bindings and it is mentioned that:

In C, all parameters are pass-by-value. If you want to allow a function to change a variable in the caller, then you need to pass a pointer to that variable.

Question: Why cant we simply re-assign the values inside the function and be free from pointers?

The following code uses pointers:

#include <stdio.h>
int i = 24;

int increment(int *j){
    (*j)++;
    return *j;
}
void main() {
    increment(&i);
    printf("i = %d", i);
}

Now this can be replaced with the following code that doesn't use pointers:

int i = 24;

int increment(int j){
    j++;
    return j;
}
void main() {
    i=increment(i);
    printf("i = %d", i);
}

Upvotes: 0

Views: 154

Answers (3)

John Bode
John Bode

Reputation: 123508

Getting this out of the way first - pointers are fundamental to C programming. You cannot be “free” of pointers when writing C. You might as well try to never use if statements, arrays, or any of the arithmetic operators. You cannot use a substantial chunk of the standard library without using pointers.

“Pass by value” means, among other things, that the formal parameter j in increment and the actual parameter i in main are separate objects in memory, and changing one has absolutely no effect on the other. The value of i is copied to j when the function is called, but any changes to i are not reflected in j and vice-versa.

We work around this in C by using pointers. Instead of passing the value of i to increment, we pass its address (by value), and then dereference that address with the unary * operator.

This is one of the cases where we have to use pointers. The other case is when we track dynamically-allocated memory. Pointers are also useful (if not strictly required) for building containers (lists, trees, queues, stacks, etc.).

Passing a value as a parameter and returning its updated value works, but only for a single parameter. Passing multiple parameters and returning their updated values in a struct type can work, but is not good style if you’re doing it just to avoid using pointers. It’s also not possible if the function must update parameters and return some kind of status (such as the scanf library function, for example).

Similarly, using file-scope variables does not scale and creates maintenance headaches. There are times when it’s not the wrong answer, but in general it’s not a good idea.

Upvotes: 2

HARUN SASMAZ
HARUN SASMAZ

Reputation: 609

So, imagine you need to pass large arrays or other data structures that need modification. If you apply the way you use to increment an integer, then you create a copy of that large array for each call to that function. Obviously, it is not memory-friendly to create a copy, instead, we pass pointers to functions and do the updates on a single array or whatever it is.

Plus, as the other answer mentioned, if you need to update many parameters then it is impossible to return in the way you declared.

Upvotes: 1

Barmar
Barmar

Reputation: 781380

You can only return one thing from a function. If you need to update multiple parameters, or you need to use the return value for something other than the updated variable (such as an error code), you need to pass a pointer to the variable.

Upvotes: 6

Related Questions