Pointer Functions in C: initialization of foo from incompatible pointer type - c

I'm working with C, and not allowed to use C++. Currently, I'm trying to implement some level of OOP in C. I'm currently working on trying to implement polymorphism and inheritance.
I've spent the majority of the day reading up on how my goals are possible through the use of function pointers. I am attempting to print the members variables of both structs as seen here:
RecordObject.h
typedef struct SuperRecordObject
{
char *Id;
char *date;
char *cases;
char *deaths;
void (*ptrPrintRecord)(char *id, char *date, char *cases, char *deaths, char *names_fr, char *names_en);
} SuperRecord;
typedef struct ChildRecordObject
{
SuperRecord super;
char *names_fr;
char *names_en;
} ChildRecord;
I have defined the function ptrPrintRecord in this file:
RecordObject.c
#include <stdio.h>
#include "RecordObject.h"
void ptrPrintRecord(char *id, char *date, char *cases, char *deaths, char *names_fr, char *names_en)
{
//char *record;
printf(" %s | %s | %s | %s | %s | %s\n", id, date, cases, deaths, names_fr, names_en);
//return record;
}
And I try to use the function in this file, as such:
DataLayer.c
#include <stdio.h>
#include <string.h>
#include "RecordObject.h"
/* more code here */
void(*fun_ptr)(char*,char*,char*,char*,char*,char*) = &record.super.ptrPrintRecord; //
(*fun_ptr)(record.super.Id, record.super.date, record.super.cases, record.super.deaths, record.names_fr, record.names_en);
/* more code here */
However, when I compile (using GCC), I get this warning which causes a crash.
warning: initialization of 'void (*)(char *, char *, char *, char *, char *, char *)' from incompatible pointer type 'void (**)(char *, char *, char *, char *, char *, char *)' [-Wincompatible-pointer-types]
62 | void(*fun_ptr)(char*,char*,char*,char*,char*,char*) = &record.super.ptrPrintRecord;
I've ran some other pointer functions in other files to mess around and test it, and the only thing I can think of as to what's going on here is it's maybe got something to do with how strings work in C?

You have an extraneous & in your attempted function pointer assignment. The ptrPrintRecord member of your structure is already a function pointer of the correct type, so you don't need the & - which would give the address of that pointer.
Just use:
void(*fun_ptr)(char*, char*, char*, char*, char*, char*) = record.super.ptrPrintRecord; // No &
As a side note, your use of ptrPrintRecord as that member (function pointer) and also as the name of an actual function (with the same 'signature') is likely to cause some issues, further down the road.
Furthermore, you need to actually initialize that member (pointer) to a valid function address before copying it to something you then call (as also with the other members of the structure). Here's a small main (using your other code) that works:
int main()
{
ChildRecord record;
record.super.ptrPrintRecord = ptrPrintRecord; // See my note about the name clash!
record.super.Id = "ID";
record.super.date = "today";
record.super.cases = "cases";
record.super.deaths = "deaths";
void(*fun_ptr)(char*, char*, char*, char*, char*, char*) = record.super.ptrPrintRecord; //
// To call the pointed-to function, we can just use the pointer name:
fun_ptr(record.super.Id, record.super.date, record.super.cases, record.super.deaths, record.names_fr, record.names_en);
return 0;
}

Related

How to fix function calling warnings in c?

