a vector of void pointers which point to array of characters

Pages: 12
Not sure why this has dragged on for this long. From what I see in your code and the compiler error, you seem to be assigning a char to a char pointer (i.e. char array). So the assignment was expecting an array of char not a single char, this should fix the problem:

1
2
3
char *sentence[] = {"A", "n", "g", "e", "l"};
    for (int i=0; i < 5; i++)
    	out[i] = sentence[i];
@Smac89
Your proposed code works, it suggests a minor improvement to the very initial code, which is as follows:
1
2
3
4
5
6
char* *out = (char**) output_items[0];
out[0] = "A";
out[1] = "n";
out[2] = "g";
out[3] = "e";
out[4] = "l";


The requirement is that I need to make the content of sentence obtained from user input when the code execution begins.
In this case the sentence need be stored as a class variable, and its value is obtained from the constructor's argument.

That's why I try to put it in this way, which fails to compile: char * sentence = "Angel";
Last edited on
If you insist on doing it the hard way, you will have to deal with the intricacies of dynamic memory management of character arrays.
Dear all above,

I think it is ok if we don't have a better idea.
So the final version of the GNU Radio block coding is as follows (though not ideal):

Filename: gr-howto/include/howto/byte_source.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* -*- c++ -*- */
#ifndef INCLUDED_HOWTO_BYTE_SOURCE_H
#define INCLUDED_HOWTO_BYTE_SOURCE_H

#include <howto/api.h>
#include <gr_block.h>

namespace gr {
  namespace howto {

    class HOWTO_API byte_source : virtual public gr_block
    {
       public:
          typedef boost::shared_ptr<byte_source> sptr;
          static sptr make();
    };

  } // namespace howto
} // namespace gr

#endif /* INCLUDED_HOWTO_BYTE_SOURCE_H */ 



Filename: gr-howto/lib/byte_source_impl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/* -*- c++ -*- */
#ifndef INCLUDED_HOWTO_BYTE_SOURCE_IMPL_H
#define INCLUDED_HOWTO_BYTE_SOURCE_IMPL_H

#include <howto/byte_source.h>

namespace gr {
  namespace howto {

    class byte_source_impl : public byte_source
    {
       private:
          unsigned int counter;
        

       public:
          byte_source_impl();
          ~byte_source_impl();

          void forecast (int noutput_items, gr_vector_int &ninput_items_required);

          int general_work(int noutput_items,
                   gr_vector_int &ninput_items,
                   gr_vector_const_void_star &input_items,
                   gr_vector_void_star &output_items);
    };

  } // namespace howto
} // namespace gr

#endif /* INCLUDED_HOWTO_BYTE_SOURCE_IMPL_H */ 



Filename: gr-howto/lib/byte_source_impl.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* -*- c++ -*- */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gr_io_signature.h>
#include "byte_source_impl.h"

namespace gr {
  namespace howto {

    byte_source::sptr byte_source::make()
    { return gnuradio::get_initial_sptr (new byte_source_impl()); }

    // private constructor
    byte_source_impl::byte_source_impl() : gr_block(
               "byte_source",
                gr_make_io_signature( 0, 0, 0 ),
                gr_make_io_signature( 1, 1, sizeof(char*) ) ),  // What to use, char* ??
                counter(0)
    { }

    // virtual destructor
    byte_source_impl::~byte_source_impl()
    { }

    void byte_source_impl::forecast (int noutput_items, gr_vector_int &ninput_items_required)
    {
        ninput_items_required[0] = 0;  // for source_block it is zero, but not noutput_items;
    }

    int byte_source_impl::general_work (
                       int                       noutput_items,
                       gr_vector_int             &ninput_items,
                       gr_vector_const_void_star &input_items,
                       gr_vector_void_star       &output_items)
    {
        char* *out       = (char**) output_items[0];
        char* sentence[] = {"A", "n", "g", "e", "l" };

        for (int i=0; i < noutput_items; i++)
        {
            out[i] = sentence[counter];

            counter++;
            if ( counter == 5 )
              counter = 0;
        }

        consume_each (0);

        // Tell runtime system how many output items we produced.
        return noutput_items;
    }

  } /* namespace howto */
} /* namespace gr */



Thank you very much.

Note:
For documentation, refer http://gnuradio.org/doc/doxygen-3.6.5
Last edited on
Topic archived. No new replies allowed.
Pages: 12