You can solve this by casting to the expected type: int wordvalue = hash((unsigned char.)buffer); Another option is performing the cast inside your hash function.
For below code how do I pass
char[][]
as char**
parameter?Getting below error:
If I change my code in
main()
as:It works fine, but I want to know how do I pass
char[][]
as parameter.it does print :
Two
So if I pass var
to char**
; won't it be equivalent to two dimentional array's address?I mean for a function
fn(char*)
we do pass as:char**
is making me confused 1,56944 gold badges2727 silver badges5252 bronze badges
![Passing Passing](https://i.stack.imgur.com/892CU.jpg)
2 Answers
If you dereference a
char **
, you should get a pointer to char
. There are no pointers in a char[3][10]
. They're just not interchangeable.It works for a one-dimensional
char *
array because the array name implicitly converts to a pointer to the first element in this context, and if you dereference a char *
you get a char
, and that's exactly what your one-dimensional char
array contains. The line of thinking that 'if it works for a one-dimensional array, shouldn't it work for a two-dimensional array?' is just invalid, here.
To make what you want to do work, you'd have to create
char *
arrays, like so:which outputs:
An alternative is to declare your function as accepting an array of (effectively a pointer to) 10-element arrays of
char
, like so:but this obviously requires hard-coding the sizes of all dimensions except the leftmost, which is usually less desirable and flexible.
Paul GriffithsPaul Griffithschar argv[][10]
in the parameter list here is another way of writing char (*argv)[10]
, i.e. declaring argv
as a pointer to array of char
of size 10
.19.3k22 gold badges3030 silver badges5252 bronze badges
A
type**
is by definition a pointer to a pointer, or an array of pointers.When used in a function declaration,
type[][]
is inteligible with type**
as in:But it is not when declaring variables. When you do this:
This is a matrix of
char
and there are no pointers envolved here. The compiler pretty much handles this as a straight forward array of char var[a*b]
and when you access var[1][2]
it internally does var[1*b+2]
. It is not really an array of strings, and its utilization is only possible because the compiler knows the value of b
when compiling this function, as the variable was declared there.In order to pass as
char**
, var
must be an array of pointers. A list containing pointers of char*
.When you do this:
Now you are telling the compiler your intention.
HavenardHavenardvar
is no longer a matrix of char
, it is truly a list of pointers char*
.19k22 gold badges2626 silver badges4848 bronze badges