Click here to Skip to main content
15,064,915 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
suppose my structure is

struct stu{
int a,b,c;
}

and i want to add another variable d at run time is it possible??


or else can we make structure at run time ??
Posted
Comments
   
Why would you need such a weird thing (which you cannot do, of course)? It looks like you don't know how to use some legitimate technique for some purposes, but you failed to share your goals, which makes the question making no sense at all.
--SA
pasztorpisti 7-Aug-12 18:06pm
   
Give us the problem you want to solve with this and we show a good solution for you instead of this bizarre idea. :-)

No. As far as I know this only works with dynamic languages.
   
I don't think this is possible in any normal way, structures don't really exist once the code is compiled. You're probably better off using a dynamic array instead of a structure, or adding a dynamic array to the structure for adding values to it. If they won't all be the same type, you can use void pointers.

e.g.
C++
struct stu 
{
    int a,b,c;
    void** extensions; /* initialize this and add values here as needed */
};

/* ... */
stu s;
s.extensions = new void*[5];
s.extensions[0] = (void*)(new int(4));



But I think if your design requires you to do this, you should probably rethink your design.
   
v2
Comments
Anand.genius 7-Aug-12 13:48pm
   
i cant understand this ,please give me some complete code using some example...
i have tried but give error.....
lewax00 7-Aug-12 14:02pm
   
I forgot a semicolon at the end of the struct, but it worked fine otherwise for me. "i have tried but give error" isn't helpful. What error?

But honestly, I wouldn't rely on this code, it's messy and error prone because you lose type checking by doing it this way. Why do you feel you need to add variables to the struct? There's probably a better way than this.
   
You know, you are technically right, but I doubt it can practically help anyone. The problem is completely different: it looks like OP does not know how to program, but failed to share what's the goal. Until we know it, all answers make no sense. When we know it, I'm sure the answer would be much more robust code. (I did not vote.)
--SA
With dynamic memory allocation, you may do whatever you want.
It is possible, for instance, this way:
C
struct stu
{
  int a,b,c;
};

int main()
{
  struct stu * pstu;
  pstu = malloc(sizeof(*pstu) + sizeof(int));

  pstu->a = 10; // init a
  pstu->b = 20; // init b
  pstu->c = 40; // init c
  
  * (int*)((unsigned char *)pstu + sizeof(*pstu)) = 40; // init 'd'


  free(pstu); // never forget to release memory
  return 0;
}


As you may easily recognize, it is not the cleanest way to do things.
(Neverthless Win32 API often uses a similar approach).
   
Comments
lewax00 7-Aug-12 16:14pm
   
The same line of thought is how I came up with mine, but either way it's not pretty. But then again, the whole question is rather hackish anyways (for C at least, a dynamic language like Python on the other hand is a different story).

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