C++ :: How To Use Type Casting In Programs
Apr 12, 2013
I find type casting to be very hard to grasp, I am not sure why. I understand how it works I suppose, but I find it hard to grasp why it would be needed in programming. So my question is, how often is type casting used in general programs? Is there an easier way I could be trying to teach myself about it? I am just using the tutorials provided by this site.
View 2 Replies
ADVERTISEMENT
Apr 13, 2014
This code i made, utilizing the type casting construct, isn't outputting what i wanted. The output for 'Dollars' and 'Cents' are returning '0' for both. instead all i want it to do is seperate the two. for example changing the float value of amount to an integer, giving a dollar value.
Code:
#include <stdio.h>
int main()
{
}
[code]....
View 1 Replies
View Related
Dec 2, 2013
I came across some code and it's not clear why it is casting an unsigned char * to another pointer type only to free it right after. Here are the relevant structures:
Code:
struct _Edje_Message {
Edje *edje;
Edje_Queue queue;
Edje_Message_Type type;
int id;
unsigned char *msg;
[Code] .....
As you can see, _Edge_Message has a *msg field, but in the function below, they cast it to the other two structure types inside the case blocks of the switch statement only to free it. What is the point or advantage of doing this?
Code:
void
_edje_message_free(Edje_Message *em) {
if (em->msg) {
int i;
switch (em->type) {
[Code] ......
View 14 Replies
View Related
Mar 18, 2014
I'm trying to cast a float to an unsigned int and getting some surprising behavior.
Code:
float x = -1.0;
unsigned int y = (unsigned int)x;
printf("y = %d
", y);
The output of this code changes depending on which compiler I use. Sometimes I get -1 and sometimes I get 0. Not really sure why though.
View 5 Replies
View Related
Jun 8, 2013
Let's say I have a product that needs service after 500 hours at 100 RPM. I can dsiplay the remaining hours only as an integer and we only have integer math. Management wants it to display 500 for the first hour, not 499 after the first minute is ticked off. But after a few minutes fullHours will be 499 and partialHours will have some value. I have 3 ways to determine displayHours to suit management and I want to know if the first is ligit.
short fullHours = 500;
short partialHours = 0;
short displayedHours = 0;
// Method 1 - Is is Kosher to cast a boolean as an int? Is TRUE always a 1 or is that a bad assumption?
displayedHours = fullHours + (short) (partialHours != 0);
//Method 2 - Works but some have disdain for the ternary conditional
displayHours = fullHours + (partialHours ? 1 : 0);
//Method 3 - seems a bit pedantic
displayHours = fullHours;
if (partialHours != 0) {
displayHours++;
}
View 19 Replies
View Related
Apr 30, 2014
I'm writing a small piece of code that increments through a string of numbers. For every 5 numbers a product is produced. However i'm having an issue understanding an error that keeps occurring.
#include <iostream>
#include <string>
#include <cstdlib>
[Code].....
if you look at the code i've got a string with 5 numbers 1-5 for testing. As the loop increments through the string it takes the first 5 characters and converts them to integers and then a product is returned. The issue is that instead of storing the single letter at position J its storing some combination from the string and as a result gives me a huge product. I tried debugging. I checked what the string[j] value was with a simple cout << and it returned the right number. I even returned the char value before it was converted into an integer and it was the right number.
View 3 Replies
View Related
Jan 3, 2015
double a = 1.0 + 1 + 1.0f; //everynumber will be added up as a double even the last one which is a float. All 3 numbers will turn into 3.0 as a double.
int x = (int)(7 + 3.0 / 4.0 * 2); //the variable will do the math bracket first. then the va type will still be an int because int was never changed.
Console.WriteLine((1 + 1) / 2 * 3); // 1 + 1 will be done first then 1 / 2 then * by 3
Console.WriteLine(x);
Console.ReadKey();
I THINK THATS ALL WRONG ^ =/ like the comments
double a = 1.0 + 1 + 1.0f;
In this equation, everything is using addition, so we start working left to right. 1.0 + 1 is the first step. These two representations of 1 aren't the same type though. In fact, none of the three are.
The first is a double, the second is an int, and the last is a float.
So in order to do 1.0 + 1, we need to convert one type to another. Since the double type is "wider" than the int type, we'll move things up to the double type. We'll convert the int version to a double, and to 1 + 1 using double types, resulting in 2.0 as a double.
Next we do the other addition. This has the same problem, though, because we'll be adding our result from the first step (a 2 as a double) to a float. So again, the float gets converted up to a double and we do the addition using doubles.
We now have a value of 3 that is the double type, which we can simply store in our a variable without any conversions at all, since our value is already a double type.
View 4 Replies
View Related
Mar 14, 2014
So, I've used int to float cast before. And it makes sense that it preserves the int value just converts it. I don't really need the answer, I'm just interested, and a resources could suffice.
1. Why in my test program it seems to preserve the int value, I expect that, but why for 0x8000 is it registering bit negation also. I know that is the negative bit for float, but it seems wrong. Is this an error in the gcc compiler conversion code?
2. What is the documentation on these type cast on how they actual work.
3. I know like no assembly, I'm wondering if some of the built in routines to handle or is it all c side code.
4. Can I convert type and preserve the bits. Maybe use void* casting ? I've never really bothered with void* so I don't all that I can do. Except be a pointer that doesn't know the type, obviously. I tested that out in the second code, the output doesn't seem correct, except -0.000. Is it working and my test numbers just are improper float format? It can't be that I test 0x3E20000 = 0.15625 from SingleWiki , but I got 1.328e-36 so the int to void* to float doesn't seem to work in the code below:
#include <cstdio>
int main(){
float flout;
unsigned int num = 1;
int ant;
printf("Int Shift float");
for(unsigned int shift=0;shift<32;shift++){
[Code] ....
View 5 Replies
View Related
Sep 5, 2014
Casting Pointers in C Programming. I don't want to move onto implicit casts until I have this down pat. I'm failing to understand how casting pointers works.
The line
int *mnt = (int*)&flt;
if I read this correctly passes the address of flt which has been converted to an int to the pointer mnt.
1 - When I output mnt I get a garbage value, probably because the address of flt is then converted to a pointer and passed onto mnt as a value and then reinterpreted as a memory address. (that is the first part I don't understand)
2 - - What exactly does the (int*) cast say? Does this mean that a pointer will be returned or an address will be returned. What does the fact that the * is inside the parenthesis mean?
Code:
#include <iostream>
using namespace std;
int main() {
float flt= 6.5;
int *mnt = (int*)&flt;
cout << mnt << endl; // outputs hex memory address
cout << *mnt << endl; // outputs garbage value
}
View 9 Replies
View Related
Jun 18, 2013
I am doing an exercise which has to do with International country codes.The user must give a code and the programm will display the corresponding country.
Code:
#include <stdio.h>
#define COUNTRY_COUNT
((int) (sizeof(country_codes) / sizeof(country_codes[0])))
[code]....
View 14 Replies
View Related
Aug 19, 2014
I am just getting back in to C++ after 10 years not doing any, contributing to an open source project. I'm adding in some functionality and am hitting a road block.
I need to send a multicast packet out on the network that is structured in a certain way. I have the definition, and know what data is going in each byte. I can successfully send a message using multicast, I now just need to send the right message.
I have used a char array to hold the message, as each char represents 1 byte, and I can transmit the array.
I am having trouble putting all of the data in the right place though. If my source data is a string, then I seem to be able to convert it, but if it is a short or int, then I keep getting errors when compiling. Similarly, two of the lines, (version and type) i initially tried using char arrays with a length of one.
Should I be using memcpy or a different function, or even be doing this in a totally different way altogether? This is the code that I am using, along with the packet structure:
//Construct a Zone Query packet
// 4 bytes - Signature "Ohz " = 0x6f, 0x68, 0x7a, 0x20
// 1 bytes - Version = 1
// 1 bytes - Type (0 = Zone Query, 1 = Zone Uri)
// 2 bytes - Entire message length = 12 + zone length
// 4 bytes - Length in bytes of the zone ID
// n bytes - Zone ID to query
[Code] ....
The errors that I get are:
error: invalid conversion from ‘short int’ to ‘const void*’ [-fpermissive]
memcpy(buffer + 6, packetLength, sizeof(packetLength));
^
[Code] ....
View 9 Replies
View Related
Mar 7, 2012
So I have a vector of 8bit variables. Is it possible to cast that to a vector of 6bit variables like...
8bit: 10011011 01100011 ===>
6bit: 100110 110110 0011XX
View 7 Replies
View Related
Mar 23, 2013
I've copied and pasted my code. The main program, the calculateTaxes.cpp function code and my makefile. I am using the makefile to link these two codes together but I get an error when I type 'make' in the command line.
I receive the error code:
assign2c.cpp.text+0x169): undefined reference to 'calculateTaxes(float, float, float*, float*, float*)'
collect: ld returned 1 exit status
make: *** [main.exe] error 1
[Code]......
View 2 Replies
View Related
Oct 10, 2013
so here is a basic program i wrote i am thinking of writing a currency conversion program that does multiple conversions and i was thinking it is possible to do something like this couldnt i call them after i write them as functions
usdtoeuro()
usdtokuna()
how would i go about doing that? or can you point me to anything?
Code:
#include <stdio.h>
int main()
{
[Code].....
View 2 Replies
View Related
Jan 19, 2015
I have a mobile application for android coded in javascript and a windows form coded in c#. I want my mobile app to send some data to the c# program. It is fine if the transmission is not secure. I searched the net. I found a site called pastebin.com. Unfortunately it provides api for posting and reading data. However I want something, where I can store,read as well as EDIT the data.
View 7 Replies
View Related
Oct 18, 2014
I am trying to experiment with programs and databases. Right now I am trying to set up a database so that my program, which currently just appends its results onto a text file, will instead store each result into a database, because this will make things much easier to access than trying to read specific results from an ever-larger, disorganized text file.
View 2 Replies
View Related
Aug 27, 2013
I just installed eclipse cdt for linux but i dont know how to compile programs on this IDE
View 3 Replies
View Related
Feb 3, 2014
I am just starting out learning C and I am using MinGW, Widnows 7, and cmd.exe.I am starting from scratch and learning very short and simple programs, like 20 lines long.After I use MinGW (gcc) to compile, the programs start up rather slowly, sometimes up to about 3-5 seconds. One with a big nested loop even took about 15 seconds to load. After they run once, the next time they run, it's instant.
Last time I learned from this book, I don't remember them starting so slowly. I use the command "gcc sample.c -o sample".It just concerns me that these tiny little programs are starting up so slowly. I'm scared it's a problem with my computer or MinGW installation. When I start making more advanced programs, I don't want them running slower than they ought to be.
View 12 Replies
View Related
Dec 22, 2012
Goal: To allocate some memory as a char*, read in some binary data, re-interpret it as a float* and then free the memory.
My code looks like:
void someFunction(float* &result) {
char * tmp = new char[1000];
//...Fill the char buffer here...
result = (float*)tmp; //Reinterpret binary data as floats
[Code] ....
Is the cast back to char* necessary on the red line (or could I have validly left it as float*)? Would it be different if I had written char * tmp = (char*)malloc(sizeof(char)*1000); on the blue line (and correspondingly used free (char*)floatData on the red line?
View 9 Replies
View Related
Apr 30, 2014
This is a problem I have been having with every program I write since I started using SDL 2. Whenever I compile my code and run my program, everything works perfectly fine until at some point (usually after 3-8 minutes of running), the program will stop responding completely and I will have to exit out of the console to close it. The code I believe is relevant is:
void MainLoop()
{
InitLoop();
while ( !QuitMain )
[Code].....
If I change SDL_PollEvent(&Event) to SDL_WaitEventTimeout(&Event,100), then the problem goes away (I did that and had the program running for about an hour without it stop responding before I decided that it solved the problem), so I believe that the problem has something to do with event handling. Also, it might be noteworthy to mention that when I use SDL_WaitEventTimeout with the second parameter being a small number (because 100 milliseconds is a long time to wait and makes the program run at like 8 FPS), the problem returns.
View 2 Replies
View Related
May 26, 2013
I have learned the basics you need to know about c++ so I was wondering, how do you make a GUI from scratch without using any programs like qt, daniweb, ect. Is there like a ongui() or something?
View 6 Replies
View Related
Sep 16, 2013
When you are creating a project or program, how do you number your versions?
Like when you release it, it becomes v1.0, am i right?
then every release or build after that might become v1.1 or v1.0001 or v1.0.1.2
Is there a certain global system for how you Number your versions or is it just up to the developer?
View 1 Replies
View Related
May 6, 2014
Is there any way to find the variables declared or defined in a c program and print those variables?? E.g. this is the code
#include<stdio.h>
int main() {
int a, b, c;
printf("Enter two numbers to add");
scanf("%d%d",&a,&B)/>;
c = a + b;
printf("Sum of entered numbers = %d",c);
return 0;
}
I want to print all the variables been used. I used (gdb) info locals but its not giving the proper ouput.
View 8 Replies
View Related
May 10, 2012
I am using a windows application to start other processes. I am looking for a way to keep the form itself as the topmost program until it is closed, allowing the other programs enough load time.
View 1 Replies
View Related
Jan 24, 2014
I'm trying to set up some basic communication between two programs via the registry.
For this I would like to use the functions
Code:
int value=10;
CString strSection="Data";
CWinApp* pApp = AfxGetApp();
pApp->WriteProfileInt(strSection,ValueName,value);
and
Code:
CString strSection="Data";
CWinApp* pApp = AfxGetApp();
int value=pApp->GetProfileInt(strSection,ValueName,0);
which read/write to the application's registry.
Is there some way of making two different programs use the same application registry?
View 14 Replies
View Related
Mar 16, 2014
my goal:
have 1 program handle the UI
have that program store variables to a DLL
have the 2nd program grab the stored variables to reform some number crunching, without interfering with the UI program, and once done, have it drop the answers back into that DLL, so that the UI can grab it when it's ready.
I have made the 2 programs, + dll
What I've Achieved:
the first program accesses the dll, and loads up a variable (and stays connected to the dll, so that the Dll instance doesn't reset)
I've gotten the DLL to output the variable to make sure it's received it, and stored it to it's own global variable.
the second program connects to the dll successfully, but when it tries to retrieve the data, it returns 0's (NULL's)
My research:from what I've read, so long the dll is connected to a program, all additional programs will attach to the same instance.
how do I make the global variable in the dll be accessible to both programs? (without resorting to saving it to the HDD Idealy)
View 7 Replies
View Related