struct reviewStruct {
char reviewer[MAX_STR];
int feedback[NUMBER_MOVIES];
};
int readMovies(FILE *file, char movieNames[NUMBER_MOVIES][MAX_STR])
int readReviews(FILE *file, struct reviewStruct reviews[NUMBER_REVIEWERS])
int main() {
FILE *file;
char movieNames[NUMBER_MOVIES][MAX_STR];
char reviews [NUMBER_REVIEWERS];
myFile("cisfile.txt");
readMovies(file, movieNames);
readReviews(file, reviews);
}
This is my main function along with the function definitions, when I compile the program I get the following errors:
"warning: passing argument 2 of ‘readReviews’ from incompatible pointer type" for the line: "readReviews(file, reviews);"
"warning: ‘file’ is used uninitialized in this function" for the line: "readMovies(file, movieNames);"
How do I fix these two errors?
I took your code and tried to make it run.
#include <stdio.h>
#include <stdlib.h>
#define MAX_STR 10
#define NUMBER_MOVIES 3
#define NUMBER_REVIEWERS 2
struct reviewStruct {
char reviewer[MAX_STR];
int feedback[NUMBER_MOVIES];
};
int readMovies(FILE *file, char movieNames[NUMBER_MOVIES][MAX_STR])
{
printf("hi\n");
}
int readReviews(FILE *file, void *reviews)
{
printf("bye\n");
}
int main(void)
{
FILE *myFile;
char movieNames[NUMBER_MOVIES][MAX_STR];
char reviews [NUMBER_REVIEWERS];
myFile = fopen("cisfile.txt","r");
readMovies(myFile, movieNames);
readReviews(myFile, reviews);
fclose(myFile);
return 0;
}
With these assumptions runs without any warnings.
I am using gcc 8.3.0 without any options.
1. First warning:
As per what #kaylum said. You are declaring char reviews[MUMBER_REVIEWERS], but when you defined the parameters for your function: int readReviews(FILE *file, struct reviewStruct reviews[NUMBER_REVIEWERS], it is clear that the parameter concerned by the array should be of type struct reviewStruct.
So in the line where you got the warning change: char reviews [NUMBER_REVIEWERS]; to struct reviewStruct reviews[NUMBER_REVIEWERS];. That should be a pointer to a structure type, not a pointer to a character type. Or if you want to go more in detail: It is a pointer to an array of structures, the type of the array is determined by the type of elements it holds.
2. Second warning:
Now let's take a look at the second warning: warning: ‘file’ is used uninitialized in this function" for the line: "readMovies(file, movieNames)
As the warning says. Your pointer file was not initialized. Take it as a rule, that you cannot use a pointer without initializing it to something to point to. I believe that you are trying to read from a stream. So just in case you need it, you perhaps may be willing to use the function fopen(), which returns a pointer to a stream.

C struct pointers return

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_BUFFER_SIZE 100
typedef struct config_struct config_t ;
struct config_struct{
char name[20];
char ip[20];
int port;
char log[100];
};
int main(int argc, char *argv[]) {
config_t config;
read_config_file(argv[1], &config);
return 0;
}
int read_int_from_config_line(char* config_line) {
int val = atoi(config_line);
return val;
}
void read_str_from_config_line(char* config_line, char* val) {
strncpy(val,config_line, MAX_BUFFER_SIZE);
}
void read_config_file(char* config_filename, config_t *config) {
FILE *fp;
char *value, buffer[MAX_BUFFER_SIZE];
fp = fopen("./settings.conf", "r");
while (fgets(buffer, MAX_BUFFER_SIZE, fp) != NULL) {
if(!strcmp(buffer,"NAME")) {
read_str_from_config_line(value, &(config->name));
}
if(!strcmp(buffer,"PORT")) {
&(config->port) = read_int_from_config_line(value);
}
}
}
I try to compile this code and it gives me a incompatible pointer type at read_str_from_config_line(value , &(config->name)); and lvalue required as left operand of assignment &(config->port) = read_int_from_config_line(value);
I am trying to return the stuct back to the main program but I'm having problem with the struct pointers.
Any solution for it to be solve?
PS: The code is a mess as I trying to shorten it. Any help will be good as I am still a beginner to C programming.
Thanks
The & operator means "take the address of the object to the right".
This:
&(config->port)
means "take the address of the port member of the structure pointed to by config.
You don't want to take the address, you just want to assign to the member. That's just:
config->port = ...
This is the same as (*config).port, but nobody writes it like that since the arrow operator is so convenient.
read_str_from_config_line(value, &(config->name));
confg->name is declared as a char array, which naturally decays to a pointer to the first element (of type "pointer-to-char") if you omit the &:
read_str_from_config_line(value, config->name);
Taking its address explicitly instead results in a pointer of type pointer-to-array-of-20-chars, which is not the same type as pointer-to-char - which is why you are getting an error. This is a common source of confusion in C; in general, you should not take the address of an array (although it does have legitimate use cases).
In this line:
&(config->port) = read_int_from_config_line(value);
You are taking the address of the port member of the structure pointed at by config, which is a non-lvalue pointer. It looks like you are actually trying to assign to the member, so the line should read:
config->port = read_int_from_config_line(value);

