Thread: don't understand

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    29

    don't understand

    I can't understand what's my error ..

    Code:
    #include<stdio.h>
    #define size 5 
    
    
    void modifyArray( int b[] , int size );
    void modifyElement( int e );
    
    
    int main()
    {
    	int a[size] = { 0, 1, 2, 3, 4 };
    	int i;
    
    
    	printf("pass array by reference\nValues of original:\n");
    
    
    	for ( i=0; i< size; i++) {
    		printf("%3d", a[i] );
    	}
    
    
    	printf("\n");
    
    
    	modifyArray( a , size );
    
    
    	printf("After :\n");
    
    
    	for (i=0; i<size; i++) {
    		printf("%3d" , a[i] );
    	}
    
    
    	printf("pass array by value\nVale of a[3] is %d\n" , a[3] );
    
    
    	modifyElement(a[3]);
    
    
    	printf("value of a[3] is %d", a[3] );
    
    
    	return 0;
    }
    
    
    void modifyArray( int b[] , int size )
    {
    	int j;
    
    
    	for (j=0; j< size; j++) {
    		b[j] *= 2;
    	}
    }
    
    
    void modifyElement( int e)
    {
    	printf("value in modifyelement is %d" , e *=2);
    }
    Someone please correct me
    Thank you !

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you want modifyElement to change the parameter, then you need to pass a pointer to it, not the value.

    Recall that arrays are always passed as a pointer, which is why your modifyArray works.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    29
    the error it give is
    Building pg199fig5.13.obj.
    C:\Users\Eric\Documents\Pelles C Projects\C5\pg199fig5.13.c(4): error #2001: Syntax error: expected ')' but found 'integer constant'.
    C:\Users\Eric\Documents\Pelles C Projects\C5\pg199fig5.13.c(37): error #2001: Syntax error: expected ')' but found 'integer constant'.
    C:\Users\Eric\Documents\Pelles C Projects\C5\pg199fig5.13.c(38): error #2094: Missing name for parameter 2 to function 'modifyArray'.
    *** Error code: 1 ***
    Done.

    so what should I change ?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The joys of macros doing text substitution that doesn't respect your wishes.

    Because of
    Code:
    #define size 5
    the compiler sees
    Code:
    void modifyArray( int b[] , int size );
    as
    Code:
    void modifyArray( int b[] , int 5 );
    Remember the preprocessor does its deed, and then the code is compiled. Not the reverse.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by loongkee
    the error it give is
    Building pg199fig5.13.obj.
    C:\Users\Eric\Documents\Pelles C Projects\C5\pg199fig5.13.c(4): error #2001: Syntax error: expected ')' but found 'integer constant'.
    Ah. This is bad:
    Code:
    #define size 5
    size is a reasonably commonly used name, and by making it a macro name for 5, each use of that name from then on will be substituted by 5. Thus, when you wrote this:
    Code:
    void modifyArray( int b[] , int size );
    it is as if you wrote:
    Code:
    void modifyArray( int b[] , int 5 );
    hence the error.

    One way to avoid this is to adopt a convention of fully capitalised names for macro names:
    Code:
    #define SIZE 5
    It would be even better if you used a more descriptive name, where appropriate.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    29
    oh.. ok ..
    Thank you very mcuh!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. please help me understand this
    By litzkrieg in forum C Programming
    Replies: 8
    Last Post: 02-16-2011, 01:44 PM
  2. Don't Understand
    By Trckst3 in forum C++ Programming
    Replies: 9
    Last Post: 04-04-2008, 11:58 PM
  3. Something I don't understand...
    By BigFish21 in forum C Programming
    Replies: 4
    Last Post: 12-14-2007, 12:55 PM
  4. Help me out understand what I need to do please?
    By seal in forum C Programming
    Replies: 8
    Last Post: 10-11-2005, 10:31 PM
  5. Help me understand why
    By byteme101 in forum C Programming
    Replies: 7
    Last Post: 12-08-2004, 09:39 AM