Solved

Newbie needs help with unconst const and cast conversions

Posted on 2001-09-07
6
618 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

746 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

12 Experts available now in Live!

Get 1:1 Help Now