初始化从指针目标类型中丢弃'const'限定符



我对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_messagen");
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 impossiblen");
}
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

argv声明为const:const char **argv。这意味着argv的每个元素都是const char *(您可以将argv视为const指针数组)。您正在将const char *赋值给非constchar *,这会丢弃const限定符。这是提示警告的内容。

可以通过修改main的签名为int main(int argc, char **argv)来避免这个问题。请注意,argv实际上不是const,因此从签名中删除const是完全安全的,参见为什么main()参数argv的类型是char*[]而不是const char*[]?

除了icebp关于const问题的正确答案之外,还有两个类型不匹配,您尝试初始化ci.vhostci.port。根据DocsForge,struct 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]

最新更新