expected ‘uint32_t’ but argument is of type ‘uint32_t *’ - c

I am new in C, trying to call a function, but it gives me error that I can not understand why
int set_price(&colour->type.name);
it returns me expected ‘uint32_t’ but argument is of type ‘uint32_t *’. warning: passing argument ‘int set_price’ makes integer from pointer without a cast
where the pointer is
house_list *colour = NULL;
and
name is defined in struct as
uint32_t name;
the original function accept
int set_price(uint32_t name) { /do something here/ }
what do I do wrong? If in the struct member, name is defined as uint32_t, and I defined a pointer colour, than I believe that I need to use & before colour->type and use dot before name isn't it?
Thank you

set_price(&colour->type.name);
remove the & and you'll be fine
set_price(colour->type.name);
set_price expects an integer as an argument, not a pointer to integer.
I suggest that you should read a good C book.

Related

Generic function typedef in C - how to get rid of initialization warning?

I'm experimenting with generic-like code and I have a function like this (a lot of not relevant code removed):
typedef uint8_t (*struct_converter_t)(void *, char *);
void convert_struct(
struct_converter_t converter, // this is a function
const char * file_name
){
some_struct_t * some_struct;
converter(some_struct, some_string_buffer);
}
And when I try to assign a function that takes some_struct_t (not void *):
static uint8_t some_converter(some_struct_t * vd, char * s);
to my struct_converter_t like this:
struct_converter_t converter = some_converter; // WARNING HERE
I'm getting this:
initialization of 'struct_converter_t' {aka 'unsigned char (*)(void *, char *)'} from incompatible pointer type 'uint8_t (*)(some_struct_t *, char *)' {aka 'unsigned char (*)(struct <anonymous> *, char *)'} [-Wincompatible-pointer-types]
I'm not experienced in C and I would like to know if there is a way to get rid of this warning elegantly.
The function that you're assigning to the function pointer type has parameters that are incompatible with the function pointer.
Your function takes a some_struct_t * as the first parameter but the function pointer type takes a void * as the first parameter. While any object pointer can be converted to/from a void *, that does not extend to function pointer parameters.
You need to change your function to take a void * for its first parameter to be compatible with the function pointer. Then inside the function you can convert that void * parameter to a some_struct_t *.
In C, you can supply an argument of type X* to a function expecting a void*. But that's as far as the conversion goes. You cannot take a function whose first parameter is an X* and use it as the argument for a parameter which is a function whose first parameter is a void*.
The reason is that C does not guarantee that X* and void* have the same representation. It does guarantee that the compiler knows how to convert an X* into a void* in a way that does not destroy information, so that it can be later converted back to an X*. But the void* might look quite different.
So the compiler can insert code which changes the X* to a void*. But how does it change a char*(*)(X*) (a function whose parameter is an X*) to a char*(*)(void*)? If the function is expecting that arg has type char*(*)(void*), then it will mostly likely call arg(v) where v has type void*. What then happens if arg actually expects an X*?
In order for the compiler to allow that possibility, it would have to somehow wrap arg in what's usually called a trampoline; a function which accepts an X* and converts it into a void* in order to call a different function. That's not so easy -- for one thing, it would have to store the trampoline somewhere -- so C refuses to do it as an automatic conversion.

How to solve this pointer error? (from incompatible pointer type)

I've this function:(myvariable is un unsigned int)
config_setting_lookup_int64 is the libconfig.h function -> int config_lookup_int64 (const config t * config, [Function]
const char * path, long long * value)
config_setting_lookup_int64(file, "myvariable", &myvariable);
I can see this warning: passing argument 3 of 'config_setting_lookup_int64' from incompatible pointer type
To solve the problem, I tried to write:
config_setting_lookup_int64(file, "myvariable", myvariable);
But I can see another warning: passing argument 3 of 'config_setting_lookup_int64' makes pointer from integer without a cast. How can I solve the problem? Thank you so much!

