c - Passing pointers to functions with and without & -


i trying understand when need use address-of operator & when passing arguments reference (for readers mind imprecision please read simulate pass-by-reference) functions without modifying function itself. give 2 small examples using structs. in both struct passed reference 1 involves usage of & , other not. explanation in particular case involve usage of malloc() in second example , can guess more experienced opinion. futhermore, question more general: there rule (or rule-of-thumb @ least) when can pass reference without using &?

example 1
#include <stdio.h> #include <stdlib.h>  struct author {         char *name;         int born;         int died;         char *notable_works; };  void print_struct(struct author *thomas_mann);  int main() {         struct author thomas_mann;         thomas_mann.name = "thomas mann";         thomas_mann.born = 1875;         thomas_mann.died = 1955;         thomas_mann.notable_works = "der zauberberg";          print_struct(&thomas_mann);          return exit_success; }  void print_struct(struct author *thomas_mann) {         printf("%s born in %d , died in %d.\n",                         thomas_mann->name, thomas_mann->born, thomas_mann->died);         printf("his notable work includes ‘%s’.\n",                         thomas_mann->notable_works); } 
example 2
#include <stdio.h> #include <stdlib.h>  struct author {         char *name;         int born;         int died;         char *notable_works; };  void print_struct(struct author *thomas_mann);  int main() {         struct author *thomas_mann = malloc(sizeof(struct author));         if (!thomas_mann) {                 fprintf(stderr, "memory allocation failed");                 exit(exit_failure);         }         thomas_mann->name = "thomas mann";         thomas_mann->born = 1875;         thomas_mann->died = 1955;         thomas_mann->notable_works = "der zauberberg";          print_struct(thomas_mann);          free(thomas_mann);          return exit_success; }  void print_struct(struct author *thomas_mann) {         printf("%s born in %d , died in %d.\n",                thomas_mann->name, thomas_mann->born, thomas_mann->died);         printf("his notable work includes ‘%s’.\n",                thomas_mann->notable_works); } 

the rule simple. want call function takes pointer struct object. if already have pointer object want use function on, don't need &. otherwise, do.

what mean "already have pointer"?

struct author *thomas_mann; 

in case, have pointer, because variable thomas_mann has type struct author *, pointer. have removed initialization because what initialize doesn't matter; it's pointer already, because of type.

struct author thomas_mann; 

in case, not have pointer. variable thomas_mann has type struct author, not pointer type.

struct author thomas_mann; struct author *p_thomas_mann = &thomas_mann; 

in case, call print_struct either p_thomas_mann or &thomas_mann , effect same.


it's important understand c not have pass reference. function arguments, regardless of type, passed value.1 function print_struct's argument passed value. value happens pointer struct object, , can therefore used how reference-argument used in language had pass reference, not reference in language-theoretic sense. (it is "reference" word casually used in english, though. confusion understandable, must move beyond become fluent in c.)

1 may see people talking "pass invisible reference" unless implementing c compiler, or has interoperate @ assembly level c calling convention, not need worry this, because it's invisible.


there additional wrinkle:

struct author a_thomas_mann[1]; 

this variable has type "array of struct author". because has array type, a_thomas_mann treated syntactic sugar &a_thomas_mann[0] in most, not all, contexts. called type decay , can mislead people thinking c have pass reference. again, doesn't. has odd bit of syntactic sugar relating arrays.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -