Hi friends...

Can any one give me some tips or logic for dynamically defining a structure in C...

Thanx for ur help.....

Recommended Answers

All 6 Replies

Hi friends...
Can any one give me some tips or logic for dynamically defining a structure in C...

Can you give me an example of dynamically defining structure in C? Structure definition in C looks like (in source code):

struct tag { /* That's a structure definition! */ };

Dynamically means at run-time. No source code at run-time, it was compiled just before ;)...

Not only a structure...You cannot define anything dynamically.
You can just set values to already allocated or defined variables.Even in functions like malloc where you allocate memory dynamically during run time you only allocate memory to predefined or declared variables. Creating a completely new entity is not at all possible.

It's always possible to "fake" something, but the OP needs to be more specific about the problem.

commented: Truth be told. +13

Can you give me an example of dynamically defining structure in C? Structure definition in C looks like (in source code):

struct tag { /* That's a structure definition! */ };

Dynamically means at run-time. No source code at run-time, it was compiled just before ;)...

Basically i am trying to develop a database using C...
where i will be taking field name and data type from the user at runtime....

enum dType { isInt, isFloat };
struct foo {
  struct foo *nextField;
  char       fieldName[100];
  enum dType fieldType;
  union {
    int vInt;
    float vFloat;
  }v;
};

A linked list of fields.
Expand dType and the union as appropriate.

All datafields have common attributes: name, type (domain) and max size (may be some others, it depends on the database architecture). You can map these base field attributes onto the common structure (struct in C) - a field descriptor.

However fields values from different domains have different sets of operators. So you can't perform such operations with the same code (for example, it's impossible to compare int with int and string with string by the same code). There are lots of methods to implement such "variant" operators. The simplest and ineffective: use switch(type).

On the other hand it's possible to make the common code of field values internal memory management. An abstract field value is a pair of a pointer (char*, for example) with a proper memory alignment and an actual field length. It's yet another candidate member of the common field descriptor structure. May be you need a pointer to this "value descriptor" as a member of a field descriptor structure...

Now you need some kind of a list or dynamically (re)allocated array of such descriptors to represent database table rows (tuples). But it's another story: it depends on your database architecture (flat files, relational, object-oriented and so on).

I hope you see that no need in "dynamical definitions of structures" as C struct type constructs. Your database engine must build dynamic data structures of database objects from a proper set of "statically" defined C structures, arrays, pointers etc...

Start from the simplest database structure then expand it step by step...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.