获得"错误:#28:EX pression必须有一个恒定值"同时采用varidic宏 [英] Getting "Error: #28: expression must have a constant value " while using varidic macro

查看:328
本文介绍了获得"错误:#28:EX pression必须有一个恒定值"同时采用varidic宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的硬件编程。下面我Varidic宏观创建。

I am working on HW programming. I created below Varidic macro.

#define UPDATE_DATA(bit,...) {  \
do { \
int i; \
int j = 0; \
int _args[] = {__VA_ARGS__};\
int *addr = (int *) ADDR ; \
for (i = 0; i < (sizeof(_args)/sizeof(_args[0])); i++) {\
 *(int *) (DATA_ADDR - j) |= _args[i];\
  j = j + 0x4; \
}\
*addr |= 1 << bit;\
 }while (0); \
}

它的工作,如果我使用像 UPDATE_DATA(1,2,3); 但是,如果使用像这样循环宏内部,

its working if I use like UPDATE_DATA(1,2,3); But if use this macro inside for loop like,

msg_id = MSG_1;
for(j=0; j<15 ; j++)
{
  msg_id = msg_id +1;
  l_data = data_array[j];
  UPDATE_DATA(msg_id,l_data,(SYSTEM_BASE_ADDR6+offset));  
  offset = (offset + 0x4);
}

我收到
错误:#28:EX pression必须有一个恒定的值
        LOGGER_UPDATE_DATA(MSG_ID,l_data,(SRAM0_SYSTEM_BASE_ADDR6 +偏移));

我没有得到我干了什么错在这里做的!

i am not getting what did i do wrong here!!

推荐答案

我的建议是不要使用可变参数宏。他们扩大code尺寸,使维护带来困难。

My suggestion is to not use a variadic macro. They expand code size and make maintenance difficult.

假设你将不再需要通过-1作为一个值,你可以用STDARG.H

Assuming that you won't need to pass -1 as a value, you can use stdarg.h

#include <stdio.h>  // for test output, not actually needed when you do the I/O
#include <stdarg.h>

#define LOGGER  ((volatile int *)0x1234)
#define LOGGER_DATA ((volatile int *)0x5678)

void sendToLoggerV(int bit, va_list ap)
{
    int i, value;

    for (i = 0; ; ++i)
    {
        // do your write here...

        value = va_arg(ap, int);
        if (value == -1)
            break;

        // Can't really write it in this test, but this is what you would do:
        //LOGGER_DATA[-i] = value;

        printf("Pretending to write %d to %p\n", value, (void*)&LOGGER_DATA[-i]);
    }
    //*LOGGER |= 1 << bit;
    printf("Pretending to OR %d to %p\n", 1<<bit, (void*)LOGGER);
}

void sendToLogger(int bit, ...)
{
    va_list ap;
    va_start(ap,bit);
    sendToLoggerV(bit, ap);
    va_end(ap);
}

#define SEND_TO(bit, ...) sendToLogger(bit, __VA_ARGS__, -1)

int main()
{
    SEND_TO(1, 16, 17, 18, 19);
    SEND_TO(2, 30, 31);
}

由于这将是在嵌入式系统中运行时,我的测试情况下只是打印的消息说什么它会怎么做。注释掉行会做实际的内存写入。我用做作的地址(为0x1234和0x5678的),你的内存映射I / O地址。

Since this will be running in an embedded system, my test case just prints messages to say what it would do. The lines commented out would do the actual memory writes. I have used contrived addresses (0x1234 and 0x5678) for your memory mapped I/O addresses.

这code输出以下,满足您的要求:

This code outputs the following, which meets your specifications:

Pretending to write 16 to 0x5678
Pretending to write 17 to 0x5674
Pretending to write 18 to 0x5670
Pretending to write 19 to 0x566c
Pretending to OR 2 to 0x1234
Pretending to write 30 to 0x5678
Pretending to write 31 to 0x5674
Pretending to OR 4 to 0x1234

这篇关于获得&QUOT;错误:#28:EX pression必须有一个恒定值&QUOT;同时采用varidic宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