0

I'm still new to C, so I don't have a great understanding of it. Right now I'm trying to figure out pointers.

I want to load the parameters of a structure amqp_connection_info through the argv variables. How can I change the load of parameters in the structure?

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;
}

When compiling, I had this problem:

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
Null
  • 1,950
  • 9
  • 30
  • 33
Electrob
  • 49
  • 7
  • To begin with, which compiler documented that `int main(int argc, const char **argv)` is a valid implementation-defined form of main()? Because `const` is non-conforming, `argv` must the read/write in a strictly conforming program. – Lundin Feb 26 '21 at 13:29

2 Answers2

2

argv is declared as const: const char **argv. This means that each element of argv is a const char * (you can view argv as an array of const pointers). You are assigning that const char * to a non-const char *, which discards the const qualifier. This is what prompts the warning.

You can avoid this problem by chaning the signature of main to int main(int argc, char **argv). Note that argv is not, in fact, const so it is perfectly safe to remove the const from the signature, see Why is main() argument argv of type char*[] rather than const char*[]?.

icebp
  • 1,608
  • 1
  • 14
  • 24
2

Aside from icebp's correct answer regarding the const issue, there are two type mismatches where you try to initialize ci.vhost and ci.port. According to DocsForge, struct amqp_connection_info is

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;
};

So, if you keep the different order in the usage port vhost, you have to also swap the initializers

    atoi(argv[4]),
    argv[5]
Armali
  • 18,255
  • 14
  • 57
  • 171