Solved

How to correctly fix 'ptr to const' to 'ptr' error?

Posted on 2011-02-15
4
641 Views
Last Modified: 2012-05-11
In a 'C' language program, pointer to constant is assigned to a pointer.

The compiler gives an error on this assignment because pointer to const is being assigned to a pointer.

What is the correct or best way to fix this error?  One way to fix this error is to typecast RHS of the assignment statement to pointer.  Is that the correct fix?

What might be some situations where pointer to const needs to be assigned to pointer?

Is it sloppy code to define pointer which will be assigned pointer to const?

Please provide some exampels to this issue.


Thanks!
0
Comment
Question by:naseeam
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 280 total points
ID: 34901204
You can't assign a "pointer to a const" to a "pointer", because then the "pointer" could be used to modify the data that has been marked as "const".

This is the whole point of the const keyword, to mark variables as "can not be modified".  Once you've done that, the compiler will enforce the rule.

So what you need to do is assign a "pointer to a const" to a "pointer to a const".

0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 220 total points
ID: 34901301
Here's a program where the intent is to keep the constant_string fixed. But if you run this program, you will see that constant_string did change. To prevent this from happening, you should use:
const char * pConstant = constant_string;

Open in new window

Now, you will get a compiler error.

Also, note that ptr is pointing to a literal string constant. On some systems, this literal string constant is read-only memory. When running this program, when calling
changeString( ptr, 15);

Open in new window

I got an access error when trying to change the first char of this literal string. To prevent this:
const char * ptr = "literal string";

Open in new window

and now the program will not compile, which is good.
char * changeString(char * str, int len) {
   if( len > 0 ) {
      str[0] = 'G';
   }
   return str;
}

int main(void)  
{  
   char * ptr = "literal string";
   const char constant_string[] = "This is a constant string - will never change";
   int len = sizeof( constant_string )/ sizeof (constant_string[0] );
   char * pConstant = (char *)constant_string;
   char * pNewString = changeString( pConstant, len);
   printf("%s\n", pNewString);
   pNewString = changeString( ptr, 15);
   printf("%s\n", pNewString);
}

Open in new window

0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 220 total points
ID: 34901540
I have my VS 2010 settings so that a warning is treated as an error which causes the build to fail. I also have settings to catch these const/non-const conditions. I'm mentioning this just in case your program builds correctly. In VS 2010, when I typecast away the const-ness of the pointer, then as mentioned before, the program crashed with an access error, which I consider good behavior, since it is easy to figure out why a program crashes as you develop it, rather than figuring out bugs once the program is deployed.

On the same computer, I built the same program in Cygwin and got a warning:
"warning: passing argument 1 of `changeString' discards qualifiers from pointer target type".
Since the program built, I ran it and was hoping for a crash; but it did not. However, the output was bad:

$ ./a
Ghis is a constant string - will never change
Giteral string

For this program, the behavior of the VS 2010 program is safer, since we do not want to inadvertently modify literal string constants.
0
 

Author Closing Comment

by:naseeam
ID: 34901781
Perfect!   Extremely useful solution!   Very quick response!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now