Click here to Skip to main content
15,885,668 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
void foo1( char* const p )
{
p = "A";
*p = 'A';
}
void foo2( const char* p )
{
p = "A";
*p = 'A';
}
void foo3( const char* const p )
{
p = "A";
*p = 'A';
}


there are errors in these functions and i could compile them and figure them out but i couldn't figure out why are there errors ... i guess i need some help... would anyone tell me why?
Posted
Comments
joshrduncan2012 20-Dec-12 11:35am    
Is this a homework assignment?
Albert Holguin 20-Dec-12 17:46pm    
Sure sounds like it doesn't it

You should pay attention at compiler messages.
C++
 1 void foo1( char* const p )
 2 {
 3 p = "A";
 4 *p = 'A';
 5 }
 6 void foo2( const char* p )
 7 {
 8 p = "A";
 9 *p = 'A';
10 }
11 void foo3( const char* const p )
12 {
13 p = "A";
14 *p = 'A';
15 }


gcc version 4.4.5 (with -Wall switch) produces the following output:

foo.cpp: In function ‘void foo1(char*)’:
foo.cpp:3: error: assignment of read-only parameter ‘p’
foo.cpp:3: warning: deprecated conversion from string constant to ‘char*’
foo.cpp: In function ‘void foo2(const char*)’:
foo.cpp:9: error: assignment of read-only location ‘* p’
foo.cpp: In function ‘void foo3(const char*)’:
foo.cpp:13: error: assignment of read-only parameter ‘p’
foo.cpp:14: error: assignment of read-only location ‘*(const char*)p’

  • The first error message, namely "assignment of read-only parameter ‘p’" simply states you cannot change the value of a constant (because p is a constant: it must always point to the same address).
    (The warning: "deprecated conversion from string constant to ‘char*’" states that a string literal should not be assigned to a char pointer (because string literals cannot be changed).
  • The error of line 9, namely: "assignment of read-only location ‘* p’" tells you cannot change a character of a constant string (p is not constant but it points to a constant string).
  • Errors of lines 13, 14 tells that you cannot assign neither a constant pointer not a constant pointed buffer.


That's all folks.
 
Share this answer
 
v2
Comments
Albert Holguin 20-Dec-12 17:45pm    
I'm pretty sure this is a homework question and you just did it for them.
CPallini 20-Dec-12 17:57pm    
I hope he/she will understand the value of compiler messages.
Albert Holguin 20-Dec-12 18:06pm    
We can only hope... :)
Stefan_Lang 21-Dec-12 6:23am    
A good explanation of the error messages, but I believe the problem lies in understanding the type definition. As Albert said, this may be homework, and if it is, it's most likely designed to make people understand the differences of subtly changed declarations. See my solution.
CPallini 21-Dec-12 6:34am    
I've already seen your solution and found it better than mine (to the point). You got my 5.
For variable (or function argument) declarations, it helps if you read them right to left, i. e. starting at the name of the variable:
C++
char* const p

means p is const and points ('*') to an object of type char. With respect to your function code, the relevant information is that p is const (i. e. cannot be changed) and the char it points to is not (i. e. it can be changed).

Similarly;
C++
const char* p

means that p points ('*') to an object of type char that is const. The relevant information is that you can change p as it is not const, but you cannot change the character it points to.

Also check out this nice site for automatically translating C/C++ type declarations into readable english. ;-)
 
Share this answer
 
I think you are not bothered about const pointer and pointer to const. This topic is little bit confusing but it is very important. I have seen your question
I strongly believe this is your homework. Okay, Many of them have doubt about this topic. I think before asking this type of errors, you can search it in google.

i can sujjest a good discussion points about this topic. you can read it from the below links.
1. http://stackoverflow.com/questions/6851436/difference-between-const-char-char-const-const-char-const-string-
2. http://www.go4expert.com/forums/showthread.php?t=26816

it will be useful for you.
Thanks
 
Share this answer
 
Because the p variables is a const char*, p is a read-only variable. You can't modify const variables, because they're constant variables. You can try this:
C++
void foo1( char* p )
{
p = "A";
*p = 'A';
}
void foo2( char* p )
{
p = "A";
*p = 'A';
}
void foo3( char* p )
{
p = "A";
*p = 'A';
}

Hope this helps.
 
Share this answer
 
Comments
Stefan_Lang 21-Dec-12 6:09am    
I don't think that answers the question at all. The OP asked for an explanation, not a fix. Anyway, if you look at the code it shouts "CONSTRUCTED" in 2m high brightly glowing letters right in your face! This isn't real code for a real application!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900