Warnings with pointer using .cfg file - c

I try to setup a c programm using the libconfig. There is example1.c:
int main()
{
const char **channel;
config_t config;
config_init(&config);
config_read_file(&config, "example.cfg");
if( config_lookup_string(&config,"value.channel",&channel) == CONFIG_FALSE)
{
printf("Failed to read fields\n");
return 1;
}
printf("argumente = %s\n", (char *)channel);
return 0;
}
and the example.cfg file
value = { channel = "hello"; }
if I compile it
gcc example1.c -lconfig
it says:
example1.c:39:3: Warning: delivery of arguments 3 from »config_lookup_string« of a incompatible pointer
/usr/include/libconfig.h:244:26: Detailed: »const char **« expected, but argument has typ »const char ***«
The funny thing is it works... the output is:
argumente = hello
How can I get rid of this warning ?
If I change the decleration to const char *channel and the output printf("argumente = %s\n", channel);, I receive a segfault at start and a warning at compiling like ... Detailed: »const char **« expected, but argument has typ »const char *«

You just need to get rid of one * in your declaration of channel. If you do that, you can also remove the cast in your printf call.
The reason it's "working" now is that cast is hiding a second warning about your printf format. Your program is behaving just like the extra * was removed already - just in a confusing way.

Related

ffmpeg, 'protocol not found' error

i'm using next code:
const char *sFileOutput;
AVOutputFormat *ofmt;
AVFormatContext *ofcx;
int main( int argc, char* argv[] )
{
av_log_set_level( AV_LOG_DEBUG );
av_register_all();
avcodec_register_all();
avformat_network_init();
char s1[40]={0};
const time_t timer = time(NULL);
u = localtime(&timer);
strftime(s1, 40, "%d.%m.%Y-%H:%M:%S.avi", u);
sFileOutput=&s1;
//char *sFileOutput = "01.01.2017-23.23.23.avi";
ofmt = av_guess_format( NULL, sFileOutput, NULL );
ofcx = avformat_alloc_context();
ofcx->oformat = ofmt;
int ret2=avio_open( &ofcx->pb, sFileOutput, AVIO_FLAG_WRITE);
if(ret2<0){
fprintf(stderr, "\nError occurred when opening output file: %s\n",av_err2str(ret2));
}
}
When i run it, i have error in console:
Error occurred when opening output file: Protocol not found
but if i uncomment string
char *sFileOutput = "01.01.2017-23.23.23.avi";
evirything is ok, progam is working without errors. please tell me what is wrong.
thank you for your answer, it helps me too.
But real problem was that generated name contained ':'. i changed string to
strftime(s1, 40, "%d.%m.%Y-%H.%M.%S.avi", u);
and it works well.
When you do:
sFileOutput=&s1
&s1 creates a pointer of type char (*)[40] and not a pointer to the first element of the array like you expect. You are passing around a pointer to the entire array which gets converted to an incompatible type. Check the compilation warnings/errors.
The solution is to use either the implicit conversion:
sFileOutput=s1
or:
sFileOutput=&s1[0]

Accessing a git_odb_writepack field in libgit2 gives error "dereferencing pointer to incomplete type"

I'm using libgit2 and I want to write a pack file to an odb created with git_repository_odb. So I call git_odb_write_pack and initialize a *git_odb_writepack. Then when I attempt to access a field of the writepack struct, I get a compiler error "dereferencing pointer to incomplete type". Here's the code:
#include <stdio.h>
#include <git2.h>
void check_error(int code, char *action) {
if (code) {
printf("Error %d, %s\n", code, action);
exit(1);
}
}
static int my_git_transfer_progress_callback(const git_transfer_progress *stats, void *payload) {
printf("Got transfer callback\n");
return 0;
}
int main(int argc, char **argv) {
int error;
const char *repo_path = "/path/to/repo";
git_repository *repo = NULL;
error = git_repository_open(&repo, repo_path);
check_error(error, "opening repo");
git_odb *odb = NULL;
error = git_repository_odb(&odb, repo);
check_error(error, "initializing odb");
git_odb_writepack *writepack = NULL;
char *payload = "here's my payload";
error = git_odb_write_pack(&writepack, odb, my_git_transfer_progress_callback, payload);
check_error(error, "opening pack writing stream");
printf("Address: %u\n", writepack->backend); // <-- Line generating the error.
return 0;
}
Then I compile and get the error:
$ gcc -lgit2 writepack_error.c && LD_LIBRARY_PATH=/usr/local/lib ./a.out
writepack_error.c: In function 'main':
writepack_error.c:33: error: dereferencing pointer to incomplete type
I'm using libgit2 version 0.21.0. I'm new to C and libgit2 so I may be doing something silly. My understanding is this "dereferencing" error means I failed to define or include a struct or typedef. However I thought libgit2 only requires one include, #include <git2.h>.
Normal usage is covered by git2.h. Some functionality is kept under the sys/ directory to indicate that it's considered more advanced usage.
This in particular looks like it might be a bug since git2.h does not include git2/odb_backend.h. For now you can simply include it manually.