Incompatible types when assigning to type 'char[100][100]' from type 'char *' error in 2d array

I found questions for similar errors having char[100]; but they didn't help me. I only know some basic C language.
This is my C code to store a person's name and number in a 2D array.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char name[100][100];
char number[100][100];
}
person;
int main(void) {
setvbuf(stdout,NULL,_IONBF,0);
setvbuf(stderr,NULL,_IONBF,0);
person people[3];
int i;
people[0].name = "Abu";
people[0].number = "+91-123456789";
people[1].name = "Tony";
people[1].number = "+1-4432788878";
people[2].name = "Simon";
people[2].number = "+42-432432233";
for(i=0;i<=2;i++){
if(strcmp(people[i].name,"Simon")==0){
printf("Found %s\n",people[i].number);
return 0;
}
}printf("Not found\n");
return 1;
}
This program shows 6 error and 2 warnings:
error: incompatible types when assigning to type 'char[100][100]' from type 'char *'
error: incompatible types when assigning to type 'char[100][100]' from type 'char *'
error: incompatible types when assigning to type 'char[100][100]' from type 'char *'
error: incompatible types when assigning to type 'char[100][100]' from type 'char *'
error: incompatible types when assigning to type 'char[100][100]' from type 'char *'
error: incompatible types when assigning to type 'char[100][100]' from type 'char *'
typedef struct sample.c:31:3: warning: implicit declaration of function 'strcmp'
typedef struct sample.c:32:4: warning: format '%s' expects type 'char *',
but argument 2 has type 'char (*)[100]'
Any help will be appreciated.
There are several errors in your code, in the lines like the following:
people[0].name = "Abu";
First, you cannot copy C-style character strings like this. Let's look at a simple example:
char dest[100]; // An array of 100 characters;
dest = "This is what I want in 'dest'."; // Error!
The error here is that the RHS of the assignment evaluates to the address of the given string literal and the LHS is an array (which is not a type that can be assigned to).
In this case, you use the strcpy function, like so, to copy the data from the RHS into the array of the LHS:
char dest[100]; // An array of 100 characters;
strcpy(dest,"This is what I want in 'dest'.");
However, in your code, there is another problem: The name and number fields of the person structure are defined as two dimensional arrays of characters (that is, each is an array of 100 strings). I can't see any reason why you would want this – presumably, each person has only one name and one number (and you have multiple person objects in the people array).
So, I would suggest that you redefine your person structure to have only a single (100 character) string for each field, like so:
typedef struct {
char name[100]; // Allows up to 99 characters for each name and number...
char number[100]; // ... 'reserving' one `char` for the REQUIRED nul terminator
} person;
Then you can copy your data to each field with lines like the following:
strcpy(people[0].name, "Abu");
strcpy(people[0].number, "+91-123456789");
strcpy(people[1].name, "Tony");
//... and so forth
Note: If you do actually require multiple names and numbers for each person, then you will need to add the index of the name or number field in the call to strcpy:
strcpy(people[0].number[0], "+91-123456789"); // Sets Abu's FIRST number
strcpy(people[0].number[1], "+91-987654321"); // Sets Abu's SECOND nmuber
Feel free to ask for further clarification and/or explanation.

Getting warning when trying to pass the address in a function