typedef char array as function parameter

C newbie here.
I wrote:
typedef unsigned char HBLOCK[SHA512_DIGEST_LENGTH];
typedef unsigned char HPRINTABLE[sizeof(HBLOCK)*2];
typedef struct {
HBLOCK Primary;
HBLOCK *Part;
} FILE_HASH_MAP;
void blockhex(const HBLOCK *, HPRINTABLE *);
intending to write the function to convert HBLOCK to hex string. However when I tried to test if individual characters are assignable
void blockhex(const HBLOCK *block, HPRINTABLE *printable)
{
printable[0]="6";
};
The compiler said
error: incompatible types when assigning to type ‘HPRINTABLE’ from type ‘char *’.
This puzzles me as I hoped the HPRINTABLE typedef would clearly indicate it to be an array type.
Moreover, when I call it like this
FILE_HASH_MAP fhm;
HPRINTABLE prt;
blockhex(fhm.Primary,&prt);
The compiler also says
warning: passing argument 1 of ‘blockhex’ from incompatible pointer type
and
note: expected ‘const unsigned char (*)[64]’ but argument is of type ‘unsigned char *’
those are not errors, of course, but I'd like to know what I did wrong anyway.
void blockhex(const HBLOCK *block, HPRINTABLE *printable)
{
printable[0]="6";
printable[0] is a char-array with sizeof(HBLOCK)*2 elements. "6" is a char-array as well (with 2 elements, that is '6' and '\0').
In C arrays cannot be assigend.
To copy arrays in general use memcpy().
memcpy(printable[0], "6", sizeof "6"); /* Note that sizeof "6" evaluates to 2,
because of the `0`-terminator each
C-"string" carries. */
To copy a C-"string" it better/easier to use strcpy() instead, like:
strcpy(printable[0], "6");
The above is equivalent to:
printable[0][0] = '6';
printable[0][1] = '\0';
More on how to handle "strings" in C is here: http://en.wikipedia.org/wiki/C_string_handling
To have printable being a char-array declare it as HPRINTABLE printable and do not use the address-of operator & on prt when calling blockhex();.
Same for block.

C issues with pointers

I'm learning about the pointers in C. I don't understand why this code fails during the compilation process.
#include <stdio.h>
void set_error(int *err);
int main(int argc, const char * argv[])
{
const char *err;
set_error(&err);
return 0;
}
void set_error(int *err) {
*err = "Error message";
}
You declare the function to expect a pointer-to-int (int *). But you give it a pointer-to-pointer-to-char and set_error treats it as such. Change the declaration thusly:
void set_error(const char ** err)
If you had compiled with warnings enabled (-Wall for GCC) it would give the following warnings:
In function 'main':
warning: passing argument 1 of 'set_error' from incompatible pointer type [enabled by default]
set_error(&err);
^
note: expected 'int *' but argument is of type 'const char **'
void set_error(int *err);
^
In function 'set_error':
warning: assignment makes integer from pointer without a cast [enabled by default]
*err = "Error message";
^
Your function expects int * type argument but you are passing to it const char ** type argument.
Change your function declaration to
void set_error(const char **err);
The issue you have unearths an important facts about strings in C.
It also raises an interesting fact about scoping.
1. There is no such thing as a string in C; only a pointer to an array of characters.
Therefore, your statement *err = "Error message"; is wrong because by derefencing err you're not getting to the value of the string, but it's first character. (You can't quantify the 'value of a string' in C because there's no such thing as a string in C)
*err is actually undefined because nothing is yet assigned.
Note that the usual definition of a string is const char * or char * so I've changed this from what you had for the note below:
#include <stdio.h>
int main(void){
char * a = "hello";
if (*a == 'h'){
printf("it's an 'H'\n");
}
else{
printf("no it isn't\n");
}
}
You'll see that *err actually returns the value of the first character because a[0] == *a
2. You cannot return pointers to locally scoped data in C
set_error() has the correct intentions, but is doomed to fail. Although "Error message"looks like a value, it is actually already a pointer (because strings in C are pointers to character arrays, as mentioned above).
Therefore, taking (1) into account you might expect to be able to do this:
void set_int(int *myint) {
*myint = 1; //works just fine because 1 is a value, not a reference
}
void set_error(char *err) {
// doesn't work because you're trying to assign a pointer to a char
*err = "Error message";
void set_error_new(char *err) {
//doesn't work because when the function returns, "Error Message" is no longer available on the stack" (assignment works, but when you later try to get at that data, you'll segfault
err = "Error message";
}
You need to take a different approach to how you play with so-called 'strings' in C. Think of them as a pointer to a character array and you'll get better at understanding these issues. Also see C: differences between char pointer and array
One problem is that set_error expects an int * parameter, but you're passing the address of a char *, which makes it a char **. In addition, as noted by #Kninnug there's a buffer overwrite problem here which needs to be dealt with. Try rewriting your code as:
#include <stdio.h>
#include <string.h>
void set_error(char *err, size_t errbuf_size);
int main(int argc, const char * argv[])
{
char err_buf[1000];
set_error(err_buf, sizeof(err_buf));
printf("err_buf = '%s'\n", err_buf);
return 0;
}
void set_error(char *err, size_t errbuf_size) {
strncpy(err, "Error message", errbuf_size-1);
}
As you'll notice in the rewritten version of set_error, another problem is that you can't just assign a value to a pointer and have the target buffer changed - you need to use the string functions from the standard library (here I'm use strncpy to copy the constant "Error message" to the buffer pointed to by the char * variable err). You may want to get familiar with these.
Share and enjoy.
Firstly you have to change your function's declaration to
void set_error(char **err);
The body of the function is the same. Also you declared err variable as const char *err and tried change it. It generates a warning.
Let's start by talking about types. In your main function, you declare err as
const char *err;
and when you call the set_error function, you pass the expression &err, which will have type "pointer to const char *", or const char **.
However, in your function declaration and definition, you declare the parameter err as
int *err;
The types const char ** and int * aren't compatible, which is why the compiler is yakking. C doesn't allow you to assign pointer values of one type to pointer variables of a different type (unless one is a void *, which is a "generic" pointer type). Different pointer types are not guaranteed to have the same size or representation on a particular platform.
So that's where the compiler issue is coming from; what's the solution?
In C, string literals like "Error message" have type char *1 (const char * in C++), so whatever I assign it to needs to have a type of either char * or const char *. Since we're dealing with a string literal, the latter is preferable (attempting to modify the contents of a string literal invokes undefined behavior; some platforms put string literals in read-only memory, some don't). So you need to make the following changes to your code2:
void set_error( const char **err )
{
*err = "Error message";
}
int main( void ) // you're not dealing with command line arguments, so for this
{ // exercise you can use `void` for your parameter list
const char *err;
set_error( &err );
return 0;
}
Remember that C passes all function arguments by value; this means that the formal parameter err in set_error is a different object in memory than the actual parameter err in main; if the code had been
void set_error( const char *err )
{
err = "Error message";
}
int main( void )
{
const char *err;
set_error( err );
return 0;
}
then the change to err in set_error would not be reflected in the variable err in main. If we want set_error to modify the value of err in main, we need to pass set_error a pointer to err and dereference it in the function. Since the parameter err has type const char **, the expression *err has type const char *, which is the type we need for this assignment to succeed.
1. Actually, that's not true; string literals have type "N-element array of char", where N is the number of characters in the string plus the 0 terminator. However, for reasons that aren't really worth going into here, the compiler will convert expressions of array type to expressions of pointer type in most circumstances. In this case, the string literal "Error message" is converted from an expression of type "14-element array of char" to "pointer to char".
2. A function definition also serves as a declaration; I typically put the called function before the caller so I don't have to mess with separate declarations. It means my code reads "backwards" or from the bottom up, but it saves some maintenance headaches.
1st error--> You are noticing is due to the fact that your function expects a pointer to int and you are passing a pointer to const char
2nd error--> You dereferenced the pointer and inserted the value "Error Message" which is a string and you pointer was pointer to char.
3rd error--> set_error(&err); --> This statement is wrong as err itself stores an address so there is no need to put & putting & means you are passing the address of the pointer *err and not the address which it is holding. So try this.
include <stdio.h>
void set_error(const char* err[]); //Function Declaration
int main()
{
const char* err[1000];
set_error(err);
printf("%s",*err);
return 0;
}
void set_error(const char* err[])
{
*err = "Error Message";
}

idiomatic C for const double-pointers

I am aware that in C you can't implicitly convert, for instance, char** to const char** (c.f. C-Faq, SO question 1, SO Question 2).
On the other hand, if I see a function declared like so:
void foo(char** ppData);
I must assume the function may change the data passed in.
Therefore, if I am writing a function that will not change the data, it is better, in my opinion, to declare:
void foo(const char** ppData);
or even:
void foo(const char * const * ppData);
But that puts the users of the function in an awkward position.
They might have:
int main(int argc, char** argv)
{
foo(argv); // Oh no, compiler error (or warning)
...
}
And in order to cleanly call my function, they would need to insert a cast.
I come from a mostly C++ background, where this is less of an issue due to C++'s more in-depth const rules.
What is the idiomatic solution in C?
Declare foo as taking a char**, and just document the fact that it won't change its inputs? That seems a bit gross, esp. since it punishes users who might have a const char** that they want to pass it (now they have to cast away const-ness)
Force users to cast their input, adding const-ness.
Something else?
Although you already have accepted an answer, I'd like to go for 3) namely macros. You can write these in a way that the user of your function will just write a call foo(x); where x can be const-qualified or not. The idea would to have one macro CASTIT that does the cast and checks if the argument is of a valid type, and another that is the user interface:
void totoFunc(char const*const* x);
#define CASTIT(T, X) ( \
(void)sizeof((T const*){ (X)[0] }), \
(T const*const*)(X) \
)
#define toto(X) totoFunc(CASTIT(char, X))
int main(void) {
char * * a0 = 0;
char const* * b0 = 0;
char *const* c0 = 0;
char const*const* d0 = 0;
int * * a1 = 0;
int const* * b1 = 0;
int *const* c1 = 0;
int const*const* d1 = 0;
toto(a0);
toto(b0);
toto(c0);
toto(d0);
toto(a1); // warning: initialization from incompatible pointer type
toto(b1); // warning: initialization from incompatible pointer type
toto(c1); // warning: initialization from incompatible pointer type
toto(d1); // warning: initialization from incompatible pointer type
}
The CASTIT macro looks a bit complicated, but all it does is to first check if X[0] is assignment compatible with char const*. It uses a compound literal for that. This then is hidden inside a sizeof to ensure that actually the compound literal is never created and also that X is not evaluated by that test.
Then follows a plain cast, but which by itself would be too dangerous.
As you can see by the examples in the main this exactly detects the erroneous cases.
A lot of that stuff is possible with macros. I recently cooked up a complicated example with const-qualified arrays.
2 is better than 1. 1 is pretty common though, since huge volumes of C code don't use const at all. So if you're writing new code for a new system, use 2. If you're writing maintenance code for an existing system where const is a rarity, use 1.
Go with option 2. Option 1 has the disadvantage that you mentioned and is less type-safe.
If I saw a function that takes a char ** argument and I've got a char *const * or similar, I'd make a copy and pass that, just in case.
Modern (C11+) way using _Generic to preserve type-safety and function pointers:
// joins an array of words into a new string;
// mutates neither *words nor **words
char *join_words (const char *const words[])
{
// ...
}
#define join_words(words) join_words(_Generic((words),\
char ** : (const char *const *)(words),\
char *const * : (const char *const *)(words),\
default : (words)\
))
// usage :
int main (void)
{
const char *const words_1[] = {"foo", "bar", NULL};
char *const words_2[] = {"foo", "bar", NULL};
const char *words_3[] = {"foo", "bar", NULL};
char *words_4[] = {"foo", "bar", NULL};
// none of the calls generate warnings:
join_words(words_1);
join_words(words_2);
join_words(words_3);
join_words(words_4);
// type-checking is preserved:
const int *const numbers[] = { (int[]){1, 2}, (int[]){3, 4}, NULL };
join_words(numbers);
// warning: incompatible pointer types passing
// 'const int *const [2]' to parameter of type 'const char *const *'
// since the macro is defined after the function's declaration and has the same name,
// we can also get a pointer to the function
char *(*funcptr) (const char *const *) = join_words;
}

Resources