getting format not a string literal even if I add %s

I have looked around for answer on various forums, tried various things and still getting this error:
warning: format not a string literal and no format arguments [-Wformat-security]
The compiler point to the line in the function that has the error, here's how it looks:
int print_notes(int fd, int uid, char *searchstring) {
int note_length;
char byte=0, note_buffer[100];
note_length = find_user_note(fd, uid);
if(note_length == -1) // if end of file reached
return 0; // return 0
read(fd, note_buffer, note_length); // read note data
note_buffer[note_length] = 0; // terminate the string
if(search_note(note_buffer, searchstring)) // if searchstring found
scanf("%s", note_buffer) // Got this line from an answer in the forums
printf(note_buffer); // compiler points here
return 1;
}
If you want the full code i can post it here, but its kind of long :/ don't know if that will be ok.
Its giving warning for :
printf(note_buffer);
As you are getting string being formed at runtime and trying to print it.
Use :
printf("%s",note_buffer);

sqlite3 library undefined reference error

I am creating a word list searcher in C for my program using sqlite3 but I've got these errors .
I tried whatever I knew but it didn't fixed. I guess the problem is in my join function but I am not sure.
code :
bool *gb_wordlist_add_to_list (gbwordlist *word_list,char *str)
{
int sql_error;
char *error_massage;
if (gb_wordlist_in_list (word_list,str))
{
sql_error = sqlite3_execute(word_list->database, gb_wordlist_join(ADD_TO_TABLE_COMMAND"\'",str,"\';"),
NULL ,NULL, &error_massage);
if( sql_error!=SQLITE_OK )
{
fprintf(stderr, "SQL error: %s\n", error_massage);
sqlite3_free(error_massage);
return 0;
}
}
else
return 0;
}
char *gb_wordlist_join (char *s1,char *s2,char *s3){
char *s;
s = malloc(strlen(s1) + strlen(s2) + strlen(s3) + 1);
if(s)
{
strcpy(s,s1);
strcat(s,s2);
strcat(s,s3);
}
return s;
}
error:
gb-sql.o: In function `gb_wordlist_remove_from_list':
/home/reza/Project/GB/Search algorithm/Source/gb-search/src/gb-sql.c:104: undefined reference to `sqlite3_execute'
Also my full codes are here. Thanks a lot!
The reason you are getting undefined reference to sqlite3_execute is well there is no such function as part of library. You probably meant to use sqlite3_exec (which use have used in some parts of the code).
Side Notes:
The function gb_wordlist_callback is returning int but has been declared to return int*. You should change the return type to int to match the expected parameters to be passed to sqlite3_exec(after modifying from sqlite3_execute)
The 4th parameter passed to sqlite3_exec (after modifying from sqlite3_execute) is expected to be void* so existance should be &existance
You have quite a few functions with mismatch between return type declared and the actual return type returned from the function.
Compile your code with -Wall -Wextra compiler options & fix all the warnings. It is good practice.
Hope this helps!

print concat value of newline("\n") result shows as <?> symbol in C

I have a code below
#include <stdio.h>
int main()
{
char *price_c = "200";
char *s_att = "test ";
int satt=strlen(s_att);
int price_len = strlen(price_c);
int send_attach_len = price_len+satt;
size_t length = send_attach_len +2;
char *concat = malloc(sizeof(char) *length);
snprintf(concat, length, "%s%s%s", s_att, price_c, "\n");
printf("value of concat is %s", concat);
}
when I see the value printed, I have only test 200 , but on some other occasion, I have test 200 < ? > where < ? > is a weird symbol, somehow the new line is not recognised.
But it is very strange because not all the time this weird symbol is shown up..
It just came up randomly. I am using ubuntu 10.04
Can anyone help me to solve this new line problem, so that it shows new line, and not weird symbol. Or maybe I can change the approach to concat the above value so that the new line is rendered correctly, and not showing a weird symbol?
The code looks ok, except for the very important fact that you are missing some headers.
strlen is in <string.h>, and malloc is in <stdlib.h>.
Include those, turn on your compiler warnings (-Wall for GCC), change your main signature to:
int main(void) { }
and actually return an int from it (or compile as C99, std=c99 for GCC), and the problems should go away.

Resources