Thread: passing by reference

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    124

    passing by reference

    i have the following program and this is the error i am getting
    illegal, right operand has type 'double *
    how do i fix it
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    void cube(double* pVariable);
    main(){
    	
    	double variable=3.43;
    	int* pVariable;
    
    	printf("Value of variable is &#37;.2lf\n",variable);
    	printf("Address of variable is %p\n",&variable);
    	
    	pVariable=&variable;
    	cube(&variable);
    	
    	system("Pause");
    }
    
    void cube(double *pVariable){
    	printf("The number %.2lf\n",pVariable);
    	pVariable=*pVariable*pVariable*pVariable;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >	printf("The number &#37;.2lf\n",pVariable);
    >	pVariable=*pVariable*pVariable*pVariable;
    I would imagine you want what the pointer points to, not the pointer itself:
    Code:
    	printf("The number %.2lf\n", *pVariable);
    	*pVariable = *pVariable * *pVariable * *pVariable;

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    main()
    Should be
    Code:
    int main(void)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > printf("The number %.2lf\n", *pVariable);
    Also unless your compiler supports C99, the correct format specifier for double is %f.
    Code:
    	printf("The number %.2f\n", *pVariable);

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Code:
    double variable=3.43;
    int* pVariable;
    pVariable=&variable;
    Your compiler should be complaining about that assignment: A pointer to double can't be converted to a pointer to int without a cast. That doesn't mean use a cast, though. It means that pVariable should be a pointer to a double.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Does C99 support &#37;lf for printf()? I couldn't find any evidence of this.

    It looks like it does support %Lf for long doubles, however. http://linux.die.net/man/3/printf
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    n869&n1124:7.19.6.1p7
    l (ell) Specifies that a following d, i, o, u, x, or X conversion specifier applies to a long int or unsigned long int argument; that a following n conversion specifier applies to a pointer to a long int argument; that a following c conversion specifier applies to a wint_t argument; that a following s conversion specifier applies to a pointer to a wchar_t argument; or has no effect on a following a, A, e, E, f, F, g, or G conversion specifier.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

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, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM