struct students
{
char name[256];
int Roll_number;
};
struct colleges
{
char name[256];
Student students[100];
};
How to access student[0].name, I have tried to access using -> and . operator is is not accessable
Structure within Structure : Nested Structure
Structure written inside another structure is called as nesting of two structures.
Nested Structures are allowed in C Programming Language.
We can write one Structure inside another structure as member of another structure.
as member of another structure
#include <stdio.h>
struct students {
char name[256];
int Roll_number;
};
struct colleges {
char name[256];
struct students students[100];
};
int main(void)
{
struct colleges c = { };
printf("%s\n", c.students[0].name);
return 0;
}
Related
I am trying to store the parameter pack of lvalue references of a variadic template for later use.
I have the following working for now.
template <typename... Ts>
class Foo {
private:
std::tuple<Ts...> m_args;
public:
template<typename... Args>
Foo(Args&&... args) : m_args(std::make_tuple(std::forward<Args>(args)...))
{
}
};
int main() {
int x = 10;
int y = 20;
Foo<int, int> foo(x, y);
}
However, I would like to store the parameter pack as a reference so that I can access the same object later.
I am not sure how I can do that. Any help would be appreciated.
The best I can imagine, is the use of std::forward_as_tuple.
Unfortunately I don't see a way to use it with perfect forwarding: if you want register values in a tuple inside a class, you have to decide the type of the tuple one time for all.
The best I can imagine is a tuple of const references; something as follows
template <typename ... Ts>
class Foo
{
private:
std::tuple<Ts const & ...> m_args;
public:
Foo (Ts const & ... as) : m_args{std::forward_as_tuple(as...)}
{ }
};
I hope isn't necessary remember you how dangling references can be dangerous for a solution based on a tuple of references.
Is there a way to write a copy-constructor for a class (say, Copyable, that holds a std::unique_ptr to a Base class (but really is storing Derived objects.
A quick test shows the expected slicing occurs, because Copyable doesn't know the real type it's holding. So I suppose a clone method is needed, but I'm wondering if there is a way to let the compiler handle this in some better way?
The slicing code:
#include <algorithm>
#include <iostream>
#include <memory>
struct Base
{
Base(int i = 0) : i(i) {}
virtual ~Base() = default;
int i;
virtual int f() { return i; }
};
struct Derived : Base
{
Derived() = default;
virtual int f() override { return 42; }
};
struct Copyable
{
Copyable(std::unique_ptr<Base>&& base) : data(std::move(base)) {}
Copyable(const Copyable& other)
{
data = std::make_unique<Base>(*other.data);
}
std::unique_ptr<Base> data;
};
int main()
{
Copyable c(std::make_unique<Derived>());
Copyable c_copy = c;
std::cout << c_copy.data->f() << '\n';
}
The clone code:
#include <algorithm>
#include <iostream>
#include <memory>
struct Base
{
Base(int i = 0) : i(i) {}
virtual ~Base() = default;
int i;
virtual int f() { return i; }
virtual Base* clone() { return new Base(i); }
};
struct Derived : Base
{
Derived() = default;
virtual int f() override { return 42; }
virtual Derived* clone() override { return new Derived(); }
};
struct Copyable
{
Copyable(std::unique_ptr<Base>&& base) : data(std::move(base)) {}
Copyable(const Copyable& other)
{
data.reset(other.data->clone());
}
std::unique_ptr<Base> data;
};
int main()
{
Copyable c(std::make_unique<Derived>());
Copyable c_copy = c;
std::cout << c_copy.data->f() << '\n';
}
Obviously the clone code works. Thing is, there's some things in it I'd like to avoid:
raw new.
a random function that needs to be part of the interface.
This function returns a raw pointer.
Every user of this class that wants to be copyable needs to call this function.
So, is there a "clean" alternative?
Note I want to use smart pointers for all the obvious reasons, I just need a deep copying std::unique_ptr. Something like std::copyable_unique_ptr, combining optional move semantics with a deep copying copy constructor. Is this the cleanest way? Or does that only add the the confusion?
You can certainly create a clone_ptr-class for any object you know statically how to clone.
It would hold a pointer to the object, and a pointer to a function for cloning said object, probably from converting a stateless lambda.
Can I have groups inside a struct?
pseudo-code:
typedef struct {
input_group {
logic a;
}
output_group {
logic b;
}
} my_signals_list
Short answer: no.
If you want to have signals grouped like this, why not create a struct for the input group and a struct for your output group?
typedef struct {
logic a;
} input_group_s;
typedef struct {
logic b;
} output_group_s;
typedef struct {
input_group_s input_group;
output_group_s output_group;
} my_signals_list;
As Greg points out in the comments, you can also have nested struct definitions inside the main struct:
typedef struct {
struct { logic a; } input_group;
struct { logic b; } output_group;
} my_signals_list;
If you want to specify signals for a module in a nice encapsulated fashion, I would suggest using an interface, though.
I have a Visual Studio 2013 C++11 project where I'm trying to define an iterator. I want that iterator to dereference to an object, but internally it actually iterates over some internal data the object requires for construction.
class my_obj
{
public:
my_obj(some_internal_initialization_value_ v);
std::wstring friendly_name() const;
// ...
};
class my_iterator
: public boost::iterator_facade<
my_iterator,
my_obj,
boost::forward_traversal_tag>
{
// ...
private:
my_obj& dereference() const
{
// warning C4172: returning address of local variable or temporary
return my_obj(some_internal_initialization_value_);
}
};
int main( int argc, char* argv[])
{
my_container c;
for (auto o = c.begin(); o != c.end(); ++o)
printf( "%s\n", o->friendly_name().c_str() );
}
These internal values are unimportant implementation details to the user and I'd prefer not to expose them. How can I write the iterator that does this correctly? The alternative is that I would have to do something like this:
my_container c;
for (auto i = c.begin(); i != c.end(); ++i)
{
my_obj o(*i);
printf( "%s\n", o.friendly_name().c_str() );
}
From the boost page on iterator_facade, the template arguments are: derived iterator, value_type, category, reference type, difference_type. Ergo, merely tell it that references are not references
class my_iterator
: public boost::iterator_facade<
my_iterator,
my_obj,
boost::forward_traversal_tag,
my_obj> //dereference returns "my_obj" not "my_obj&"
See it working here: http://coliru.stacked-crooked.com/a/4b09ddc37068368b
I want to add customized atag variable in U-Boot and Linux kernel.
How can i achieve this?
Is there any procedure to add an ATAG variable in U-Boot and Linux?
The latest Linux kernel is attempting to obsolete ATAGS with device trees. However, the setup.h file defines the different ATAG values and structures. To parse these, you need to add them with something like,
static int __init parse_tag_custom(const struct tag *tag)
{
if (tag->hdr.size > CUSTOM_SIZE) {
/* Use, storing or acting on passed values */
tag->u.custom;
}
return 0;
}
__tagtable(ATAG_CUSTOM, parse_tag_custom);
as found in atags_parse.c. Of course, you need to add these to the values in setup.h.
u-boot is probably less defined as for the most part, it passes arguments via the kernel command line as this is not ARM specific. A command argument or device trees is probably the preferred method. If you gave an example of what type of configuration you need, someone could probably give better guidance.
U-Boot changes required :
A. Make sure the CONFIG_CMDLINE_TAG/CONFIG_SETUP_MEMORY_TAGS/CONFIG_INITRD_TAG are defined in you project definition header file ( u-boot/include/configs/am335x_evm.h ), and we can add our own tag here, eg. CONFIG_CUSTOM_TAG.
B. Add the structure definition you wanna append w/ ATAG in u-boot/include/asm-arm/setup.h, eg.
#define ATAG_CUSTOM 0x5441000a
struct tag_custom{
unsigned char mac_addr[6];
};
C. Add the struct at the tail of "u"...
struct tag {
struct tag_header hdr;
union {
struct tag_core core;
struct tag_mem32 mem;
struct tag_videotext videotext;
struct tag_ramdisk ramdisk;
struct tag_initrd initrd;
struct tag_serialnr serialnr;
struct tag_revision revision;
struct tag_videolfb videolfb;
struct tag_cmdline cmdline;
/*
* Acorn specific
*/
struct tag_acorn acorn;
/*
* DC21285 specific
*/
struct tag_memclk memclk;
/****** INFOTECH Custom TAG ********/
struct tag_custom custom;
} u;
};
D. Add implementation code in lib_arm/bootm.c:
static void setup_custom_tag(bd_t *bd);
static void setup_custom_tag(bd_t *bd) {
params->hdr.tag = ATAG_CUSTOM;
params->hdr.size = tag_size (tag_macaddr);
params->u.custom.cmd =0;
params = tag_next (params);
}
E. Add "#ifdef CONFIG_CUSTOM_TAG / #endif" at every place you change the code.
F. Done of U-Boot modification.
Linux Changes required:
A. Add parse tag code in linux/arch/arm/kernel/setup.c:
int cmd;
static int __init parse_tag_custom(const struct tag *tag){
printk("u.custom.cmd=%d\n",tag->u.custom.cmd);
return 0;
}
__tagtable(ATAG_MACADDR, parse_tag_custom);
B. Add the structure declaration as U-Boot did in linux/include/asm-arm/setup.h:
#define ATAG_MACADDR 0x5441000a
struct tag_custom {
int cmd;
};
C. Add the struct at the tail of "u"...
struct tag {
struct tag_header hdr;
union {
struct tag_core core;
struct tag_mem32 mem;
struct tag_videotext videotext;
struct tag_ramdisk ramdisk;
struct tag_initrd initrd;
struct tag_serialnr serialnr;
struct tag_revision revision;
struct tag_videolfb videolfb;
struct tag_cmdline cmdline;
/*
* Acorn specific
*/
struct tag_acorn acorn;
/*
* DC21285 specific
*/
struct tag_memclk memclk;
/* Add Infotech custom tag */
struct tag_custom custom;
} u;
};
D. Done w/ Kernel parts.
Follow this procedure ,
To achieve this goal, there're 2 parts need to be modified. One is the U-Boot, and the other one is the Linux kernel.
1. U-Boot changes required :
A. Make sure the CONFIG_CMDLINE_TAG/CONFIG_SETUP_MEMORY_TAGS/CONFIG_INITRD_TAG are defined in you project definition header file ( u-boot/include/configs/am335x_evm.h ), and we can add our own tag here, eg. CONFIG_CUSTOM_TAG.
B. Add the structure definition you wanna append w/ ATAG in u-boot/include/asm-arm/setup.h, eg.
#define ATAG_CUSTOM 0x5441000a
struct tag_custom{
unsigned char mac_addr[6];
};
C. Add the struct at the tail of "u"...
struct tag {
struct tag_header hdr;
union {
struct tag_core core;
struct tag_mem32 mem;
struct tag_videotext videotext;
struct tag_ramdisk ramdisk;
struct tag_initrd initrd;
struct tag_serialnr serialnr;
struct tag_revision revision;
struct tag_videolfb videolfb;
struct tag_cmdline cmdline;
/*
* Acorn specific
*/
struct tag_acorn acorn;
/*
* DC21285 specific
*/
struct tag_memclk memclk;
/****** INFOTECH Custom TAG ********/
struct tag_custom custom;
} u;
};
D. Add implementation code in lib_arm/bootm.c:
static void setup_custom_tag(bd_t *bd);
static void setup_custom_tag(bd_t *bd) {
params->hdr.tag = ATAG_CUSTOM;
params->hdr.size = tag_size (tag_macaddr);
params->u.custom.cmd =0;
params = tag_next (params);
}
E. Add "#ifdef CONFIG_CUSTOM_TAG / #endif" at every place you change the code.
F. Done of U-Boot modification.
2. Linux Changes required:
A. Add parse tag code in linux/arch/arm/kernel/setup.c:
int cmd;
static int __init parse_tag_custom(const struct tag *tag){
printk("u.custom.cmd=%d\n",tag->u.custom.cmd);
return 0;
}
__tagtable(ATAG_MACADDR, parse_tag_custom);
B. Add the structure declaration as U-Boot did in linux/include/asm-arm/setup.h:
#define ATAG_MACADDR 0x5441000a
struct tag_custom {
int cmd;
};
C. Add the struct at the tail of "u"...
struct tag {
struct tag_header hdr;
union {
struct tag_core core;
struct tag_mem32 mem;
struct tag_videotext videotext;
struct tag_ramdisk ramdisk;
struct tag_initrd initrd;
struct tag_serialnr serialnr;
struct tag_revision revision;
struct tag_videolfb videolfb;
struct tag_cmdline cmdline;
/*
* Acorn specific
*/
struct tag_acorn acorn;
/*
* DC21285 specific
*/
struct tag_memclk memclk;
/* Add Infotech custom tag */
struct tag_custom custom;
} u;
};
D. Done w/ Kernel parts.