?
Solved

Newbie needs help with unconst const and cast conversions

Posted on 2001-09-07
6
Medium Priority
?
645 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 400 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files 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.

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