[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Newbie needs help with unconst const and cast conversions

Posted on 2001-09-07
6
Medium Priority
?
651 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 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
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.

829 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