Thread: any help would be appreciated.

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    99

    any help would be appreciated.

    Hi all-
    Could someone please help me with this assignment? I have a swap code that works, but now I need to alter it so the swap function appears in the main function after I call the function. Does this make sense? Maybe I do not understand correctly. This is where I am.

    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
    	void swap(int x, int y);
    
    
    	int first_num = 10;
    	int second_num = 50;
    
    
    	printf("\nThe value of first_num is %d \nThe value of second num is %d \n",  
                               first_num,  second_num );
    
    
    	swap(first_num, second_num);
    	{
    	int local_int;
    
    
    	local_int = x;
    	x = y;
    	y = local_int;
    
    
    	printf("\nIn the function first_num is %d\nIn the function second_num is %d \n", x, 
                          y );
    	}
    	
    	printf("\nThe value of first_num is %d \nThe value of second num is %d \n",  
                               first_num,  second_num );
    return 0;
    }
    My error reads that 'x and y are not declared in this scope.' Any help would be appreciated. Thanks.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    No, you do not understand correctly. Nesting of functions within functions is not permitted in C. Functions call each other. They do not contain implementations of each other.

    Move the definition of swap() after the final closing brace for the main() function.

    That is not actually enough to get your swap() function working though. Function arguments are passed by value in C. That means, if a function changes its argument, the change is not visible to the caller. Look for information on passing pointers to functions to have a means of making changes that are visible to the caller.

    And, no, I'm not going to modify your code for you. It will take some effort to puzzle through all parts of what I've said but, once you do, my hope is that you'll be on the road to improving your understanding.
    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.

  3. #3
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    C doesn't allow nested function definitions. I suggest the following design:

    Code:
    #include <stdio.h>
    
    static void swap(int *x, int *y)
    {
        int tmp = *x;
    
        *x = *y;
        *y = tmp;
    }
    
    int main(void)
    {
        int a = 10, b = 20;
    
        printf("%d %d\n", a, b);
        swap(&a, &b);
        printf("%d %d\n", a, b);
        return 0;
    }

  4. #4
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by rosemary View Post
    I have a swap code that works
    Your problem is threefold.

    First, you need to declare the swap function before the main. The declaration is the void swap(int x, int y); line. Its purpose is to tell the compiler that you'll be defining the the function with that name and signature later on.

    Second, you need to define the function somewhere else. Right now, you have some form of definition within the main() function, and that is wrong. Move it either before or after main().

    Third, that signature for your swap function will not work. In C, variables are passed by value. That means that any changes to the variables (x and y) will only be visible within the function. What you need to do, is to pass pointers to the variables you wish to swap, so that the function can swap the contents pointed by the two pointers instead. The main function then calls it with e.g. swap(&u, &v);

    Personally, I prefer to define a generic swap function, with the signature
    Code:
    void swap(void *const p1, void *const p2, size_t const size);
    Given two variables x and y of the same type, you can swap their contents using swap(&x, &y, sizeof x); no matter what the type actually is.

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    I didn't think C allowed nested functions. That's why I was confused. I already have a code that works with the function after main. I will contact the professor and ask for a clarification. Thank you.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Sorry, I meant I have another code that works. The one I pasted is the one I was trying to make into a nested function, but obviously it is not possible. Thanks for your input.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Nominal Animal View Post
    First, you need to declare the swap function before the main.
    That's not true - it's optional. Function declarations are permitted within function bodies (and the definition is only visible within scope of the containing function). It is function definitions (implementations) that are not permitted in functions.
    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.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Personally, I prefer to define a generic swap function, with the signature
    void swap(void *const p1, void *const p2, size_t const size);
    What did you do for the temp variable?

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    What does this mean to you-
    "Now modify your program so that the swapping performed by the function will appear in the main program after calling the function." ?

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think he's trying to say the same sort of things grumpy is.

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Quote Originally Posted by grumpy View Post
    That's not true - it's optional. Function declarations are permitted within function bodies (and the definition is only visible within scope of the containing function). It is function definitions (implementations) that are not permitted in functions.
    Could you explain more?

  12. #12
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    This is my beginning code-
    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
    	void swap(int x, int y);
    
    
    	int first_num = 10;
    	int second_num = 50;
    
    
    	printf("\nThe value of first_num is %d \nThe value of second num is %d \n",  
                               first_num,  second_num );
    
    
    	swap(first_num, second_num);
    
    
    	printf("\nThe value of first_num is %d \nThe value of second num is %d \n",  
                               first_num,  second_num );
    }
    
    
    void swap(int x, int y)
    {
    	int local_int;
    
    
    	local_int = x;
    	x = y;
    	y = local_int;
    
    
    	printf("\nIn the function first_num is %d\nIn the function second_num is %d \n ", x, 
                          y );
    }

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by rosemary View Post
    Could you explain more?
    This is legal ...
    Code:
    void swap(int x, int y);     /*  Function declaration  - legal.  Visible to all subsequent functions in this source file */
    int main()
    {
        void swap(int x, int y);    /* function declaration - legal.  Only visible inside main() */
    
        int a = 3, b = 2;
        swap(a,b);                     /*  call the swap() function.  */
    }
    but this is not ....
    Code:
    void swap(int x, int y);   /* function declaration - legal */
    int main()
    {
        int a = 3, b = 2;
        swap(a,b);                   /*  call the swap() function.  OK */
    
        void swap(int x, int y)      /*  function definition inside a function.  Illegal! */
         { /* whatever implementation */};
    }

    Note that, in this post, I have not corrected the swap() function so things it does are visible to the caller. The arguments of swap() need to be pointers, not of type int.
    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.

  14. #14
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by whiteflags View Post
    What did you do for the temp variable?
    You treat the two as char arrays of the specified size, and swap char by char. A straightforward implementation is a dozen lines of less, plus comments.

    An optimized implementation using GCC/Intel CC/Pathscale built-ins and unsigned integer transfers (only if aligned on alignment-sensitive architectures) is a bit longer. The built-ins let you do optimizations when e.g. the size is a compile-time constant, or the compiler knows the pointers are aligned.

  15. #15
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by grumpy View Post
    Function declarations are permitted within function bodies (and the definition is only visible within scope of the containing function).
    Technically, yes. Try submitting code that does that to any project, and you'll be asked to rewrite it.

    Just because something is technically allowed, does not mean you should encourage it.

    I firmly believe giving learners simple rules that lead to good practices, to start with. Then, when they grasp concepts such as scope, you can point them to the actual rules.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help, would be very appreciated.
    By DK12 in forum C Programming
    Replies: 16
    Last Post: 07-28-2009, 04:54 PM
  2. Little help will be appreciated
    By nicolassp in forum C Programming
    Replies: 18
    Last Post: 06-24-2009, 06:57 AM
  3. Help would be much appreciated.
    By jitterbug in forum C++ Programming
    Replies: 6
    Last Post: 04-01-2009, 08:02 PM
  4. Any help much appreciated
    By euclid in forum C Programming
    Replies: 13
    Last Post: 11-15-2007, 05:53 AM
  5. much appreciated
    By razrektah in forum C++ Programming
    Replies: 3
    Last Post: 09-14-2001, 10:14 AM