3

I was working on the following as an example to see the differences between passing an object directly and then passing a pointer to it:

#include "stdio.h"

// Car object
typedef struct Car {
    char*           name;
    unsigned int    price;
} Car;

void print_car(Car car) {
    printf("<Car: %s, Price: $%d>", car.name, car.price);
};

void print_car2(Car *car) {
    printf("<Car: %s, Price: $%d>", car->name, car->price);
};

int main(int argc, char* argv[]) {

    Car chevy = {chevy.name = "Chevy", chevy.price = 45000};
    print_car(chevy);

    Car mazda = {chevy.name = "Mazda", chevy.price = 30000};
    print_car2(&mazda);

    return 1;

}

Other than the first approach being much more readable and easier to understand for me, what are the differences between the two? When would passing a pointer be the only option, and why do both work in the above case?

11
  • 1
    The first example makes a copy of the whole struct so is less efficient that way. Also, if the struct fields of the caller's variable need to be modified by the function then only the second option will work. Commented Jan 5, 2021 at 23:54
  • @kaylum I see, so in the background the compiler is basically passing each value of the struct into the register/stack, and so the function has a 'copy-of' those values? I know this is loose/poor terminology, but is that accurate? Commented Jan 5, 2021 at 23:57
  • @kaylum also, is this the distinction between pass-by-value print_car and then pass-by-reference print_car2 ? Commented Jan 6, 2021 at 0:01
  • Everything in C is pass-by-value. The difference is that the value you are passing is the address of a struct rather than the value of the struct. Commented Jan 6, 2021 at 0:08
  • @ChristianGibbons I see -- what's an example of a language that has pass-by-reference? Commented Jan 6, 2021 at 0:19

3 Answers 3

5

There are two reasons to use the second approach:

  1. If you want to avoid copying the whole struct. If the struct is big, this can affect performance.
  2. If you want to modify struct that you're passing.
Sign up to request clarification or add additional context in comments.

Comments

3

In general (not only for structs) passing a variable to a function will make a copy of this variable so if you want to alter this variable you ll have to return the value of the altered copy but you may want to alter the variable and return something else, in this case you have no other choice of passing a pointer as argument exemple :

first exemple with passing a variable

int useless_func(int nb) /*nb is actually a copy of the variable passed as argument */
{
    nb++; /* the copy was incremented, not the real variable */
    return nb; /* the new value is returned */
}
int main()
{
    int nb = 1;

    useless_func(nb); /* here nb is still = 1 cause it wasn't altered by the function */
    nb = useless_func(nb); /* here nb is = 2 cause it took the return value of the func */
}

now a second stupid exemple with pointer :

char *useless_func(int *nb) /* nb is now a pointer to the variable */
{
    *nb++; /* the derefencement of the pointer (so the variable value) was incremented */
    return strdup("This is a useless return"); /* we return some other stupid stuff */
}
int main()
{
    int nb = 1;
    char *str = useless_func(&nb); /* now nb is = 2 and str is an allocated useless string woohoo */
}

1 Comment

and i forgot to put the & sign in the last line ^^stupid me
3

When a function is called, the arguments in a function can be passed by value or passed by reference.

void print_car(Car car)

In here you are directly passing an object to the function, that means it will be copied into the functions stack and destroyed after function call ends. This method should be avoided because copying is expensive and unnecessary. Also if your objects are quite big, this operation will take a lot of time

void print_car2(Car *car) {

In this situation you are passing a pointer to the object which is called pass by reference, that means you are working with the original object and changes you make will directly affect it. It's a lot faster because you are not moving your object, but you should be careful about alter of original data

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.