Macros in C

How can be the macro below modified to handle non-scalar types as well like
struct test {int mint,char [16+1]}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  /* a macro swap(t,x,y) that interchanges two arguments of type t */

#include<stdio.h>

#define swap(t,x,y) { t _z; \
             _z = x;\
              x = y;\
              y = _z; }

int main(void)
{
    char x,y;
    x='a';
    y='b';
    printf("x= %c \t y= %c\n",x,y);
    swap(char,x,y);
    printf("x=%c \t y=%c\n",x,y);
}
what you have already works with such types:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>
#define swap(t,x,y) { t _z; \
             _z = x;\
              x = y;\
              y = _z; }

struct test {int mint; char pepper[16+1]; };
int main(void)
{
    struct test x1 = {1, "one"}, y1 = {2, "two"};
    printf("x1= %d %s \t y1= %d %s\n", x1.mint, x1.pepper, y1.mint, y1.pepper);
    swap(struct test, x1, y1);
    printf("x1= %d %s \t y1= %d %s\n", x1.mint, x1.pepper, y1.mint, y1.pepper);
}


demo: http://coliru.stacked-crooked.com/a/f3810e49f9488565
Hi,
Try this :
1
2
3
4
#define swap(type, t, x, y) { type t; \
             t = x;\
              x = y;\
              y = t; } 


And in the function main(), you call the macro :
swap(char, t, x, y);
Does that help you? :)
Not really.But it can be that I am doing a mistake in main.Could you please provide me with main als well so I can search for a mistake in my code,if possible.And is there a difference between char s[16+1] and char s[17]?
Last edited on
Could you please provide me with main als well so I can search for a mistake in my code,if possible
Cubbi provided you with one.


And is there a difference between char s[16+1] and char s[17]?

No.
Topic archived. No new replies allowed.