【问题标题】:initialization discards 'const' qualifier from pointer target type初始化从指针目标类型中丢弃“const”限定符
【发布时间】:2021-02-26 12:41:39
【问题描述】:

我还是 C 的新手,所以我对它不是很了解。现在我正在尝试找出指针。

我想通过argv变量加载结构amqp_connection_info的参数。如何更改结构中参数的负载?

int main(int argc, const char **argv) {
    amqp_connection_state_t conn;

    // char const *user;
    // char const *password;
    // char const *host;
    // int port;
    // char const *vhost;

    int debit_limit;
    int message_count;
    char const *new_queue = NULL;
    char const *new_echange;
    char const *echangetype;
    char const *cle_liaison;
    char const *contenu_message;

    static int durable = 1; // La durabilité du queue

    if (argc < 13) {
    printf("Usage: amqp_producteur utilisateur mdp nom_hote port vhost debit_limit message_count new_queue \
    new_echange echangetype cle_liaison contenu_message\n");
    return 1;
    }
    struct amqp_connection_info ci;
    // ci : variable globale contenant les paramètres de configuration de connexion
    struct amqp_connection_info ci = {
    argv[1],
    argv[2],
    argv[3],
    atoi(argv[4]),
    argv[5]};

    debit_limit = atoi(argv[6]);
    message_count = atoi(argv[7]);
    new_queue = argv[8]; /* exp:"unroutable" */
    new_echange = argv[9];   /* exp:"amq.direct" */
    echangetype = argv[10];
    cle_liaison = argv[11]; /* exp:"test" */
    contenu_message = argv[12]; /* exp:"Message à envoyer" */

    // Les routines AMQP
    if ((conn = amqp_connexion(&ci)) != NULL)
    {
        amqp_creation_queue (conn, new_queue, durable);
        amqp_creation_echange (conn, new_echange, echangetype, durable);
        amqp_production(conn, new_queue, debit_limit, message_count, contenu_message, new_echange, cle_liaison);
        amqp_deconnexion(conn);
    }
    else
    {
        printf("connexion impossible\n");
    }
    return 0;
}

编译的时候遇到了这个问题:

amqp_producteur.c: In function 'main':
amqp_producteur.c:60:5: error: initialization discards 'const' qualifier from pointer target type [-Werror]
     argv[1],
     ^
amqp_producteur.c:61:5: error: initialization discards 'const' qualifier from pointer target type [-Werror]
     argv[2],
     ^
amqp_producteur.c:62:5: error: initialization discards 'const' qualifier from pointer target type [-Werror]
     argv[3],
     ^
amqp_producteur.c:63:5: error: initialization makes pointer from integer without a cast [-Werror]
     atoi(argv[4]),
     ^
amqp_producteur.c:63:5: error: (near initialization for 'ci.vhost') [-Werror]
amqp_producteur.c:64:5: error: initialization makes integer from pointer without a cast [-Werror]
     argv[5]};
     ^
amqp_producteur.c:64:5: error: (near initialization for 'ci.port') [-Werror]
cc1: all warnings being treated as errors

【问题讨论】:

  • 首先,哪个编译器记录了int main(int argc, const char **argv) 是main() 的有效实现定义形式?因为const 不符合要求,所以argv 必须在严格符合要求的程序中进行读/写。

标签: c


【解决方案1】:

argv 被声明为 const: const char **argv。这意味着argv 的每个元素都是一个const char *(您可以将argv 视为一个常量指针数组)。您正在将 const char * 分配给非常量 char *,它会丢弃 const 限定符。这就是提示警告的原因。

您可以通过将main 的签名更改为int main(int argc, char **argv) 来避免此问题。请注意,argv 实际上不是const,因此从签名中删除const 是完全安全的,请参阅Why is main() argument argv of type char*[] rather than const char*[]?

【讨论】:

  • 就是这样,你说得对,其实我没有这个问题。谢谢。
  • 最重要的是,有效的main()形式是由编译器决定的,而不是程序员!
【解决方案2】:

除了icebp 关于const 问题的正确答案外,还有两种类型不匹配,您尝试初始化ci.vhostci.port。根据DocsForgestruct amqp_connection_info

struct amqp_connection_info {
  char *user; /**< the username to authenticate with the broker, default on most
                 broker is 'guest' */
  char *password; /**< the password to authenticate with the broker, default on
                     most brokers is 'guest' */
  char *host;     /**< the hostname of the broker */
  char *vhost; /**< the virtual host on the broker to connect to, a good default
                  is "/" */
  int port;    /**< the port that the broker is listening on, default on most
                  brokers is 5672 */
  amqp_boolean_t ssl;
};

所以,如果你在使用 port vhost 时保持不同的顺序,你也必须交换初始化器

    atoi(argv[4]),
    argv[5]

【讨论】:

  • @Amali,是的,你是对的,我会改变它。谢谢。
猜你喜欢
  • 1970-01-01
  • 2011-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多