Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaration of pointers in C++

Tags:

c++

pointers

In C++ whats the difference between char const *ptr=&ch; and const char *ptr=&ch;

like image 518
shreyasva Avatar asked Apr 30 '10 07:04

shreyasva


People also ask

How do you declare a pointer?

Pointers must be declared before they can be used, just like a normal variable. The syntax of declaring a pointer is to place a * in front of the name. A pointer is associated with a type (such as int and double ) too.

What is the pointer declarations used in C explain with example?

A pointer is a variable that stores the address of another variable. Unlike other variables that hold values of a certain type, pointer holds the address of a variable. For example, an integer variable holds (or you can say stores) an integer value, however an integer pointer holds the address of a integer variable.

How are pointers declared and initialized?

You need to initialize a pointer by assigning it a valid address. This is normally done via the address-of operator (&). The address-of operator (&) operates on a variable, and returns the address of the variable. For example, if number is an int variable, &number returns the address of the variable number.


2 Answers

They are the same, i.e. pointer to const char.

However char * const ptr is different, being a const pointer to (non-const) char.

And just to complete the set, const char * const ptr is a const pointer to const char.

like image 180
Paul R Avatar answered Oct 20 '22 09:10

Paul R


No difference in C++.

It is important const is before * or after *.

like image 25
Alexey Malistov Avatar answered Oct 20 '22 09:10

Alexey Malistov