passing value or reference?

This is a discussion on passing value or reference? within the C Programming forums, part of the General Programming Boards category; hi all, i have a doubt for this simple program. i'm passing just a copy of p to changeNum function. ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    26

    passing value or reference?

    hi all,
    i have a doubt for this simple program. i'm passing just a copy of p to changeNum function. why is it changing the original? i know its pretty silly but i can't understand this. please tell me. One more question is if ihave int **ptr, how to pass ptr to a function so it can change the value. thanks in advance.

    Code:
    #include <stdio.h>
    
    void changeNum(int *);
    
    int main(void)
    {
    	int i = 0;
    	int *p;
    
    	p = &i;
    	printf("*p is %d\n", *p);
    	changeNum(p);
    	printf("*p is %d\n", *p);
    	return 0;
    }
    
    void changeNum(int *p)
    {
    	*p = 10;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: passing value or reference?

    Originally posted by pari
    hi all,
    i have a doubt for this simple program. i'm passing just a copy of p to changeNum function. why is it changing the original?
    Code:
    #include <stdio.h>
    
    void changeNum(int *);
    
    int main(void)
    {
    	int i = 0;
    	int *p;
    
    	p = &i;
    	printf("*p is %d\n", *p);
    	changeNum(p);
    No you're not. You're passing it a pointer. A pointer which points to the original. Thus, when you change the contents of what the pointer points at, you change the original. Because you're changing what the pointer points at.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    26
    i found this on website. please, what does this mean?
    http://www.eskimo.com/~scs/C-faq/q4.8.html

    Question 4.8
    I have a function which accepts, and is supposed to initialize, a pointer:
    Code:
    void f(ip)
    int *ip;
    {
    	static int dummy = 5;
    	ip = &dummy;
    }
    But when I call it like this:
    Code:
    int *ip;
    f(ip);
    the pointer in the caller remains unchanged.


    --------------------------------------------------------------------------------

    Are you sure the function initialized what you thought it did? Remember that arguments in C are passed by value. The called function altered only the passed copy of the pointer. You'll either want to pass the address of the pointer (the function will end up accepting a pointer-to-a-pointer), or have the function return the pointer.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    26
    thank you both. i got it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 05:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM

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