/ | ||||
(C++11) | ||||
(C++11) |
(C++11) | ||||
(C++20) | ||||
(C++20) |
(C++11) | ||||
expression |
pointer |
specifier | ||||
specifier (C++11) | ||||
specifier (C++11) |
(C++11) | ||||
(C++11) |
(C++11) | ||||
(C++11) |
Specifiers | ||||
function specifier | ||||
function specifier | ||||
(C++20) | ||||
/struct | ||||
volatile | ||||
(C++26) | ||||
(C++11) |
Declarators | ||||
Block declarations | ||||
→ (C++17) | ||||
(C++11) | ||||
declaration | ||||
directive | ||||
declaration (C++11) | ||||
declaration | ||||
(C++11) | ||||
Other declarations | ||||
(C++11) | ||||
(C++11) | ||||
Declares a variable of a pointer or pointer-to-member type.
Syntax Pointers Pointers to objects Pointers to void Pointers to functions Pointers to members Pointers to data members Pointers to member functions Null pointers Invalid pointers Constness Composite pointer type Defect reports See also |
A pointer declaration is any simple declaration whose declarator has the form
attr (optional) cv (optional) declarator | (1) | ||||||||
nested-name-specifier attr (optional) cv (optional) declarator | (2) | ||||||||
nested-name-specifier | - | a |
attr | - | (since C++11) a list of |
cv | - | const/volatile qualification which apply to the pointer that is being declared (not to the pointed-to type, whose qualifications are part of declaration specifier sequence) |
declarator | - | any other than a reference declarator (there are no pointers to references). It can be another pointer declarator (pointer to pointers are allowed) |
There are no pointers to references and there are no pointers to bit-fields . Typically, mentions of "pointers" without elaboration do not include pointers to (non-static) members.
Every value of pointer type is one of the following:
A pointer that points to an object represents the address of the first byte in memory occupied by the object. A pointer past the end of an object represents the address of the first byte in memory after the end of the storage occupied by the object.
Note that two pointers that represent the same address may nonetheless have different values.
Indirection through an invalid pointer value and passing an invalid pointer value to a deallocation function have undefined behavior. Any other use of an invalid pointer value has implementation-defined behavior. Some implementations might define that copying an invalid pointer value causes a system-generated runtime fault.
A pointer to object can be initialized with the return value of the address-of operator applied to any expression of object type, including another pointer type:
Pointers may appear as operands to the built-in indirection operator (unary operator * ), which returns the lvalue expression identifying the pointed-to object:
Pointers to class objects may also appear as the left-hand operands of the member access operators operator-> and operator->* .
Because of the array-to-pointer implicit conversion, pointer to the first element of an array can be initialized with an expression of array type:
Because of the derived-to-base implicit conversion for pointers, pointer to a base class can be initialized with the address of a derived class:
If Derived is polymorphic , such a pointer may be used to make virtual function calls .
Certain addition, subtraction , increment, and decrement operators are defined for pointers to elements of arrays: such pointers satisfy the LegacyRandomAccessIterator requirements and allow the C++ library algorithms to work with raw arrays.
Comparison operators are defined for pointers to objects in some situations: two pointers that represent the same address compare equal, two null pointer values compare equal, pointers to elements of the same array compare the same as the array indices of those elements, and pointers to non-static data members with the same member access compare in order of declaration of those members.
Many implementations also provide strict total ordering of pointers of random origin, e.g. if they are implemented as addresses within continuous virtual address space. Those implementations that do not (e.g. where not all bits of the pointer are part of a memory address and have to be ignored for comparison, or an additional calculation is required or otherwise pointer and integer is not a 1 to 1 relationship), provide a specialization of std::less for pointers that has that guarantee. This makes it possible to use all pointers of random origin as keys in standard associative containers such as std::set or std::map .
Pointer to object of any type can be implicitly converted to pointer to (possibly cv-qualified ) void ; the pointer value is unchanged. The reverse conversion, which requires static_cast or explicit cast , yields the original pointer value:
If the original pointer is pointing to a base class subobject within an object of some polymorphic type, dynamic_cast may be used to obtain a void * that is pointing at the complete object of the most derived type.
Pointers to void have the same size, representation and alignment as pointers to char .
Pointers to void are used to pass objects of unknown type, which is common in C interfaces: std::malloc returns void * , std::qsort expects a user-provided callback that accepts two const void * arguments. pthread_create expects a user-provided callback that accepts and returns void * . In all cases, it is the caller's responsibility to cast the pointer to the correct type before use.
A pointer to function can be initialized with an address of a non-member function or a static member function. Because of the function-to-pointer implicit conversion, the address-of operator is optional:
Unlike functions or references to functions, pointers to functions are objects and thus can be stored in arrays, copied, assigned, etc.
Note: declarations involving pointers to functions can often be simplified with type aliases:
A pointer to function can be used as the left-hand operand of the function call operator , this invokes the pointed-to function:
Dereferencing a function pointer yields the lvalue identifying the pointed-to function:
A pointer to function may be initialized from an overload set which may include functions, function template specializations, and function templates, if only one overload matches the type of the pointer (see address of an overloaded function for more detail):
Equality comparison operators are defined for pointers to functions (they compare equal if pointing to the same function).
[ edit ] pointers to data members.
A pointer to non-static member object m which is a member of class C can be initialized with the expression & C :: m exactly. Expressions such as & ( C :: m ) or & m inside C 's member function do not form pointers to members.
Such a pointer may be used as the right-hand operand of the pointer-to-member access operators operator. * and operator - > * :
Pointer to data member of an accessible unambiguous non-virtual base class can be implicitly converted to pointer to the same data member of a derived class:
Conversion in the opposite direction, from a pointer to data member of a derived class to a pointer to data member of an unambiguous non-virtual base class, is allowed with static_cast and explicit cast , even if the base class does not have that member (but the most-derived class does, when the pointer is used for access):
The pointed-to type of a pointer-to-member may be a pointer-to-member itself: pointers to members can be multilevel, and can be cv-qualified differently at every level. Mixed multi-level combinations of pointers and pointers-to-members are also allowed:
A pointer to non-static member function f which is a member of class C can be initialized with the expression & C :: f exactly. Expressions such as & ( C :: f ) or & f inside C 's member function do not form pointers to member functions.
Such a pointer may be used as the right-hand operand of the pointer-to-member access operators operator. * and operator - > * . The resulting expression can be used only as the left-hand operand of a function-call operator:
Pointer to member function of a base class can be implicitly converted to pointer to the same member function of a derived class:
Conversion in the opposite direction, from a pointer to member function of a derived class to a pointer to member function of an unambiguous non-virtual base class, is allowed with static_cast and explicit cast , even if the base class does not have that member function (but the most-derived class does, when the pointer is used for access):
Pointers to member functions may be used as callbacks or as function objects, often after applying std::mem_fn or std::bind :
Pointers of every type have a special value known as null pointer value of that type. A pointer whose value is null does not point to an object or a function (the behavior of dereferencing a null pointer is undefined), and compares equal to all pointers of the same type whose value is also null .
A null pointer constant can be used to initialize a pointer to null or to assign the null value to an existing pointer, it is one of the following values:
(usually nullptr). | (since C++11) |
The macro NULL can also be used, it expands to an implementation-defined null pointer constant.
Zero-initialization and value-initialization also initialize pointers to their null values.
Null pointers can be used to indicate the absence of an object (e.g. std::function::target() ), or as other error condition indicators (e.g. dynamic_cast ). In general, a function that receives a pointer argument almost always needs to check if the value is null and handle that case differently (for example, the delete expression does nothing when a null pointer is passed).
A pointer value p is valid in the context of an evaluation e if one of the following condition is satisfied:
If a pointer value p is used in an evaluation e , and p is not valid in the context of e , then:
Syntax | meaning |
---|---|
const T* | pointer to constant object |
T const* | pointer to constant object |
T* const | constant pointer to object |
const T* const | constant pointer to constant object |
T const* const | constant pointer to constant object |
In general, implicit conversion from one multi-level pointer to another follows the rules described in qualification conversions .
When an operand of a comparison operator or any of the second and third operands of a conditional operator is a pointer or pointer-to-member, a composite pointer type is determined to be the common type of these operands.
Given two operands p1 and p2 having types T1 and T2 , respectively, p1 and p2 can only have a composite pointer type if any of the following conditions are satisfied:
IMAGES
VIDEO
COMMENTS
A null pointer assignment error, or many other errors, can be assigned to this issue and example. In simpler architecture or programming environments, It can refer to any code which unintentionally ends up creating nulls as pointers, or creates a bug that in anyway halts the execution, like overwriting a byte in the return stack, overwriting ...
A pointer of type int** has to point to an object of type int*.There is no object of type int* in your program.a2d is an object of type int[2][2], consisting of two objects of type int[2], each of which consists of two objects of type int.You can derive pointer values from each of these, but there are no int* pointer objects unless you define them. - Keith Thompson
Here: ptr_one = &x; ptr_two = &x; ptr_three = &y; ptr_one, ptr_two and ptr_three are int*s and &x and &y are double*s.You are trying to assign an int* with a double*.Hence the warning. Fix it by changing the types of the pointers to double* instead of int*, i.e, change the following lines. int *ptr_one; int *ptr_two; int *ptr_three;
char *q; p = q; // Error: incompatible pointer types. This is because the pointer `p` is of type `int *`, and the pointer `q` is of type `char *`. These two types are incompatible, and you cannot assign a pointer of one type to a variable of another type. An incompatible pointer type is a pointer that is not of the same type as the variable ...
Mistake # 11 : Creating garbage objects using C pointers. You need a pointer to a memory location to free / deallocate that memory. If you re-assign a pointer and there is no other pointer pointing to that memory block, you cannot deallocate that previous memory block. This causes a memory leak.
If you read this far, tweet to the author to show them you care. Tweet a Thanks
Technical Information Database TI500C.txt Null Pointer Assignment Errors Explained Category :General Platform :All Product :Borland C++ All Description: 1.
Technical Information Database TI500C.txt Null Pointer Assignment Errors Explained Category :General Platform :All Product :Borland C++ All Description: 1.
Either change the type of the fields to std::string, or derefence the field before assigning to it. ie. myClass->field1 = &myString; Now the pointer points to myString. *myClass->field1 represents the string pointed to by field1. is that the field1 pointer will become invalid when myString goes out of scope.
Consider the following program void func(int n) { int i; int grp[n][n]; int (*ptr)[n]; for (i = 0; i < n; i++) grp[i][i] = i; for (i = 0; i < n; i++) ptr = &grp[i ...
Re: C - assigment makes integer from pointer without a cast warning. path [1] is the second element of a char array, so it's a char. home is a char *, i.e. a pointer to a char. You get the warning because you try to assign a char* to a char. Note also the potential to overflow your buffer if the content of the argv [1] argument is very long.
Dereferencing a Pointer in C. Dereference operations begin at the pointer and follow up to the pointee. The objective could be to examine or modify the pointee state. A pointer can only be dereferenced if it has a pointee; the pointee must also be allocated before the pointer can be made to point at it.
Syntax of Null Pointer Declaration in C. type pointer_name = NULL; type pointer_nam e = 0; We just have to assign the NULL value. Strictly speaking, NULL expands to an implementation-defined null pointer constant which is defined in many header files such as " stdio.h ", " stddef.h ", " stdlib.h " etc.
A null pointer stores a defined value, but one that is defined by the environment to not be a valid address for any member or object. NULL vs Void Pointer - Null pointer is a value, while void pointer is a type. Wild pointer in C. A pointer that has not been initialized to anything (not even NULL) is known as a wild pointer. The pointer may ...
Solution 1. You only have a const void* and assign that to a void*. Hence you can modify the target with pt_SAP->pt_param although you could not with pt_Param. This breaks the contract of the function parameter declaration, which "promises" not to modify the object to which pt_Param pointer. And hence the warning. Nice explanation, 5ed.
This would help you resolve that particular error, but there are bigger issues that will likely make this a moot point. In reading your code, it's not clear what you were trying to do. Part of it looks like it is trying to cycle through the key string to 'shift' all of it's elements.
The first pointer is used to store the address of the variable. And the second pointer is used to store the address of the first pointer. That is why they are also known as double-pointers. We can use a pointer to a pointer to change the values of normal pointers or create a variable-sized 2-D array.
6.7.6.1 Pointer declarators. 2 For two pointer types to be compatible, both shall be identically qualified and both shall be pointers to compatible types. However types int or char on the one hand and type void on the other hand are not compatible types. You could define your functions the following way
datatype ** pointer_name; Dereferencing Double Pointer *pointer_name; // get the address stored in the inner level pointer **pointer_name; // get the value pointed by inner level pointer. Note: In C, we can create multi-level pointers with any number of levels such as - ***ptr3, ****ptr4, *****ptr5 and so on. 6. NULL Pointer. The Null Pointers are those pointers that do not point to any ...
If the original pointer is pointing to a base class subobject within an object of some polymorphic type, dynamic_cast may be used to obtain a void * that is pointing at the complete object of the most derived type. Pointers to void have the same size, representation and alignment as pointers to char.. Pointers to void are used to pass objects of unknown type, which is common in C interfaces ...
You need take the address-of instead of and then pass to p, that's a pointer. In other words,you are assign char to a char* returned from value at i index in exp` array. Try this: p = &exp[i]; answered Jan 9, 2013 at 19:13. Jack.