Thread: Need help with macros (urgent)

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

    Smile Need help with macros (urgent)

    Hey, I'm new here.

    I am in a C programming course in my college. Its an online course so sometimes I can't get the help I need. I don't recommend taking programming as an online course. One really does need a live instructor to understand such important concepts. But I had no choice except an online course.

    Anyway, I have an assignment on macros, and its due in a couple of hours. Since I'm in an online course my book is my only instructor, and the book doesn't have ANY examples using macros. It just has lines of code explaining in bits and pieces how to define macros and etc.

    My book is C How to Program 5th edition and I program in Visual C++ 2005 express.

    I need an example of a macro. My assignment asks me to write a program that uses a macro to sum the values in an array.

    From the book I understand that a macro is very similar to a function. But my problem is that, since there are no examples of macros in my book I'm not putting things in the correct places, and getting major errors.

    Here is an example macro I tried. It failed horribly. Maybe if you can help me fix this example I will understand enough to do the assignment myself. The following example has nothing to do with my assignment, its just a test to see if I'm doing macros correctly.


    Code:
    #include <stdio.h>
    
    # define Rect( x, y) ( ( x) * (y ) )
    
    
    
    int main( void )
    {
    	int rectArea, x, y;
    
    	rectArea= Rect( x + 4, y + 7) ;
    	
    
    	printf( "%d ", rectArea );
    
    }
    Please help me fix that ^^^

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Initialize x and y to be actual numbers rather than "random uninitialized data". But otherwise that's a macro.

    Also, I wonder what you think an example would look like other than "bits of code".

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    Ah youre right its so simple. (slaps self)

    With all this confusion I forgot that I left x and y as abstract variables.

    Yea here I fixed the code. Output is 117

    Code:
    #include <stdio.h>
    
    # define Rect( x, y) ( ( x) * (y ) )
    
    
    
    int main( void )
    {
    	int rectArea;
    	int x= 5;
    	int y= 6;
    
    	rectArea= Rect( x + 4, y + 7) ;
    	
    
    	printf( "&#37;d ", rectArea );
    
    }
    Ok now concerning my actual assignment. My macro needs to sum the variables of an array of integers.

    My plan of attack. The macro recieves as arguments, the array, and the number of elements. Then something like

    sum = sum + a[i];

    but it also needs a for loop to sum the array up. And how do I use a for loop with a macro? Thats my real question. Because the macro alone should sum my array up, just like a function, so that later in main I only have have to provide random numbers to fill the array, and to call on the macro like a function.
    Last edited by Countfog; 04-26-2008 at 09:01 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I do not see why you believe there is something magical about macros. They are C code. Write the code just like you would otherwise, but without the line breaks (or using backslashes to make a multi-line macro).

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    The reason why I think macros are different, is because they are predefined before the program begins, it makes one think that anything outside of main cant be used the same way.

    Once again if I had a live instructor he would probably explain this to me, and books assume things sometimes. Assumptions are dangerous in programming.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    ? Your entire program is, of course, predefined before the program begins, otherwise you wouldn't have a compiled program to execute. All the preprocessor does with a macro is a little search and replace.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    Ok still having trouble.

    Here is the function that sums up the values of an array.

    Code:
    int sum_array(int a[], int num_elements)
    {
       int i, sum=0;
       for (i=0; i<num_elements; i++)
       {
    	 sum = sum + a[i];
       }
       return(sum);
    }
    Now how to do the same exact thing using a macro? Here is what I tried. Its most likely complete nonsense.
    Code:
    #include <stdio.h>
    
    # define SUMARRAY( int a[], int elements) ( some calculation involving int elements and int a[]  )
    
    
    
    int main( void )
    {
    	int a[10] = {1, 2, 8, 2, 4, 5, 6, 9, 10, 19};
    	int sum;
    }
    Please help me out. The book doesn't explain this well, and I have no live instructor.

    The second part of the macro should probably have something to do with the int elements, and sum them up somehow. But how?
    Last edited by Countfog; 04-26-2008 at 09:37 PM.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Unfortunately, I left my copy of Deitel and Deitel at the office, but I recall it being a perfectly good explanation of macros.

    But anyway: why did you break the definition of a macro function that you had right before? And you have to actually copy the function into the macro instead of just deleting it.
    Code:
    #define SUMARRAY(a, num_elements) ({int i, sum=0; for (i=0; i<num_elements; i++) sum = sum + a[i]; sum;})
    Instead of an explicit return we just use the variable by itself; it's a do-nothing statement, but it does have the value of the variable, so the whole parenthesized expression takes that value, so we can use it as a right-hand side in an assignment statement like total = SUMARRAY(a, 10); or whatever.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    Wow!!! So the macro is literally almost exactly like a function. The book does not stress this point enough, it doesn't explain that I can put the entire code of a similar function into the macro and it will work.

    I did try it and it does work, but only one problem. The compiler still gives me errors.

    It seems to have a problem with line 16.

    Code:
    #include <stdio.h>
    
    #define SUMARRAY(a, num_elements) ({int i, sum=0; for (i=0; i<num_elements; i++) sum = sum + a[i]; sum;})
    
    
    
    int main( void )
    {
    	int a[10] = {1, 2, 8, 2, 4, 5, 6, 9, 10, 19};
    	int sum;
    
    
    16.	sum = SUMARRAY(a, 10); 
    
    		printf("The sum is &#37;d\n", sum);
    
    		return 0;
    
    }
    Last edited by Countfog; 04-26-2008 at 10:39 PM.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't think you've quite got it yet: macros don't do anything, which is why the book doesn't stress what they do very much (since they don't, in fact, do anything). They are nothing more, nor less, then search-and-replace just like Ctrl-F. If it would work if you did search and replace, it would work with a macro.

    Upon further review, the ability to put a curly-brace set on the right side of the expression appears to be a GCC extension, since Visual Studio, as noted, doesn't like it. To make it work I had to pass in the variable I wanted to use as the accumulator as a third parameter, since without braces I can't declare a new variable. And then the macro would just be the for statement, and it would appear in the code by itself (not as the right-hand-side of an assignment).

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    Thanks a lot tabstop, I really do understand macros a lot more now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Order of execution of preprocessor macros
    By DL1 in forum C Programming
    Replies: 2
    Last Post: 04-02-2009, 06:52 PM
  2. parameterized macros vs. functions
    By Tbrez in forum C Programming
    Replies: 3
    Last Post: 04-02-2009, 12:33 PM
  3. function definition with macros
    By toshog in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2009, 09:22 PM
  4. Pre-processor macros causing segfaults
    By nempo in forum C++ Programming
    Replies: 6
    Last Post: 02-10-2009, 02:35 AM
  5. Macros inside of macros
    By Chewie8 in forum C Programming
    Replies: 2
    Last Post: 02-24-2008, 03:51 AM