Thread: Macro Help

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    3

    Macro Help

    Hi folks,

    Trying to create a macro that sums the elements of an integer array. This is the code so far, and I'm receiving some errors here...

    Code:
    #include <stdio.h>#include <string.h>	
    #include <stdlib.h>
    
    
    #pragma warning(disable: 4996)
    
    
    #define ARRAY_SIZE 10
    
    
    #define SUMMARY (arrayOne) {									\
    	int sum = 0;												\
    	for (int c = 0; c <= ARRAY_SIZE - 1; c++) {					\
    		sum += (arrayOne[c]);									\
    	}															\
    	printf("The sum of this array (1-10) is: %d", sum);			\
    }																\
    
    
    int main()
    {
    	int arrayOne[ARRAY_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    	SUMMARY(arrayOne);
    
    
    	system("pause");
    
    
        return 0;
    }
    I'm receiving these errors:
    Code:
    
    Error (active)	E0065	expected a ';' Line 23
    
    
    Error	C2062	type 'int' unexpected	Line 23	
    
    
    
    Error	C2065	'sum': undeclared identifier	Line 23
    Is this because I need to be working with pointers when I'm passing the array to the directive SUMMARY, or am I not using parenthesis properly in the directive? Hhmm...

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Your main error is putting a space between SUMMARY and (arrayOne) in the define. You also shouldn't have the last backslash.

    However, the macro is not properly constructed anyway. Consider what would happen if you used it like this:
    Code:
        if (do_summary)
            SUMMARY(a);
        else
            printf("yo\n");
    The above will be turned into this:
    Code:
        if (do_summary)
        {
            int sum = 0;                                                \
            for (int c = 0; c <= ARRAY_SIZE - 1; c++) {                 \
                sum += (arrayOne[c]);                                   \
            }                                                           \
            printf("The sum of this array (1-10) is: %d", sum);         \
        };
        else
            printf("yo\n");
    Note the extraneous semicolon before the else! That will screw up the if/else statement.

    The proper way to fix it, although it looks a little strange, is to wrap the whole thing in a do/while(0) construct. That way it can properly absorb the semicolon while providing the needed braces.

    Code:
    #define SUMMARY(arrayOne) do{                                \
        int sum = 0;                                             \
        for (int c = 0; c <= ARRAY_SIZE - 1; c++) {              \
            sum += (arrayOne[c]);                                \
        }                                                        \
        printf("The sum of this array (1-10) is: %d\n", sum);    \
        }while(0)
    BTW, this is abnormal
    Code:
        for (int c = 0; c <= ARRAY_SIZE - 1; c++) {
    You should write it like everyone else:
    Code:
        for (int c = 0; c < ARRAY_SIZE; c++) {

    I should also add that this is not a good chunk of code to make a macro out of. I don't see what you would gain over just making it a function.

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by algorism View Post
    I should also add that this is not a good chunk of code to make a macro out of. I don't see what you would gain over just making it a function.
    I agree, with todays 64 bit very fast processors, the cost in time of a function call, is relatively infinitesimal. I no longer use or recommend macro functions for anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking a macro's sign in order to define other macro
    By Egalestrenge in forum C Programming
    Replies: 5
    Last Post: 05-23-2016, 09:39 PM
  2. what does this macro do?
    By MayaIlias in forum C Programming
    Replies: 0
    Last Post: 06-09-2015, 03:23 AM
  3. Help with macro
    By spanlength in forum C Programming
    Replies: 4
    Last Post: 07-03-2012, 03:37 PM
  4. help with macro
    By dudeomanodude in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 01:55 PM
  5. Macro
    By rajashree in forum C Programming
    Replies: 5
    Last Post: 01-10-2008, 05:24 AM

Tags for this Thread