Thread: part of code that will not affect the rest of main()

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    79

    part of code that will not affect the rest of main()

    I am wondering if its possible to execute a part of code in main() that will not affect the rest of the execution. For example assign different values to variables that will be only valid inside a particular part of the main() and after exiting this part the old values will exist. Is there anything that I could look at?

    Many thanks
    Chris

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Quote Originally Posted by Chris_1980 View Post
    . For example assign different values to variables that will be only valid inside a particular part of the main() and after exiting this part the old values will exist. ?
    sorry i am little confused with the question the old values should exist or should not exist. i think it is not exist, then you can very well use blocks. i have just attached a sample code where the variable "i" has different values.
    Code:
    #include <stdio.h>
    int main(int argc, char *argv[])
    {
        {
         int i;
         i=12;
         printf("%d",i);
        }
        {
            int i;
            i = 13;
            printf("%d",i);
        }
        return 0;
    }
    thanks and regards,
    satya

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    A function with pass by value parameters? You should explain better what exactly you're trying to do.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    79
    Lets say I have a structure:
    Code:
     struct chromosome { array[100]; fitness;}; struct chromosome  old_chrome[100];
    And in a while loop the values of array are changed. In the loop in every iteration I have a report function where I sort the old_chrome[100] form 0 to 99 concerning the value fitness. Now, when I exit the report function I want the members of the structure to be like before and not sorted old_chrome[0] to 99.
    Like the report function never existed.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well as long as you aren't changing them inside the function, you have nothing to worry about. Otherwise you can make a copy of them inside the function, and then use that:
    Code:
    void foo( type array[], int size )
    {
        type copy[ size ];
        int x;
        for( x = 0; x < size; x++ )
            copy[ x ] = array[ x ];
        ...
        do stuff to copy
    }
    The reason you actually have to make a copy here is because arrays are passed as a pointer to the first element. Whereas if you were passing a single integer around, that integer would actually be passed as a copy.
    Code:
    void foo( int x )
    {
        x++;
        printf( "x is %d\n", x );
    }
    ...
    int x = 3;
    printf( "x outside of foo is %d\n", x );
    foo( x );
    printf( "x outside of foo is %d\n", x );

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    79
    Thanks for the reply.

    Chris

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cant see what certain part of this code does..
    By transgalactic2 in forum C Programming
    Replies: 4
    Last Post: 04-14-2009, 06:22 AM
  2. Replies: 5
    Last Post: 04-17-2008, 02:31 AM
  3. Help with part of code.
    By ajdspud in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2006, 05:21 PM
  4. Having problems with the rest of the code
    By CXHatchback in forum C Programming
    Replies: 7
    Last Post: 07-20-2004, 05:18 AM
  5. Part of my C++ code
    By Maverick in forum C++ Programming
    Replies: 11
    Last Post: 02-15-2003, 09:02 AM