Solved

Newbie needs help with unconst const and cast conversions

Posted on 2001-09-07
6
622 Views
Last Modified: 2008-01-09
Hello,

Basically I am trying to use a bit of DJGPP source code, in a Win32 console application that I am going to compile using Microsoft Visual C 5. (I know I could use DJGPP to compile the application but that wouldn't increase my knowledge, or settle my curiosity) The problem that I am having is that the DJGPP source code calls a function called unconst() and Visual C doesn't have it. I can find no documentation for this function within DJGPP, so I am sutruggling to know what it does. I have gleened after a bit of web ferreting that it cast converts a const variable to a non const variable (I think).

The DJGPP file that I am trying to use is
http://www.ludd.luth.se/~ams/djgpp/cvs/djgpp/src/libc/posix/unistd/getopt.c and the line that is casing the trouble is "optarg = unconst(place, char *);" it is near the end of the file.

My three questions are really:

1) Why is the place variable declared as const in the first place in this file.

2) How can I replace the unconst function in Visual C

3) Why in general would you need to convert a const variable to a non const, surely this is redundant as const signals to the compiler that the variable can be stored in ROM, therefore even if you unconst a variable you won't/shouldn't be able to alter it.

Please keep the answers simple. Cheers.

0
Comment
Question by:cweeks
  • 3
  • 3
6 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 100 total points
ID: 6465869
>>2) How can I replace the unconst function in Visual C

You can use a macro to replace the function.

#define unconst(var,conv)     (conv)var
0
 
LVL 30

Expert Comment

by:Axter
ID: 6465894
>>3) Why in general would you need to convert a const
>>variable to a non const

Sometimes third party functions are created that accepts a non-const variable, but it does not modify the variable.

Example:
void FunctionXyz(char *somedata)
{
     printf(somedata);
}

#define unconst(var,conv)     (conv)var

int main(int argc, char* argv[])
{
     const char *data = "Hello World";
     FunctionXyz(unconst(data,char*));
     return 0;
}

In the above code, you have a const variable that needs to be used with FunctionXyz().  But FunctionXyz, takes a non-const variable.  Furthermore, FunctionXyz() does not attempt to modify the variable, so it would be safe to pass a const variable to FunctionXyz.

The true fix for the above code would be just to change the declaration for FunctionXyz, but if you're using a third party function, you may not have this option.
0
 

Author Comment

by:cweeks
ID: 6466033
Ok, excellent answers, I understand it, and getopt.c now seems to compile without errors (I haven't run time tested it yet), but what about question 1)

1) why on earth declare place as "static const char *place"

It seems needless to me ?
0
Superior storage. Superior surveillance.

WD Purple drives are built for 24/7, always-on, high-definition security systems. With support for up to 8 hard drives and 32 cameras, WD Purple drives are optimized for surveillance.

 

Author Comment

by:cweeks
ID: 6466058
Ok, excellent answers, I understand it, and getopt.c now seems to compile without errors (I haven't run time tested it yet), but what about question 1)

1) why on earth declare place as "static const char *place"

It seems needless to me ?
0
 
LVL 30

Expert Comment

by:Axter
ID: 6467720
>>1) why on earth declare place as "static const char *place"

Sorry, but I have no idea why it was declared static const.

I'm not familiar with the function, so I'm not sure what it's trying to do.
0
 

Author Comment

by:cweeks
ID: 6468307
Brilliant, quickly answered to, and it compiles a treat, thanks and thanks again.

Cam..
0

Featured Post

Do email signature updates give you a headache?

Are you constantly making changes to email signatures? Are the images not formatting how you want them to? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
In desperate need of help 8 132
How to set environment variables in C 2 78
c++ substatte a varabe for a string in a LPCTSTR statment 8 83
gdb doesn't stop on breakpoint 2 48
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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

911 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

19 Experts available now in Live!

Get 1:1 Help Now