C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 22nd, 2003, 02:54 PM
changhai changhai is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 49 changhai User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 57 sec
Reputation Power: 11
What's the meaning of: int const *pt

Is it equivalent to

const int *pt

or

int *const pt

What's the systematic way to determine the meaning of such expressions (with const qualifier)? Thanks a lot.

Reply With Quote
  #2  
Old May 22nd, 2003, 03:28 PM
defjamninja defjamninja is offline
Overly white
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Fresno, CA
Posts: 83 defjamninja User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 11
Hey a C++ question that I can answer!!! I just read about doing this in my N00b book so I may still be wrong.

Anyway, if I remeber correctly const int *pt can only point to a const integer, while int *const pt is a pointer that only points to one place and can not be changed. I think that is right .

Reply With Quote
  #3  
Old May 22nd, 2003, 03:47 PM
Combat Combat is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 42 Combat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 11
Sounds good to me!

Reply With Quote
  #4  
Old May 22nd, 2003, 04:07 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,382 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 20 h 31 m 48 sec
Reputation Power: 4080
int const *p is equivalent to const int *p (at least according to my g++ 2.95 compiler). There is a difference between const int *p and int * const p though.

const int *pi
* Can be pointed at multiple variables.
* Cannot be used to assign a new value to a variable via indirection.

int *const ip
* Can only be pointed to a single variable (done at initialization)
* Can be used to assign a new value to a variable via indirection.

const int * const pip
(Yes, it is possible to declare a variable of this type too.)
* Can only be pointed to a single variable (done at initialization)
* Cannot be used to assign a new value via indirection

To illustrate the differences:
Code:
#include <iostream>
using namespace std;
int main(void) {
  int x = 1;
  int y = 2;

  const int *pi;
  pi = &x;
  pi = &y; // Can be pointed to another variable
  *pi = 23; // <----- NOT ALLOWED
  cout << *pi << endl; // <--- Value can be accessed

  int * const ip = &x;
  *ip = 7; // <----- Can assign a value via indirection
  ip = &y; // <----- NOT ALLOWED
  cout << *ip << endl; // <---- Value can be accessed

  const int * const pip = &y;
  *pip = 42; // <------ NOT ALLOWED
  pip = &x; // <------ NOT ALLOWED
  cout << *pip << endl;

  return 0;
}


Hope this helps

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > What's the meaning of: int const *pt

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap