[Solved] Explicit type casting operator in C/C++

i = int (f); is valid in C++ but not in C. From the C99 Standard, 6.5.4 Cast operators cast-expression: unary-expression ( type-name ) cast-expression C++ supports the above form of casting as well as function style casting. Function style casting is like calling the constructor of a type to construct on object. Check out … Read more

[Solved] Purpose of void*

Try to compile you code with a serious ANSI C compiler, from C89 to C11, and you will get the same error: Test.c(9): error #2168: Operands of ‘=’ have incompatible types ‘double *’ and ‘int *’. Test.c(11): error #2168: Operands of ‘=’ have incompatible types ‘double *’ and ‘char *’. Test.c(13): error #2168: Operands of … Read more

[Solved] Can I change the datatype of previously declared variable in C?

Since the pointer returned by malloc() is sufficiently well aligned to be used as a pointer to any type, you could (but probably shouldn’t) use: assert(sizeof(float) <= 10); void *data = malloc(10); char *str = data; strcpy(str, “123.4”); float *vp = data; *vp = strtof(str, NULL); …use *vp… free(data); And now you can use *vp … Read more

[Solved] Why can I convert from int ** to int * in the case of the two-dimensional array, but not in the case of the array of pointers

You might want to learn how these objects are laid out in memory: int array[][3] = { {1,2,3}, {4,5,6}, {7,8,9} }; int *p_array = (int *) array; This creates an object array of type int[][3] in memory (in this case stack) physically laid out in a hypothetical (16-bit word size) architecture as: array 0x8000: 0x0001 … Read more

[Solved] C/C++ divisions with double and following shift operation

A very small rounding difference, within the range possible for the difference between 64 and 80 bits, could account for the different output. The combination of the truncate to int and shift can magnify a tiny difference. This program: #include <stdio.h> int main(){ double zaehler = -20; double teiler = 0.08; printf(“ergebnis = %d \n”, … Read more

[Solved] Unsigned int not working C++

You are expecting the cast from int to unsigned int to simply change the sign of a negative value while maintaining its magnitude. But that isn’t how it works in C or C++. when it comes to overflow, unsigned integers follow modular arithmetic, meaning that assigning or initializing from negatives values such as -1 or … Read more

[Solved] Converting String to Double in Java [closed]

GPA = Double.parseDouble(df.format (((double)qualityPoints) /(double)(sumOfHours))); int qualityPoints = 0; int sumOfHours = 0; double GPA = 0; DecimalFormat df = new DecimalFormat(“0.00”); GPA = Double.parseDouble(df.format (((double)qualityPoints) /(double)(sumOfHours))); No compilation error for this. 8 solved Converting String to Double in Java [closed]

[Solved] Convert string into float in C++ with full Significant figures [Fixed working in Dev C++]

If you want to control the precision then include #include <iomanip> and use std::cout << std::setprecision(17); to set the number of digits you want. Also float has 6 significant bits precision whereas double has 12. So whenever your string has more than 6 digits in decimal there is a possibility of loosing the precision. 7 … Read more

[Solved] Cast increment and assign in C

[How can I] De-reference and assign a value to a pointer and increment the pointer by (uint64_t) bytes in one line[?] You are trying to do too many things with a single expression. You should break it up. Moreover, “one line” is more or less arbitrary. I could take your one-liner and spread it out … Read more

[Solved] How can we convert a string to a user defined object in java [closed]

You want to transfer a String into a Treecreat? You should give Treecreat a constructor like this. public Treecreat(String yourString){ this.myString = yourString; } So the String is in Treecreat and you can work with it in your method, with call the method in this way: add(new Treecreat(data). solved How can we convert a string … Read more

(Solved) Do I cast the result of malloc?

TL;DR int *sieve = (int *) malloc(sizeof(int) * length); has two problems. The cast and that you’re using the type instead of variable as argument for sizeof. Instead, do like this: int *sieve = malloc(sizeof *sieve * length); Long version No; you don’t cast the result, since: It is unnecessary, as void * is automatically … Read more