473,563 Members | 2,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic structure

2 New Member
I need a structure to create dynamically at run time through the user input. The input will be given at run time through the key board.
Is it possible to create a structure at run time through c language?
For reference follow the link given below.
https://stackoverflow.c om/questions/22084223/creating-a-certain-struct-at-run-time-in-c
Jul 16 '17 #1
1 1146
weaknessforcats
9,208 Recognized Expert Moderator Expert
A structure is part of the code. There is no code at run time. Therefore, you cannot create a struct at runtime.

Instead what you do is define a struct in the code and then use malloc() at runtime to create an instance of the struct dynamically.

Read up on this since you are responsible for deleting anything you create at runtime.

Expand|Select|Wrap|Line Numbers
  1. struct Data
  2. {
  3.    ....your members go here...
  4.  
  5. };
  6.  
  7. In main():
  8.  
  9. Data* ptr = 0;
  10.  
  11. ptr = malloc(sizeof Data);
  12.  
Now use ptr to access the struct members.
Jul 16 '17 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
1860
by: Neuruss | last post by:
I've been reading an article published in E-Week entitled "Microsoft Lures Open-Source Programmer", which contains a definition for dynamic languages as follows: "Dynamic programming languages enable programs to change their structure as they run." I wonder if this definition is correct. Can we define dynamic languages this way?
2
2190
by: Sylvia | last post by:
Hi, I'm trying to render a XML structure to HTML using XSLT. My XML describe the header of a table with a complex and not linear structure. The first row of the header table always contains the ServiceName (attribute name), under I have to show all the other fields with correct structure. Can you help me to write XSLT document? My XML...
11
2393
by: Mannequin* | last post by:
Hi all, I'm working on a quick program to bring the Bible into memory from a text file. Anyway, I have three questions to ask. First, is my implementation of malloc () correct in the program to follow? Second, have I correctly passed the structure's pointer to the functions in this program?
8
3669
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was static; if the input file contained more than entries, tough. This time I want to do it right - use a dynamic array that increases in size with...
2
2460
by: xinsir | last post by:
dynamic array as a byref parameter by used in function and have a Marshal error ,what is the matter?thanks source like as this . ------------------------------------------------------ Declare Function finit Lib "DllCap.dll" _ (ByRef rdAcnt As ACNTINF2) As Integer <StructLayout(LayoutKind.Sequential)> Structure ACNTINF...
11
2333
by: aaragon | last post by:
Hi everyone. I'm trying to write a class with policy based design (Alexandrescu's Modern C++ Design). I'm not a programmer but an engineer so this is kind of hard for me. Through the use of policies, I want to customize the structure of a class. The idea is to investigate the use of several data structures. One option would be the use of...
0
1432
by: karthkeyanmani | last post by:
Hi all, I want to create a dynamic data structure (ie) getting the data from database, using this data's (not fixed) create a dynamic structure, and access these data's somewhere. Thank you.
4
5055
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or deleted, however it must be initialized by a function during run-time to contain so many users which each contain so many directories of which each...
3
2982
by: ranjeetasharma81 | last post by:
Hi all, I have a big C-cod, in which there are lots of dynamic memory allocation used. I want to replace dynamic memroy allocation by static arrays. The following are the problems that i am facing: 1- From structure and dynamic memory allocation point of view, the code is very complicated. The code has various “nested structures”...
1
2311
by: Gurur | last post by:
Hi all, I have a doubt. If I have 2 structures and one is parent of other , ie the child structure is present in the parent one . And if the child structure is declared as dynamic array in the parent , will it be possible to pass the parent structure thru network using sockets onto other application running on different system provided the...
0
7659
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7580
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7882
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7945
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5481
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5208
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3634
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2079
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.