typedef

const B and const A* are incompatible, even when B is aliased to A* [duplicate]

倾然丶 夕夏残阳落幕 提交于 2020-07-09 05:44:11
问题 This question already has answers here : typedef pointer const weirdness (6 answers) What is the difference between char * const and const char *? (19 answers) Closed 3 years ago . Why aren't const B and const A* indistinguishable, when B is typedef'ed to A* ? When compiling this simple example: struct A {}; typedef A* B; void f1(const A* a1); void f2(const B a2); int main() { const A a; f1(&a); f2(&a); } I get the following compiler output (G++ 6.3.1): test.cpp: In function ‘int main()’:

typedef'ng a pointer and const

我与影子孤独终老i 提交于 2020-07-08 05:38:47
问题 I was looking at an example which showed that why typedef'ng a pointer is a bad practice. The part I didn't understand about the example is that why the compiler wasn't able to catch the problem. I elaborated the example into the following code: #include <stdio.h> typedef int *TYPE; void set_type(TYPE t) { *t = 12; } void foo(const TYPE mytype) { set_type(mytype); // Error expected, but in fact compiles } int main() { TYPE a; int b = 10; a = &b; printf("A is %d\n",*a); foo(a); printf("A is %d

Is size_t guaranteed to be an alias type to one of integer types?

痴心易碎 提交于 2020-07-02 18:08:52
问题 Or can it be a separate unsigned integer type? I have different specializations of a template function for different (unsigned) integer types. Do I need to provide a separate specialization for size_t ? 回答1: The C++ Standard says: 18.2/2 The contents are the same as the Standard C library header , with the following changes: 18.2/6 The type size_t is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object. 18.2/7 [ Note: It is

Is size_t guaranteed to be an alias type to one of integer types?

冷暖自知 提交于 2020-07-02 18:08:33
问题 Or can it be a separate unsigned integer type? I have different specializations of a template function for different (unsigned) integer types. Do I need to provide a separate specialization for size_t ? 回答1: The C++ Standard says: 18.2/2 The contents are the same as the Standard C library header , with the following changes: 18.2/6 The type size_t is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object. 18.2/7 [ Note: It is

Use of comma in a typedef declaration?

与世无争的帅哥 提交于 2020-06-27 15:10:19
问题 Is this declaration C99/C11 compliant ? typedef struct element { char *data; struct element* next; } element, *list, elements[5]; I could not find why it works in the standard. 回答1: Yes, it is standard compliant. typedef declarations are like normal declarations except the identifiers declared by them become type aliases for the type of object the identifier would be if the declaration had no typedef in it. So while int integer, *pointer_to_integer; declare an int object named integer and an

Ambiguous type reference

限于喜欢 提交于 2020-06-17 03:01:53
问题 Why does this works : template <typename T> struct foo { }; struct A { typedef foo<A> type; }; struct B : public A { typedef foo<B> type; }; int main() { B::type john; return 0; } But not this : template <typename T> struct foo { }; template <typename T> struct Shared { typedef foo<T> type; }; struct A : public Shared<A> { }; struct B : public A, public Shared<B> { }; int main() { // g++ 4.5 says : // error: reference to 'type' is ambiguous B::type john; return 0; } In my code, foo is

typedef with pointer in C [duplicate]

你说的曾经没有我的故事 提交于 2020-05-27 12:21:08
问题 This question already has answers here : Is it a good idea to typedef pointers? (14 answers) Confusion with typedef and pointers in C [duplicate] (2 answers) Closed 4 years ago . I am beginner in C programming and I am studying on Linked Lists. I am trying to create a Linked List which is gonna display the letters in correct order. The program enables the user to insert a character in the list in alphabetical order or to delete a character from the list. So, I follow an example to a point but