C function - Pass by value? Pass By Ref?

This is a discussion on C function - Pass by value? Pass By Ref? within the C Programming forums, part of the General Programming Boards category; Hi Guys, Look at the following code; Why doesnt it indicate whether its passed by val or pass by ref ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    41

    C function - Pass by value? Pass By Ref?

    Hi Guys,

    Look at the following code; Why doesnt it indicate whether its passed by val or pass by ref (by using the '&' operator?)? Or by default C is passed by ref?

    Are there some default rules somehow?

    Please advise

    Code:
    /*********************************************************************
     *
     * Purpose: Reverse characters in a string.
     * Author:  K&R
     * Date:    
     *
     *********************************************************************/
    
    void reverse(char s[]);
    
    main()
    {
      char text[80]="martin";
    
      printf("string is %s\n", text);
      reverse(text);
      printf("string is %s\n", text);
    }
    
    void reverse(char s[])
    {
      int c, i, j;
       
      for (i=0, j=strlen(s)-1; i < j;i++, j--)
      {
        c = s[i];
        s[i] = s [j];
        s[j] = c;
      }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    C always passes by value. There is no such thing as by 'reference' in C. You can use pointers to pass the value of a variable's address, and dereference that to access a variable, simulating by 'reference', but it's still actually by value.

    Arrays are always passed as pointers to their first element. So any array you pass to a function, if changes are made inside the function to it, will affect the actual array outside of the function.


    Quzah.
    Last edited by quzah; 11-18-2005 at 12:12 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Edit: Oops, too late, and I think you mean passes by value in the first sentence, Quzah :P

    C is always, always pass by value.

    When you pass an array to a function, you are actually passing the address of the first element, so in the above case, you are effectively passing &text[0], or the address of the letter 'm' in "martin". Because the function has the address, any modification to the array in the function is to the array that was passed.
    Last edited by cwr; 11-17-2005 at 06:56 PM.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    41
    oooh!

    Alright. Understand!

    Thanks!

  5. #5
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    >>C always passes by reference.
    Dont mix beer and C.Afterall its beer effect
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Speed test result
    By audinue in forum C Programming
    Replies: 4
    Last Post: 07-07-2008, 06:18 AM
  2. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21