Co oznacza const pointer-to-pointer w C i w C++?
Znam zasadę czytania deklaracji od prawej do lewej I byłem całkiem pewien, że wiem, co się dzieje, dopóki kolega mi nie powiedział:
const MyStructure** ppMyStruct;
Oznacza "ppMyStruct is A pointer to a const pointer to a (mutable) MyStructure " (W C++).
Myślałem, że oznacza to "ppMyStruct to Wskaźnik do wskaźnika do const Mistruct ". Szukałem odpowiedzi w C++ spec, ale najwyraźniej nie jestem w tym zbyt dobry...
Co oznacza in w C++, a czy w C oznacza to samo?
7 answers
Twój kolega się myli. To jest (non-const) wskaźnik do (non-const) wskaźnik do const Mistruktura. Zarówno w C jak i C++.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-12-03 09:30:48
W takich przypadkach narzędzie cdecl (lub C++decl) może być pomocne:
[flolo@titan ~]$ cdecl explain "const struct s** ppMyStruct"
declare ppMyStruct as pointer to pointer to const struct s
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-12-03 09:44:52
Miałeś rację w swojej interpretacji. Oto inny sposób na to:
const MyStructure * *ppMyStruct; // ptr --> ptr --> const MyStructure
MyStructure *const *ppMyStruct; // ptr --> const ptr --> MyStructure
MyStructure * *const ppMyStruct; // const ptr --> ptr --> MyStructure
Są to wszystkie alternatywy wskaźnika do wskaźnika z jednym kwalifikatorem const. Reguła od prawej do lewej może być używana do rozszyfrowania deklaracji(przynajmniej w C++; nie jestem ekspertem w C).
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-12-03 09:55:01
Masz rację.
Inna ODPOWIEDŹ wskazywała już na " zgodnie z ruchem wskazówek zegara ". Bardzo mi się spodobała - trochę rozbudowana.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:54:07
Twój kolega się myli i tak samo jest z C i C++. Spróbuj:
typedef struct foo_t {
int i;
} foo_t;
int main()
{
foo_t f = {123};
const foo_t *p = &f;
const foo_t **pp = &p;
printf("f.i = %d\n", (*pp)->i);
(*pp)->i = 888; // error
p->i = 999; // error
}
Visual C++ 2008 podaje następujące błędy dla dwóch ostatnich linii:
error C2166: l-value specifies const object
error C2166: l-value specifies const object
GCC 4 says:
error: assignment of read-only location '**pp'
error: assignment of read-only location '*p'
G++ 4 says:
error: assignment of data-member 'foo_t::i' in read-only structure
error: assignment of data-member 'foo_t::i' in read-only structure
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-12-03 10:50:24
Jako następstwo innych komentarzy, nie stawiaj 'const' na pierwszym miejscu. Naprawdę należy do tego typu. To by od razu wyjaśniło znaczenie, po prostu przeczytaj jak zwykle:
MyStructure const** ppMyStruct;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-12-03 13:52:34
void Foo( int * ptr,
int const * ptrToConst,
int * const constPtr,
int const * const constPtrToConst )
{
*ptr = 0; // OK: modifies the pointee
ptr = 0; // OK: modifies the pointer
*ptrToConst = 0; // Error! Cannot modify the pointee
ptrToConst = 0; // OK: modifies the pointer
*constPtr = 0; // OK: modifies the pointee
constPtr = 0; // Error! Cannot modify the pointer
*constPtrToConst = 0; // Error! Cannot modify the pointee
constPtrToConst = 0; // Error! Cannot modify the pointer
}