0

Using the C ANSI language, I am trying to pass main's argv parameter into a function that will print a msg in stderr.

Here is the function signature :

void erreur(Erreur_prog erreur_entree, const char **argv);

Here is the function :

void erreur(Erreur_prog erreur_entree, const char **argv)
{

    switch (erreur_entree)
    {
    case status_ok:
        fprintf(stderr, "%s : Le status est ok.\n", argv[0]);
        exit(0);
        break;   
    case fichier_pgm_manquant:
        fprintf(stderr, "%s : Le fichier PGM est manquant.\n", argv[0]);
        exit(fichier_pgm_manquant);
        break;
    default:
        fprintf(stderr, "%s : Erreur.\n", argv[0]);
    }

}

I keep on getting this type of error :

tp2.c:139:33: attention : passing argument 2 of ‘erreur’ from incompatible pointer type
erreur(arguments_invalide, argv);
^
tp2.c:55:6: note: expected ‘const char **’ but argument is of type ‘char **’
void erreur(Erreur_prog erreur_entree, const char **argv);

Here's an example of the call :

erreur(arguments_invalide, argv);

I tryed playing around with the pointers but ... there's something i'm not getting.

Anyone can point me out what i'm missing out ?

Byte Lab
  • 1,576
  • 2
  • 17
  • 42
Cyberflow
  • 1,255
  • 5
  • 14
  • 24
  • 3
    `expected ‘const char **’ but argument is of type ‘char **’` Is that message not very clear? – kaylum Nov 22 '16 at 05:41
  • Sorry forgot to put the calling part ... – Cyberflow Nov 22 '16 at 05:42
  • 1
    Possible duplicate of [Why does passing char\*\* as const char\*\* generate a warning?](http://stackoverflow.com/questions/14562845/why-does-passing-char-as-const-char-generate-a-warning) – Byte Lab Nov 22 '16 at 07:15
  • 1
    Hint: Look at the function signature for main. Is `argv` a `const char **`? – Byte Lab Nov 22 '16 at 07:16
  • I was writing an answer but it occurred to me that the C qualifier system for pointer-to-pointers is so incredibly stupid, that it is useless in practice. Things to avoid at all costs: pointer-to-pointer, qualified types of pointer-to-pointer. pointer-to-qualified-pointer. Whoever came up with this system and the syntax for it wasn't a sane person. – Lundin Nov 22 '16 at 08:04
  • And what solution would you suggest ? Cause i tried everyrhing ... ill retry later again ive got a good night sleep – Cyberflow Nov 22 '16 at 14:05
  • This should work: `void erreur(Erreur_prog erreur_entree, const char **argv)` -> `void erreur(Erreur_prog erreur_entree, char **argv)` – Jabberwocky Nov 22 '16 at 17:10
  • The error message answer your question perfectly. If you cannot read, then there's nothing one can help you. – Yan Zhou Nov 22 '16 at 18:23
  • Ty Yan for your unconstructive comment – Cyberflow Nov 22 '16 at 18:37
  • @Cyberflow to be fair, Yan was correct. The error message was very clear and pointed you directly to the problem. If that message confused you then I highly suggest you take a step back and review pointers and what the const keyword means. – Byte Lab Nov 22 '16 at 21:19
  • I understand but no need to be patronize in his answer. After all, we're all here to learn and grow thru this process of learning. It's a bit normal for a person that is new to learning pointers and is a bit overwelmed about all this theory that he just doesn't see it or understand's the meaning. It's not the first time i encounter answer's of people that you feel they take themselves has superior to the person asking for help. At the end, we all die and finish in the grave. No one is better then another. We're all here to learn. Ty again. – Cyberflow Nov 23 '16 at 00:48

1 Answers1

0

Well after giving it a good tought and having my head cleared i've re-arranged the code and it works.

Here is the function signature :

void erreur(Erreur_prog erreur_entree, const char * nomAppli);

Here is the function :

void erreur(Erreur_prog erreur_entree, const char * nomAppli)
{

    switch (erreur_entree)
    {
    case status_ok:
        fprintf(stderr, "%s : Le status est ok.\n", nomAppli);
        exit(0);
        break;   
    case fichier_pgm_manquant:
        fprintf(stderr, "%s : Le fichier PGM est manquant.\n", nomAppli);
        exit(fichier_pgm_manquant);
        break;
    default:
        fprintf(stderr, "%s : Erreur.\n", nomAppli);
    }

}

Here's an example of the call :

erreur(arguments_invalide, argv[0]);

No more compilation warning

I guess he didn't liked that i used const char **argv in the signature

It was a pointer to a pointer and i needed to use a pointer (const char *monAppli)

Hope this will help others in the futur.

Cyberflow
  • 1,255
  • 5
  • 14
  • 24