I get the warning:
expected 'const CHAR *' but argument is of type 'CHAR (*)[18]'
Why is the compiler giving me this warning? This is the line I am using to call to the api:
AG_AS(g_AgMgrCtx.audio_device.Profile_Type,&g_AgMgrCtx.SDevAddr);
The declaration of the function is
AG_AS(AG_DType d_type , CHAR *addr);
char SDevAddr[18];
What is the problem with passing the address? When I remove & the warning goes away.
What does the warning actually mean?
That means your call should be (without the &):
AG_AS(g_AgMgrCtx.audio_device.Profile_Type, g_AgMgrCtx.SDevAddr);
g_AgMgrCtx.SDevAddris an array of 18 chars. So when you pass it to a function, it gets converted into a pointer to its first element (thus matching the type that AG_AS() expects).
Relevant: What is array decaying?
When you have an object like this
T obj;
where T is some type then expression &obj has type T * that is it is a pointer to an object of type T.
This array declaration
char SDevAddr[18];
can be transformed to the form shown above using a typedef. For example
typedef char T[18];
T SDevAddr;
In this case expression &SDevAddr has type T * as it has been pointed above. however in this case T in turn an alias for the type char[18]. Thus &SDevAddr is a pointer to an object of type char[18] Its type looks like char ( * )[18].
However as it is seen from the function declaration
AG_AS(AG_DType d_type , CHAR *addr);
the second parameter has type char *. If you will pass as an argument the array itself like SDevAddr then it will be implicitly converted to pointer to its first element and will have type char * that is required for the function call.

Why did these variable apparently change type?

Python spoiled me and trying to wrap my mind around C now is being a bloodbath of stupid errors. This is one I can't quite understand.
I wanted the C equivalent of Python's os.path.split, but there's no exact equivalent. strsep looks similar enough, but needs some massaging to be used simply.
First off, I defined my path type: a string of given length.
#define MAX_PATH_LEN 200 /* sigh */
typedef char t_path[MAX_PATH_LEN];
Then I wrote some code that does the actual massaging, attempting to avoid side effects -- just to keep things fool proof.
typedef struct {
t_path next;
t_path remainder;
} t_path_next
t_path_next path_walk_into(t_path path) {
t_path_next output;
t_path my_next, my_remainder = "/";
strncpy(my_next, path, MAX_PATH_LEN);
strsep(&my_next, my_remainder);
output.remainder = my_remainder;
output.next = my_next;
return output;
}
gcc, however, is not impressed.
badp#delta:~/blah$ gcc path.c -Wall
path.c: In function ‘path_walk_into’:
path.c:39: warning: passing argument 1 of ‘strsep’ from incompatible pointer type
/usr/include/string.h:559: note: expected ‘char ** __restrict__’ but argument is of type ‘char (*)[200]’
path.c:41: error: incompatible types when assigning to type ‘t_path’ from type ‘char *’
path.c:42: error: incompatible types when assigning to type ‘t_path’ from type ‘char *’
I am baffled by the note -- how are char ** and char (*)[200] really different -- but the error is even weirder. I want to assign a variable I declared t_path in a field of type t_path, but I don't get to.
Why is that?
For anybody interest here's the correctly working version of the function:
t_path_next path_walk_into(t_path path) {
t_path_next output;
t_path my_path, delim = "/";
char* my_path_ptr = my_path;
strncpy(my_path, path, MAX_PATH_LEN);
strsep(&my_path_ptr, delim); //put a \0 on next slash and advance pointer there.
if (my_path_ptr == NULL) //no more slashes.
output.remainder[0] = 0;
else
strncpy(output.remainder, my_path_ptr, MAX_PATH_LEN);
strncpy(output.next, my_path, MAX_PATH_LEN);
return output;
}
The errors: You can't directly assign to an array, such as a string, in C. You need to copy char by char, or call str(n)cpy, which does it for you.
For the warning : you are probably already aware that array may decay to pointer. That is, for example, what makes an array acceptable as an argument to a function where a pointer is expected. In your case, what you have is a pointer to an array : there is no reason for such a thing to get converted to a pointer to pointer.
For the record, the C99 standard says (6.3.2.1/3) :
Except when it is the operand of the sizeof operator or the unary & operator, or is a
string literal used to initialize an array, an expression that has type ‘‘array of type’’ is
converted to an expression with type ‘‘pointer to type’’ that points to the initial element of
the array object and is not an lvalue.
You're in the context of a unary & : no conversion for you.
For the error : it has already been answered, but array assignment is not directly possible. You might want to use strcpy or strncpy.

Resources