473,320 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How does a pointer declared with const keyword considered as a pointer to a constant

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int main()
  3. {
  4.     int x=3;
  5.     int const *ptr =&x;
  6.     printf("%d",++x);
  7.     printf("\n %d",++*ptr);
  8.     return 0;
  9. }
  10.  
I am unable to get that when x is a variable not a constant then how come the error is that the since ptr is a pointer to a constant hence we cannot change its value, although ++x works fine, whats the logic behind this ?

just one confusion that why are we saying that ptr is a pointer to a constant when actually ptr is holding the address of a variable ?
Jul 26 '15 #1
2 1367
weaknessforcats
9,208 Expert Mod 8TB
In your example x is an int. So ++x works fine. No mystery there.

This code:

Expand|Select|Wrap|Line Numbers
  1. int const *ptr =&x;
  2.  
says that ptr is a pointer to a const int. This pointer contains the address of x.

It doesn't matter whether x is const or not. As far as ptr is concerned, x is const because ptr is a pointer to a const int. Therefore, ptr cannot be used to change x.
Jul 26 '15 #2
donbock
2,426 Expert 2GB
By the way,
Expand|Select|Wrap|Line Numbers
  1. int * const ptr = &x;
Notice that this time const follows the asterisk (*). This means that ptr is a constant pointer to int. That is, you can change the value of the int that ptr points to, but you cannot change the value of ptr to make it point at a different int.
Jul 27 '15 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: quantdev2004 | last post by:
Hi all, I have been deling with this kind of code: class Foo { public: void NonConstMethod() {} };
1
by: Izvra | last post by:
How can i specify default XML namespace when it does not declared in the xml document I need it for validation against xml schema @@@I have a procedure Sub ValidateXMLDocument(ByRef XMLDocument...
6
by: Ian Pilcher | last post by:
The more I think about pointers, the more my head hurts. From what I can tell, nothing in the standard guarantees that pointer assignment preserves bit patterns. Section 6.5.16.1, paragraph 2,...
5
by: MC | last post by:
Hi If I have a pointer to a some structure say for example payroll_ptr where struct payroll { ... } has some members and if i use a function argument as int function_process ( (payroll_ptr)...
3
by: System Administrator | last post by:
I have a function int match(struct regexp *regexp, char *string, char **tail) which matches a string against a regular expression, and if it succeeds sets *tail to be the unmatched tail of the...
41
by: Dead Loop | last post by:
Hi all, I'm a beginner and my question is: Are there any differences between char *p = "Hello, world!"; and const char *p = "Hello, world!"; ?
2
by: linq936 | last post by:
Hi, I have the following code: int main(){ /* line 1 */ const int i = 10; /* line 2 */ const int j = 20; /*...
4
by: Rob | last post by:
If I have a function that has a variable declared inside it (which is a pointer) that can be set equal to a pointer returned from another function, do i need to delete it before returning from the...
7
by: Szabolcs Borsanyi | last post by:
I know that this topic has been discussed a lot, still I'd appreciate a clear cut (and correct) answer: I pass a multidimensional array to a function, which is defined as int f(int a) { int...
2
myusernotyours
by: myusernotyours | last post by:
Hello everyone. I am using a C library from C++ code. The C library declares some function pointers using typedef, and then makes some variables using this function pointer as the type. Like: ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.