Pass by reference in C

后端 未结 7 1521
夕颜
夕颜 2020-12-03 03:34

I\'m trying to use pass by reference in C so that the function can modify the values of the parameters passed to it. This is the function signature:

int loc         


        
相关标签:
7条回答
  • 2020-12-03 03:47

    C has no reference variables but you can consider reference as const pointer to data so ,

    Make const pointer to data like this so that pointer cant point to other data but data being pointed by it can be changed.

    int  locate (char *name,  int  * const s, int * const i)
    
    0 讨论(0)
  • 2020-12-03 03:57

    If you wish to use pass by reference method to modify a parameter used in function call, always use pointers as formal arguments of function.

    Hence instead of using this

    int locate(char *name, int &s, int &i)
    

    use,

    int locate(char *name, int *s, int *i)
    

    When you will call this function, you may use

    .........

    int a=5;
    int d=10;
    char c='A'; 
    int result;
    
    result=locate(&c, &a, &d);
    

    I hope all would be clear now,

    For more idea about passing by value or passing by reference and which one is better, this source is good for beginners

    http://www.learnc.net/call-by-reference.php

    Have fun.

    0 讨论(0)
  • 2020-12-03 03:58

    (I am have been using C for a while but I am pretty still new to few concepts in C. I am trying to answer this to the best of my knowledge, feel free to correct me If I am wrong)

    When you say pass by reference you pass the address, and do to so you would use pointers pointing to the addresses. So you need to pass the pointers to your function prototype but what you are doing is passing address directly.

    What your function tries is de-reference(using &) your pointer, but you never had a pointer to parameters 's' and 'i' to de-reference it.

    The right way to do for the function prototype/definition is

    int locate(char *name, int *s, int *i)
    

    and when you have to call this function you use the below declaration in the main or calling function

    int locate(char *name, &s, &i)  
    
    0 讨论(0)
  • 2020-12-03 04:03

    C does not support pass by reference. You'll need C++ to do it the way it is written, or modify into

    int locate(char *name, int *s, int *i)
    

    and pass pointers to the second and third parameter variables.

    0 讨论(0)
  • 2020-12-03 04:06

    You can't do this in c. c doesn't have reference, you can use pointer instead.

    0 讨论(0)
  • 2020-12-03 04:12

    The earlier answers given seem to be missing context, and don't address the property that C pointers are overloaded. It's completely fair to claim that C "passes by reference." A pointer is a reference, too. However, more formally, a C "reference" is the mechanism to represent a symbol's memory address.

    • A pointer is simply a variable in memory whose value is treated as an address.
    • A reference is simply the value-literal for a memory address.
    • The C language allows for passing by reference - but only in the right context!

    The separation of context is that of signature definition and signature invocation.

    Consider the primitives int a = 0x22e8; and somewhere else we declare int* b = &a; (I'll get to functions in a minute).

    We could visualize this in memory (assume endianness doesn't matter for this example):

    ...
    0x00000200:        22e8        ; this is the variable for int a's literal value:
                                   ; a == 0x000022e8
                                   ; &a == 0x00000200
    0x00000???:        9090        ; ... more code ...
    0x00000400:        0200        ; this is pointer b pointing to a's address:
                                   ; b == 0x00000200
                                   ; *b == 0x000022e8
                                   ; &b == 0x00000400
    ...
    

    It's easy to see that we can assign a pointer to any address without any special tricks. A pointer is just a variable, but C allows us to reference the address it's pointing to; we can "dereference" the pointer from its value-address to instead treat the pointer as its pointee's underlying value during an invocation context: while ( *b == a ) .... We could easily invoke b == &a.

    When applying this to function calls, you have to separate the context of function (signature) definition vs invocation in order to differentiate pass by reference.

    int* foo(int* blah, int nah) { .. }     // signature definition context:
                                            // These are parameters
    ... vs ...
    
    b = foo( &a, 4);                        // invocation context:
                                            // These are arguments
    

    In defining a function signature, we are not telling the compiler which address an argument is accessed from — the runtime doesn't even know yet! It's just nonsense to define void bar(int &blah) {...}. Instead we use the dereferenced pointer syntax — "whatever argument is passed in will be loaded at the address pointed to" — to reference the desired argument value when runtime occurs. Thus C can pass arguments by reference.

    The contexts of both function signature definitions and function calls change how the compiler looks at pointer overloading vs reference, and how the runtime can actually address them.

    0 讨论(0)
提交回复
热议问题