Need help with this compiler error

This is a discussion on Need help with this compiler error within the C Programming forums, part of the General Programming Boards category; Hi I'm quite new to C Programming, I only started it this semester at University. I tried to compile a ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    3

    Need help with this compiler error

    Hi

    I'm quite new to C Programming, I only started it this semester at University.

    I tried to compile a program today and I recieved this error:

    Error E2293 Assignment.c 17: ) expected

    On line 17 I have this:
    Code:
    int getChange(int &den, int amt)
    I can't figure out how I'm supposed to fix it.. any tips or answers would be great

    And could someone also please refer me to a tutorial where they explain in detail about parameter passing/pointers/etc? I still can't understand some of it.

    Thanks a lot!
    Last edited by Evangeline; 04-05-2008 at 02:42 AM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,781
    Ehhh, & is for references which are only available in C++.
    Are you trying to compile a C++ app as C?
    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.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    Thanks for the fast reply.

    My lecturer's notes regarding C has implied that we should put an ampersand before the variable when we are passing by reference, unless I have misunderstood it. Here's the direct quote:

    "Simplified Pass by Reference
    • To use the simplified pass by reference system of C++ in our C programs means there is only one small change that needs to be made.

    • When making a call to a function and passing the parameters by reference, no changes need to be made:

    GetData(a, b);

    • However, the declaration of the function should indicate that the variables are passed by reference with the inclusion of an ampersand in front of their name:

    void GetData(int &x, int &y)
    {

    • Once inside the function, you can use a variable that has been passed by reference exactly the same as normal.
    • The only difference will be that any changes made inside the function will still exist when the function returns."


    The compiler also gets many parameter-related errors if I take out the &.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,781
    This is purely C++. Your teacher must either be taking about C++ or is confused.
    Only pointers exist in C.

    This is C:
    void GetData(int* x, int* y)
    GetData(&a, &b);
    *a = 10;
    *b = 20;

    This is C++:
    void GetData(int& x, int& y)
    GetData(a, b);
    a = 10;
    b = 20;
    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.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    Ah sorry about that.. I explored some more in his notes

    "• For the purposes of this unit we are going to use a simplified version of how pass by reference is implemented in the C language.
    • This involves using a feature of the C++ language which is an evolution of C and is backwardly compatible.
    • So we are still using C but borrowing a feature from C++."

    And I think my problem is because I'm compiling as a .c and not a .cpp

    I'll try it and post if it works.. thanks again

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    It sounds as if your instructor is confused.

    With C, you can pass by reference, like this:

    Code:
    #include <stdio.h>
    
    void pass_by_value(int number) // indicate an integer is being passed 
    { 
    	printf("pass_by_value...\n", number) ; 
    	printf("\tThe incoming number is %d\n", number) ; // num is 5 
    	printf("\tThe address of number is %p\n", &number) ; // some address on the stack 
    	number *= number ;  // square it ;
    	printf("\tAfter squaring, number is now %d\n\n", number) ; // report new value
    }
    
    void pass_by_reference(int *number)  // indicate a pointer is being passed 
    { 
    	printf("pass_by_reference...\n") ;
    	printf("\tThe incoming number is %d\n", *number) ; // num points to 5, so use indirection 
    	printf("\tThe address of number is %p\n", number) ; // same address as main()
    	*number *= *number ;  // square it - have to use indirection ;
    	printf("\tAfter squaring, number is now %d\n\n", *number) ; 
    }
    
    
    int main (int argc, const char * argv[]) {
    	int i = 5 ; 
    	printf("main...\n") ;
    	printf("\tThe number is %d\n", i) ; 
    	printf("\tThe address of number is %p\n\n", &i) ;  // report it's memory address 
    
    	pass_by_value(i) ;      // pass the value of variable i 
    	printf("main after calling pass_by_value...\n") ;
    	printf("\tThe number is now %d\n\n", i) ;  // show i has not changed value 
    
    	pass_by_reference(&i) ; // pass the address of variable i 
    	printf("main after calling pass_by_reference...\n") ;
    	printf("\tThe number is now %d\n", i) ; // show the new value of i 
    
    	return 0;
    }
    Here is the output. Note that with when passing by value, a new copy of the variable is created on the stack, as can be seen by its memory address being different than the address in main(), and any changes to it in the called function are not reflected in the calling function.

    However, when passing by reference in C, any changes to the value are reflected in the calling function, because a new copy of the variable is not made on the stack - the calling function passed the pointer to variable to the called function.

    Code:
    main...
    	The number is 5
    	The address of number is 0xbffffa0c
    
    pass_by_value...
    	The incoming number is 5
    	The address of number is 0xbffff9f0
    	After squaring, number is now 25
    
    main after calling pass_by_value...
    	The number is now 5
    
    pass_by_reference...
    	The incoming number is 5
    	The address of number is 0xbffffa0c
    	After squaring, number is now 25
    
    main after calling pass_by_reference...
    	The number is now 25
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,781
    I'd rather not call pass by pointer pass by reference, though. It might just be my own opinion, though.
    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.

  8. #8
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by Elysia View Post
    I'd rather not call pass by pointer pass by reference, though. It might just be my own opinion, though.
    It's not as much the technical meaning as it is the meaning of the word.

    Passing the address to a function by the use of pointers can be denoted "pass by reference", as the variable itself isn't passed by value, but by reference. That's what I think Todd meant.

    Either way, you're right - let's not confuse the OP any further by mixing pointers and references.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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