Click here to Skip to main content
15,900,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The following code creates a connection to Redis using the hiredis synchronous API: But I get null response with redisCommand in gcc version (10.2.1 20210110).


What I have tried:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <hiredis/hiredis.h>

int main (int argc, char **argv) {
    redisReply *reply;
    redisContext *c;

    c = redisConnect("127.0.0.1", 6379);
      if (c == NULL || c->err) {
        if (c) {
            printf("Connection error: %s\n", c->errstr);
            redisFree(c);
        } else {
            printf("Connection error: can't allocate redis context\n");
        }
      }
    /* PINGs */
    printf("%ld\n", __STDC_VERSION__);
    reply = redisCommand(c,"PING %s", "HelloWorld");
    printf("RESPONSE: %s\n", reply->str);
    printf("error: %s\n", c->errstr);
    freeReplyObject(reply);

    redisFree(c);
    return 0;
}

Output: [gcc version 10.2.1 20210110 (Debian 10.2.1-6)]

201710 RESPONSE: (null) error:

Expected output: [gcc version 6.3.0 20170516 (Debian 6.3.0-18+deb9u1)]

201112 RESPONSE: HelloWorld error:
Posted
Updated 23-Feb-23 4:14am

1 solution

I could not find anything on the subject, maybe check documentation on your API.

I eventually ran this at GPT (yeah, I know). I have no idea if this will help you and I also make no claim to the code generated...

The error is likely occurring because the redisCommand() function is being called incorrectly. The redisCommand() function takes a format string as its first argument, which should include a format specifier for each additional argument passed to the function. In this case, the format string is "PING %s", which includes a %s format specifier, indicating that an additional string argument will be passed to the function. However, only one argument is passed to the function: the format string itself.

To fix the error, you should pass the additional argument to the redisCommand() function as a separate parameter.


#include <hiredis/hiredis.h>

int main(int argc, char **argv) {
    redisReply *reply;
    redisContext *c;

    c = redisConnect("127.0.0.1", 6379);
    if (c == NULL || c->err) {
        if (c) {
            printf("Connection error: %s\n", c->errstr);
            redisFree(c);
        } else {
            printf("Connection error: can't allocate redis context\n");
        }
        return 1;
    }

    /* PINGs */
    reply = redisCommand(c, "PING %s", "HelloWorld");
    printf("RESPONSE: %s\n", reply->str);
    freeReplyObject(reply);

    redisFree(c);
    return 0;
}
 
Share this answer
 
Comments
Tanjila Diwan 27-Feb-23 1:48am    
Thanks for your response.
I have tried the same code which you provided but still I get the same response.
Can you write where to pass the additional argument to the redisCommand() function as a separate parameter?

Output:
RESPONSE: (null)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900