C complains about passing char** value to function taking char const*const*const but C++ doesn't

user2485710

I'm having difficulties in understanding why C++ behaves in a more "relaxed" way than C when it comes to interpreting and creating types for the parameters of a function.

C does the simplest thing in the world, it sticks with what you write and that's it, C++ on the other hand operates in a twisted way that I can't really comprehend.

for example the popular argv which is a char* [] when passed to a function becomes char** and I really don't get why, what I expect and "want" is char * const * but I got this behaviour instead.

You can also read this article in PDF that talks about this differences between C and C++, the article also ends with this phrase:

Although C++ ignores top-level cv-qualifiers in parameter declarations when determining function signatures, it does not ignore those cv-qualifiers entirely.

and since I can't find this issue online ( Embedded System Programming - February 2000 , and this old issues are free ), I'm wondering what this phrase could possibly mean.

Someone can explain why this behaviour is the way it is in C++ ?

EDIT:

One of my examples is

#include <stdio.h>

void foo(int argc, const char *const *const argv) {
  printf("%d %s\n", argc, argv[0]);
}

int main(int argc, char *argv[]) {
  foo(argc, argv);
  return (0);
}

and if you compile this with gcc 4.8.1 you get the expected error

gcc cv_1.c 
cv_1.c: In function ‘main’:
cv_1.c:8:3: warning: passing argument 2 of ‘foo’ from incompatible pointer type [enabled by default]
   foo(argc, argv);
   ^
cv_1.c:3:6: note: expected ‘const char * const* const’ but argument is of type ‘char **’
 void foo(int argc, const char *const *const argv) {
      ^

this output makes implicit the fact that argv is interpreted as char**

David Rodríguez - dribeas

Function arguments can be passed by value or by reference. In the case of by reference there is no top-level qualifier so we can ignore that case.

In the case of by-value parameters, the top-level qualifier affects only the copy, and is completely independent of the original that is used to copy-construct that argument. If the top level qualifier was not dropped from the signature, the following two functions would be valid and different overloads:

void f(int       i);
void f(int const i);

Now the question is, given a call to f(1) which of the two overloads should be selected? The problem here is that whether the argument is const or not does not affect what it can be constructed from, so the compiler would never be able to resolve which is the correct overload. The solution is simple: in the signature the top level qualifier is dropped and both are the same function.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing const char * to a function in C

From Dev

Passing const char * to a function in C

From Dev

Passing a buffer (char*) to a function in C

From Dev

Passing char pointer to another function: blank value. c++

From Dev

C programming passing a char array into a function

From Dev

Passing a pointer to a char array as an argument to a function - C

From Dev

C18 Passing a char array to function

From Dev

Passing a pointer to a char array as an argument to a function - C

From Dev

How to point to the expected char array after passing char* into a function in C?

From Dev

How to point to the expected char array after passing char* into a function in C?

From Dev

Assigning value to char array in C doesn't work

From Dev

Passing char pointers in C

From Dev

Char passing argument in c

From Dev

How to pass an array of Swift strings to a C function taking a char ** parameter

From Dev

C - remove() doesn't delete with char *

From Dev

Char passing in C++ with pointers

From Dev

ANSI C - passing data from function to a CHAR pointer

From Dev

Char * function in C

From Dev

C function and char *

From Dev

C function and char *

From Dev

passing char array to function to get the value of update char array

From Dev

C++ Linker complains multiple definition of char* but not std::string

From Dev

c++ Search for char in char function?

From Dev

C++ - char to char16_t*

From Dev

Why C doesn't allow implicit conversion from char ** to const char *const * (and C++ does)?

From Dev

Passing char array to function

From Dev

Passing a char array by value

From Dev

C - Opening a file and reading char by char with passing the file pointer as argument

From Dev

Efficiency of passing char by reference in C++

Related Related

  1. 1

    Passing const char * to a function in C

  2. 2

    Passing const char * to a function in C

  3. 3

    Passing a buffer (char*) to a function in C

  4. 4

    Passing char pointer to another function: blank value. c++

  5. 5

    C programming passing a char array into a function

  6. 6

    Passing a pointer to a char array as an argument to a function - C

  7. 7

    C18 Passing a char array to function

  8. 8

    Passing a pointer to a char array as an argument to a function - C

  9. 9

    How to point to the expected char array after passing char* into a function in C?

  10. 10

    How to point to the expected char array after passing char* into a function in C?

  11. 11

    Assigning value to char array in C doesn't work

  12. 12

    Passing char pointers in C

  13. 13

    Char passing argument in c

  14. 14

    How to pass an array of Swift strings to a C function taking a char ** parameter

  15. 15

    C - remove() doesn't delete with char *

  16. 16

    Char passing in C++ with pointers

  17. 17

    ANSI C - passing data from function to a CHAR pointer

  18. 18

    Char * function in C

  19. 19

    C function and char *

  20. 20

    C function and char *

  21. 21

    passing char array to function to get the value of update char array

  22. 22

    C++ Linker complains multiple definition of char* but not std::string

  23. 23

    c++ Search for char in char function?

  24. 24

    C++ - char to char16_t*

  25. 25

    Why C doesn't allow implicit conversion from char ** to const char *const * (and C++ does)?

  26. 26

    Passing char array to function

  27. 27

    Passing a char array by value

  28. 28

    C - Opening a file and reading char by char with passing the file pointer as argument

  29. 29

    Efficiency of passing char by reference in C++

HotTag

Archive