how to pass array to this function ?
this is the function :
void fire(const uint8_t *const s[])
{
cout<<*s<<endl;
}
and I want to pass this array to that :
unsigned char X[10] = {255,255,255,255};
it is done by this and it works
unsigned char X[5] = {255,255,255,255};
unsigned char *pointertoX ;
pointertoX = X;
fire(&pointertoX);
why I need *pointertoX ?
is there any other way for do this ?
whole code :
#include <iostream>
using namespace std;
void fire(const uint8_t *const s[])
{
cout<<*s<<endl;
}
int main() {
unsigned char X[10] = {255,255,255,255};
unsigned char *pointertoX ;
pointertoX = X;
fire(&pointertoX);
return 0;
}
note : I'm trying to pass bitmap to ffmpeg "sws_scale" ..
https://ffmpeg.org/doxygen/4.1/group__libsws.html#gae531c9754c9205d90ad6800015046d74
this is the function :
void fire(const uint8_t *const s[])
That function accepts a pointer to a const pointer to a const uint8_t.
and I want to pass this array to that :
unsigned char X[10] = {255,255,255,255};
You cannot.
In order to pass an array into a function that accepts a pointer, the function would have to accept a pointer to element type of that array (after other implicit conversions such as pointer from to non-const into pointer to const). The element of that array is unsigned char, while the function accepts a pointer to a const pointer to a const uint8_t.
why I need *pointertoX ?
Because the function accepts a pointer to a const pointer to a const uint8_t, and &pointertoX is a pointer to a pointer to an unsigned char. Given that uint8_t is an alias of unsigned char, &pointertoX is implicitly convertible to the function parameter.
note : I'm trying to pass bitmap to ffmpeg "sws_scale" ..
Read the documentation carefully:
srcSlice the array containing the pointers to the planes of the source slice
dst the array containing the pointers to the planes of the destination image
You're trying to pass an array of characters into a function that expects an array of pointers.
P.S. The behaviour of the program is undefined because *s does not point to a null terminated string, but you insert it into a character stream which has such requirement.
Related
I am currently working with a C-API (OpenSSL) and there are a lot functions declared like the following:
// From bytes to TYPE
TYPE *d2i_TYPE(TYPE **a, unsigned char **ppin, long length);
// From TYPE to bytes
int i2d_TYPE(TYPE *a, unsigned char **ppout);
But sometimes, functions expect a pointer to an array of pointers to some const type:
// From bytes to TYPE
TYPE *d2i_TYPE(TYPE **a, const unsigned char **ppin, long length);
Notice the const unsigned char ** vs unsigned char ** and ignore the functions' first argument.
Now consider the following example, where some input data is given as non-const pointers. What is the correct way of casting the array of pointers to an array of pointers const? And also, since the function expects a pointer, the parameter must be an lvalue. I am guessing this is the reason there is no implicit cast..
int length = 42;
unsigned char * data = new unsigned char[length];
// Error C2664: cannot convert argument 2 from 'unsigned char **' to 'const unsigned char **'
TYPE * my_typeE = d2i_TYPE(nullptr, &data, length);
// My solution:
auto x = reinterpret_cast<unsigned char const *>(data);
auto y = const_cast<unsigned char const *>(data);
TYPE * my_typeX = d2i_TYPE(nullptr, &x, length);
TYPE * my_typeY = d2i_TYPE(nullptr, &y, length);
Are there other solutions that are possibly easier to read?
No need of cast:
const unsigned char* z = data;
[[maybe_unused]]TYPE * my_typeZ = d2i_TYPE(nullptr, &z, length);
Demo
I have this function
unsigned char NCN_System_upload(unsigned char *data, unsigned char len);
I have an array defined as such:
unsigned char data[3];
I'm using the function as the following:
NCN_System_upload(&data, 3);
However, I get the following error:
argument of type unsigned char (*)[3] is incompatible with parameter of type unsigned char*
Why doesn't this work?
The name of the array is already (can be implicitly converted) to a pointer to its beginning. Thus, you should do it like this:
NCN_System_upload(data, 3);
I want to ask if there is difference between this two array passing method:
unsigned char array[100];
function(array);
Where:
library.cpp
uint8_t LibraryClass::function(unsigned char array[]) { }
library.h
uint8_t function(unsigned char array[]);
And this:
unsigned char array[100];
function(array);
Where:
library.cpp
uint8_t LibraryClass::function(const unsigned char* array) { }
library.h
uint8_t function(const unsigned char* array);
My questions is:
There is difference between this to methods?
Additional question:
My MCU need to do additional operation in method 1 instead of method 2?
There is additional const in method 2, why is used? It is safety to use const when using pointers?
unsigned char array[] is just syntactic sugar for unsigned char *array in a function declaration. They're literally identical.
The const means that function guarantees not to modify the contents of array. The first example makes no such guarantee to the caller.
Normally in c/c++ unsigned char array[] is unsigned char* array which consists of contiguous memory.
Whenever an array is passed to a function it is treated as pointer(*) denoting the base address.
Mentioning const- (const unsigned char* array) denotes its value will not change.
I have a class with a method with the following signature:
void print(unsigned char *word);
I need to set "" as default value for word, how can I do that?
I tried the obvious void print(unsigned char *word=""); but I got the following error:
error: cannot initialize a parameter of type
'unsigned char *' with an lvalue of type 'const char [1]'
void print(unsigned char *word="");
Since I can't initialize word with a string literal who should I do it?
You say that this is a "prefix" argument to apply to the printing.
The answer is that you should make the argument const, stop doing whatever mutations you're doing to it inside the function, and then use "" as a default argument:
void print(const char* prefix = "")
try
unsigned char empty[] = { 0 };
void print(unsigned char* word = empty )
{
...
}
"" yields an array of const char, whereupon you want an array of or pointer to NON-const unsigned char, both the type and the cv-qualification don't fit.
Note also that in C++ char != signed char and char != unsigned char.
Possibly you mean void print(const char *word);, but probably you want just print(std::string const &) or print(std::string).
void print();
void print(unsigned char* prefix);
// in cpp file:
void print(){
unsigned char temp = 0;
print(&temp);
}
This provides two overloads, one with 0 and one eith one argument.
The zero argument one has some automatic storage memory it provides to the one argument one. Note that only the single byte under the pointer is valid to read/write: without a length, print has no way to know any different anyhow.
While there is no array, none is needed for a single element.
I have this array
unsigned char bit_table_[10][100];
What is the right way to fill it with 0.
I tried
std::fill_n(bit_table_,sizeof(bit_table_),0x00);
but vc 2010 flags it as error.
On initialization:
unsigned char bit_table_[10][100] = {};
If it's a class member, you can initialize it in the constructor, like this:
MyClass::MyClass()
:bit_table_()
{}
Otherwise:
std::fill_n(*bit_table_,sizeof(bit_table_),0);
The type of bit_table_ is unsigned char [10][100], which will decay (that is, the compiler allows it to be implicitly converted to) into unsigned char (*)[100], that is, a pointer to an array of 100 unsigned chars.
std::fill_n(bit_table_, ...) is then instantiated as:
std::fill_n(unsigned char (*)[100], ...) which means it expects a value of type unsigned char [100] to initialize bit_table_ with. 0 is not convertible to that type, so the compilation fails.
Another way to think about it is that the STL functions that deal with iterators only deal with a single dimension. If you are passing in a multidimensional structure those STL functions will only deal with a single dimension.
Ultimately, you can't do this; there is no way to assign to an array type. I.e., since you can't do this:
char table[100];
char another_table[100]= { };
table= another_table;
you can't use std::fill_n on multidimensional arrays.
You can also try unsigned char bit_table_[10][100]= { 0 } to fill it with zeros.
int main()
{
unsigned char bit_table_[10][100]= { 0 };
return 0;
}