Click here to Skip to main content
13,508,222 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have two questions

Question Number 1
int *p;
int a[]={1};

char *q;
char b[]="welcome";

in part1.c it prints the address of variable where in part2.c it prints the values that is string? Only the return type is different how?

Question No 2

int a=10;
int const *p=&a;

Above program is pointer to constant . Here i have changed the value of a but it is not showing errors why? Does pointer to constant mean that variable should be declared as const (i.e)
const int a =10;
Posted 30-Jan-13 4:01am
Updated 30-Jan-13 4:05am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I hope this isn't somebody's homework. Anyway in Q1 the difference between part one and part two is largely down to the format strings "%s" and "%d"

Given "%s" the printf code expects a char*, it get one so it prints the string. given "%d" the printf code expects an int. It gets an int* but an int* is automatically convertible to an int, it's just a 4 byte integer value ( assuming a 32bit system ) so you get the address printed as an integer. Pass *p if you want to see the value.

Q2. constness is not broken because the value of p never changes. Try for yourself deleting the line
You'll see it makes no difference to the result. 'p' is just a pointer to where 'a' is stored( the address of a ). The value in 'a' can change as many times as you like, it's still stored in the same place so 'p' doesn't change. *p is a way of saying ( the value of what p points to ) and that will change everytime 'a' changes

If pointers are confusing you then I'd advise getting a really clear understanding of them before throwing 'const' into the mix. constness is neither as easy as it should be nor the best implemented part of the C++ language. Don't get me wrong it's a useful idea just not one that fits very comfortably into C++ which makes it a slippery thing to get a grip on.
Richard MacCutchan 30-Jan-13 10:50am
Your final paragraph really says it all.
Sergey Alexandrovich Kryukov 30-Jan-13 17:22pm
Like the reply, my 5.
steven8Gerrard 31-Jan-13 2:15am
I'm actually learning in C . BTW they say it is a pointer to constant . So value should not change right?
Matthew Faithfull 31-Jan-13 3:58am
Yes I should have spotted the 'C'ness and no a pointer to a constant is just a pointer, it can change. It's the constant being pointed at that can't change in that case. As I said it's a slippery thing.
steven8Gerrard 31-Jan-13 4:43am
So you are implying a is const? Value (i.e) 10 can change . if so how this one works ?

const int a=10;
int const *p=&a;
int b=20;

I still have doubts in pointer to constant . Source found in net is not clear
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Solution 1 is good but doesn't differentiate the different flavors of const. Consider the following:

int i1 = 0;
const int i2 = 0;

char* c1 = "...";
char* const c2 = "...";
char const* c3 = "...";
const char* c4 = "...";
const char* const c5 = "...";

I think you have i1 and i2 figured out. Pointers complicate it a little.

c1 is a pointer to char that can be modified at will. c2 is a const pointer to char (can't change where c2 points but you can change what it points to). c3 and c4 are the same thing. c3/c4 are pointer to const char (you can change where c3/c4 points but can't change what they point to). c5 is a const pointer to const char (can't change where it points or what it points to).

Introduction of 'typedef' into the mix invites a whole new dimension.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.180417.1 | Last Updated 4 Feb 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100