Solved

Newbie needs help with unconst const and cast conversions

Posted on 2001-09-07
6
625 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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 and use conditional statements in the C programming language.

777 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