NO

Author Topic: a##b problem  (Read 5192 times)

JeanPaul

  • Guest
a##b problem
« on: June 01, 2007, 06:47:48 AM »
Hi everybody,
Does anyone know how to glue variables?
example if i do
#define glue(a,b) a##b
and call glue(mamma_, mia) then I get mamma_mia.
but if I do following:
int i = 20, j=30;
glue(i,j) then I get ij instead of 2030.

Help anyone?

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2099
Re: a##b problem
« Reply #1 on: June 01, 2007, 11:12:28 AM »
The ## is a preprocessor operator, and works on your source before compilation. This means that it uses the literal value not the value assigned during program run.
You must use string functions instead:
Code: [Select]
char *glue(int i, intj)
{
  static char str[128];
  sprintf(str, "%d%d", i, j);
  return str;
}
« Last Edit: June 01, 2007, 11:20:55 AM by frankie »
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

JeanPaul

  • Guest
Re: a##b problem
« Reply #2 on: June 01, 2007, 02:54:32 PM »
THanks I'll give a shot.

JeanPaul

  • Guest
Re: a##b problem
« Reply #3 on: June 04, 2007, 12:05:10 AM »
Hi again,
How do I do the call of that kind of function?

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2099
Re: a##b problem
« Reply #4 on: June 04, 2007, 10:46:20 AM »
Code: [Select]
char string[128];
int i = 20, j=30;
strcpy( string, glue(i,j) );
printf("Full code is: \"%s\"\n",string);
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

JeanPaul

  • Guest
Re: a##b problem
« Reply #5 on: June 04, 2007, 03:46:51 PM »
May be I forgot to mention that the goal is use a #define'd variable. here is the list of my #define's

Code: [Select]
#define square_1111  4002
#define square_1121  4003
#define square_1131  4004
#define square_1112  4005
#define square_1122  4006
#define square_1132  4007
#define square_1113  4008
#define square_1123  4009
#define square_1133  4010
#define square_1211  4011
#define square_1221  4012
#define square_1231  4013
#define square_1212  4014
#define square_1222  4015
#define square_1232  4016
#define square_1213  4017
#define square_1223  4018
#define square_1233  4019
#define square_2111  4020
#define square_3111  4029
#define square_2211  4038
#define square_3211  4047
#define square_1311  4056
#define square_1312  4057
#define square_1313  4058
#define square_1321  4059
#define square_1322  4060
#define square_1323  4061
#define square_1331  4062
#define square_1332  4063
#define square_1333  4064
#define square_2311  4065
#define square_3311  4074
#define square_2112  4021
#define square_2113  4022
#define square_2121  4023
#define square_2122  4024
#define square_2123  4025
#define square_2131  4026
#define square_2132  4027
#define square_2133  4028
#define square_3112  4030
#define square_3113  4031
#define square_3121  4032
#define square_3122  4033
#define square_3123  4034
#define square_3131  4035
#define square_3132  4036
#define square_3133  4037
#define square_2212  4039
#define square_2213  4040
#define square_2221  4041
#define square_222  4042
#define square_2223  4043
#define square_2231  4044
#define square_2232  4045
#define square_2233  4046
#define square_3212  4048
#define square_3213  4049
#define square_3231  4053
#define square_3232  4054
#define square_3233  4055
#define square_2312  4066
#define square_2313  4067
#define square_2321  4068
#define square_2322  4069
#define square_2323  4070
#define square_2331  4071
#define square_2332  4072
#define square_2333  4073
#define square_3312  4075
#define square_3313  4076
#define square_3321  4077
#define square_3322  4078
#define square_3323  4079
#define square_3331  4080
#define square_3332  4081
#define square_3333  4082

May be I'm going the wrong way. Is there any suggestion? Thanx in advance