passing by reference

This is a discussion on passing by reference within the C Programming forums, part of the General Programming Boards category; i have the following program and this is the error i am getting illegal, right operand has type 'double * ...

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    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
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    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,046
    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,006
    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, 09:18 AM

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