Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7563817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:45:41+00:00 2026-05-30T13:45:41+00:00

Consider following example: #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <hiredis/hiredis.h> int main(int argc,

  • 0

Consider following example:

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

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

  redis = redisConnect("127.0.0.1", 6379);
  if(redis->err) {
    fprintf(stderr, "Connection error: %s\n", redis->errstr);
    exit(EXIT_FAILURE);
  }

  reply = redisCommand(redis, "SET %s %s", "foo", "bar");
  printf("SET %s %s: %s\n", "foo", "bar", reply->str);
  freeReplyObject(reply);

  reply = redisCommand(redis, "SET %s %s", "name", "value");
  printf("SET %s %s: %s\n", "name", "value", reply->str);
  freeReplyObject(reply);

  reply = redisCommand(redis, "MGET %s %s", "foo", "name");
  printf("MGET %s %s: %s\n", "foo", "name", reply->str);
  freeReplyObject(reply);

  exit(EXIT_SUCCESS);
}

The output is:

PING: PONG
SET foo bar: OK
GET foo: bar
SET name value: OK
MGET foo name: (null)

It’s about return from MGET. Can I get multi keys using hiredis?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:45:43+00:00Added an answer on May 30, 2026 at 1:45 pm

    A redisReply is a typed object (see the type field), and a multi-bulk reply has a specific type (REDIS_REPLY_ARRAY). The str field is not relevant in that case.

    From the hiredis documentation:

    The number of elements in the multi bulk reply is stored in reply->elements.
    Every element in the multi bulk reply is a redisReply object as well
    and can be accessed via reply->element[..index..].
    Redis may reply with nested arrays but this is fully supported.
    

    So your code should be changed as follows:

    reply = redisCommand(redis, "MGET %s %s", "foo", "name" );
    if ( reply->type == REDIS_REPLY_ERROR )
      printf( "Error: %s\n", reply->str );
    else if ( reply->type != REDIS_REPLY_ARRAY )
      printf( "Unexpected type: %d\n", reply->type );
    else 
    {
      int i;
      for ( i=0; i<reply->elements; ++i )
        printf( "Result: %s\n", reply->element[i]->str );
    }
    freeReplyObject(reply);
    

    With this change, the output is now:

    SET foo bar: OK
    SET name value: OK
    Result: bar
    Result: value
    

    Note: there is no need to free each individual element since freeReplyObject deletes the whole tree.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following example from APUE book: #include <errno.h> #include <sys/socket.h> int initserver(int type,
Let us consider the following factorial example : #include <iostream.h> int factorial(int); void main(void)
Consider the following example: #include <iostream> #include <sstream> #include <vector> #include <wchar.h> #include <stdlib.h>
Consider following example. #include <iostream> #include <algorithm> #include <vector> #include <boost/bind.hpp> void func(int e,
Consider following example. #include <iostream> #include <boost/optional.hpp> template < typename A > int boo(
Consider the following example program: next :: Int -> Int next i | 0
Consider the following example: int size = 10, *kk = new int[size]; for (int
Consider following example: #include <iostream> #include <functional> #include <algorithm> #include <vector> #include <boost/bind.hpp> const
Consider this demo program: #include <stdio.h> class Base { public: virtual int f(int) =0;
Consider the following minimal example: #include <iostream> using namespace std; class myostream : public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.