warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘size_t’ - g++

I followed some similar posts to modify the code, however the warnings are still generated.
$ g++ ncfile.c -o ncfile -g -lnetcdf
ncfile.c: In function ‘int main(int, char**)’:
ncfile.c:363: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘size_t’
ncfile.c:363: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘size_t’
ncfile.c:364: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘size_t’
ncfile.c:364: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘size_t’
In that block around line 363 and 364:
for(i = 0; i < ndimsp; i++) {
char * temp_name;
size_t temp_len;
temp_name = (char *)malloc(sizeof(char) * 20);
nc_inq_dim(cid, dimids[i], temp_name, &temp_len);
dimlens[i] = temp_len;
if(dimids[i] == unlimdimidp) printf("\t\t%d %s \tlength: %d (UNLIMITED)\n", i, temp_name, temp_len);
else printf("\t\t%d %s \tlength: %d\n", i, temp_name, temp_len);
total_records *= temp_len;
free(temp_name);
}
What should I get rid of the warnings. It that harmful to the results?
Thanks,
Michael

Try using the z modifier. Basically %zu for the size_t value.
this will be the outcome:
printf("\t\t%d %s \tlength: %zu\n", i, temp_name, temp_len);
Take a look at this question:
How can one print a size_t variable portably using the printf family?

Related

Raku: How do I assign values to CArray[WCHAR]?

$ raku -v
This is Rakudo version 2019.07.1 built on MoarVM version 2019.07.1
The following was done on Raku REPL. What am I doing wrong here? How do I assign values to CArray[WCHAR]?
I want $lpData[0] to be 0xABCD and $lpData[1] to be 0xEF12.
> use NativeCall;
Nil
> constant WCHAR := uint16;
(uint16)
> my $ValueData = 0xABCDEF12;
2882400018
> my CArray[WCHAR] $lpData;
(CArray[uint16])
> $lpData[ 0 ] = ( $ValueData +& 0xFFFF0000 ) +> 0x10;
Type check failed in assignment to $lpData; expected NativeCall::Types::CArray[uint16] but got Array ($[])
in block <unit> at <unknown file> line 1
> $lpData[ 1 ] = $ValueData +& 0x0000FFFF;
Type check failed in assignment to $lpData; expected NativeCall::Types::CArray[uint16] but got Array ($[])
in block <unit> at <unknown file> line 1
Many thanks,
-T
The problem is stated clearly in the error message: in the way you declare it, it's expecting every item to be a CArray[WCHAR]. Declare it this way, as is indicated in the documentation:
use NativeCall;
constant WCHAR = uint16; # No need to bind here also
my $native-array = CArray[WCHAR].new();
$native-array[0] = 0xABCDEF12 +& 0x0000FFFF;
say $native-array.list; # OUTPUT: «(-4334)␤»
CArray is not exactly a Positional, but it does have AT-POS defined so you can use square brackets to assign values. The error arises when, you try to assign to an non-initialized Scalar (which contains any) as if it were an array. The minimal change from your program is just initializing to a CArray[WCHAR]:
use NativeCall;
constant WCHAR = uint16; # No need to bind here also
my CArray[WCHAR] $native-array .= new;
$native-array[0] = 0xABCDEF12 +& 0x0000FFFF;
say $native-array.list; # OUTPUT: «(-4334)␤»

How to fill an xtensor array, with complex data type

Paraphrasing here, but this is the problem
xt::pyarray< std::complex<double> > output;
output = 0; // fails to compile
output = double(0); // also fails to compile
output = complex<double>(0); // succeeds
and yet this is fine
std::complex<double> foo;
foo = double(0);
The problem is I'm writing a function which accepts arbitrary xtensor array, eg.
template<typename xtarray_t>
void my_function(xtarray_t &output, const xtarray_t &input) {
output = 0;
}
so in general this
output = complex<double>(0);
would fail. I've seen this, but am not sure if this was implemented, or how to use it, couldn't find the documentation. In general though, I think it should still work with std::complex.
thanks for any help!
compiler output:
xtensor-python/include/xtensor-python/pyarray.hpp:360:20: note: candidate function not viable: no known conversion from 'double' to 'const xt::pyarray<std::__1::complex<double>, xt::layout_type::dynamic>::self_type' (aka 'const pyarray<std::__1::complex<double>, (xt::layout_type)0>') for 1st argument
self_type& operator=(const self_type& rhs);
xtensor-python/include/xtensor-python/pyarray.hpp:363:20: note: candidate function not viable: no known conversion from 'double' to 'xt::pyarray<std::__1::complex<double>, xt::layout_type::dynamic>::self_type' (aka 'pyarray<std::__1::complex<double>, (xt::layout_type)0>') for 1st argument
self_type& operator=(self_type&& e) = default;
xtensor-python/include/xtensor-python/pyarray.hpp:369:20: note: candidate template ignored: could not match 'xexpression<type-parameter-0-0>' against 'double'
self_type& operator=(const xexpression<E>& e);
Answer is to do this
using value_t = typename xtarray_t::value_type;
output = value_t(0);

How do I convert array<unsigned char> to an unsigned char[]?

In a CLR project I have the output of AesManaged class as a 16 byte array
array<unsigned char>^ result = msEncrypt->ToArray();
However I need to convert this to an array of type unsigned char defined like this
unsigned char buff[16];
EDIT: I did try this but its giving error (no method signature with those parameters, although there is one)
System::Runtime::InteropServices::Marshal::Copy(result, 0, buff, 16);
And this one
buff = reinterpret_cast<unsigned char>(&result);
But the error is Expression must be a modifiable lvalue
According to this MSDN documentation I used this and it appears to work
pin_ptr<unsigned char>buff = &result[0];

Copy two dimensional arrays using pointers

I want to copy two dimensional arrays and I made this function but it caused a compilation error
void Cpy_2d_arr(unsigned char *from,unsigned char *to)
{
unsigned char col,row;
for (row=0;row<4;row++)
{
for(col=0;col<4;col++)
{
(*(*(to+row)+col)) = (*(*(from+row)+col));
}
}
}
The two dimensional arrays are
unsigned char arr[4][4] = {'7','8','9','-','4','5','6','*','1','2','3','-','c','0','=','+'};
the target is an array inside a struct with the same size
the errors are :
1- Error 8 expected 'unsigned char ' but argument is of type 'unsigned char ()[4]
2- Error 11 subscripted value is neither array nor pointer
3- Error 11 invalid type argument of unary '*' (have 'int')
char arr[4][4] = {'7','8','9','-','4','5','6','*','1','2','3','-','c','0','=','+'};
is an array of pointers
void Cpy_2d_arr(unsigned char *from,unsigned char *to)
receives an single pointer( e.g. to an array of chars).
the types of your parameters are not compatible, change it to
void Cpy_2d_arr(unsigned char from[4][4],unsigned char to[4][4])
it should work just fine

MessageBoxW cannot convert

I am using wxWidgets 2.9.4 in Visual Studio 2012 and I keep getting these two error messages:
Error 1 error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char *' to 'LPCWSTR'
IntelliSense: argument of type "const char *" is incompatible with parameter of type "LPCWSTR"
My code is:
#ifdef _WIN32
std::string msg;
StringFromFormatV(&msg, format, args);
retval = IDYES == MessageBox(0, msg.c_str(), "ERROR! Continue?", MB_ICONQUESTION | MB_YESNO);
You are compiling your project using multi-byte characters as default. You can change that in your project's properties, or you can use msg.wc_str(), or even enforce the use of MessageBoxA instead of using the macro MessageBox.