首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

宏定义这个如何改

2013-08-09 
宏定义这个怎么改?#define geta(x,n) \int c\while(n)\{ \c x%10\xx/10\n--}\return cint main(){

宏定义这个怎么改?


#define geta(x,n) \
   int c;\
   while(n)\
   { \
  c = x%10;\
   x=x/10;\
   n--;}\
   return c;
int main()
{
int d;
d = geta(3215,2);

geta()显示错误做操作数左值必须为左值。怎么改啊,帮助下,菜鸟了。 宏 C
[解决办法]
 2 #include <stdio.h>
  3 #include <string.h>
  4 
  5 
  6 #define geta(x,n,c) \
  7     {   \
  8         while(n) \
  9         { \
 10             c = x%10; \
 11             x = x/10;  \
 12             n--;    \
 13         }   \
 14     }\
 15 
 16 
 17 int main()
 18 
 19 {   
 20     int a = 3215, b = 2;
 21     int c = -1;
 22     geta( a, b, c);
 23     
 24     printf("### d = %d \n", c);
 25  
 26     return 0;
 27 }
~                             

[解决办法]
帮你找到了解决办法,变量定义成全局的

  1 #include <stdio.h>
  2 int d,n;


  3 
  4 #define geta(x,n)   \
  5    int c;           \
  6    while(n)         \
  7    {                \
  8       c = x%10;     \
  9        x=x/10;      \
 10        n--;}        \
 11        d=c;
 12 
 13 int main()
 14 {
 15     d = 3215;
 16     n = 2;
 17     geta(d,n);
 18 }
 19 
~                                                                                      



说明一点
像下面这号的函数宏定义一般都是为了避免重复定义只有很少差别的函数,它和c++的模板有点思想相近,但是机制不一样
lz如果要这里做一个完整的函数,而且包含return,这是不可能的。

325 #define kstrto_from_user(f, g, type)                                    \
326 int f(const char __user *s, size_t count, unsigned int base, type *res) \
327 {                                                                       \
328         /* sign, base 2 representation, newline, terminator */          \
329         char buf[1 + sizeof(type) * 8 + 1 + 1];            \


330                                                                         \
331         count = min(count, sizeof(buf) - 1);                            \
332         if (copy_from_user(buf, s, count))                              \
333                 return -EFAULT;                                         \
334         buf[count] = '\0';                                              \
335         return g(buf, base, res);                                       \
336 }                                                                       \
337 EXPORT_SYMBOL(f)
338 
339 kstrto_from_user(kstrtoull_from_user,   kstrtoull,      unsigned long long);

热点排行