473,782 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

general array in C

Cyn
Hi,
I want to create a general array structure which can hold all types.
Something like this:

struct ARRAY
{
void **array;
size_t size;
};

Work nice to store pointers to structures in it but what is a good way to be
able to both store pointers to structures and primitive types in it?
Oct 24 '06 #1
20 2326
Cyn wrote:
Hi,
I want to create a general array structure which can hold all types.
Something like this:

struct ARRAY
{
void **array;
size_t size;
};

Work nice to store pointers to structures in it but what is a good way to be
able to both store pointers to structures and primitive types in it?

typedef union {
void *Vptr;
int Int;
short Short;
char Char;
long Long;
long long LLong;
double Double;
long double LDouble;
float Float;
} CELL;

struct ARRAY {
CELL *array;
size_t size;
};

struct ARRAY *newArray(size_ t siz)
{
// Allocates and returns an ARRAY
// ...
}

int main(void)
{
ARRAY *a;

a = newArray(1024);
a.array[23].Float = 2.13f;
a.array[24].Vptr = malloc(267);
a.array[25].LLong = 533777899LL;
a.array[26].Char = 'F';
a.array[27].Vptr = "This is an example string";
}

I would use a type field to avoid mistakes, storing which
type is stored in tha array by changing:
struct ARRAY {
CELL *array;
char *CellTypes;
size_t size;
};

#define VPTR 1
#define INT 2
#define SHORT 3
.... etc

and you store the type of each cell in the adjacent array of characters.
jacob
Oct 24 '06 #2

Cyn wrote:
Hi,
I want to create a general array structure which can hold all types.
Something like this:

struct ARRAY
{
void **array;
size_t size;
};

Work nice to store pointers to structures in it but what is a good way to be
able to both store pointers to structures and primitive types in it?
You're trying to re-invent the C++ object. If you want to do much of
this, it's best to let the C++ compiler and run-time library worry
about all this.

Some other fields you may want to include: the size of each element, a
code for the type of element, the number of dimensions, maybe a link to
the next object (for garbage collection)

Oct 24 '06 #3
Ancient_Hacker wrote:
Cyn wrote:
>>Hi,
I want to create a general array structure which can hold all types.
Something like this:

struct ARRAY
{
void **array;
size_t size;
};

Work nice to store pointers to structures in it but what is a good way to be
able to both store pointers to structures and primitive types in it?


You're trying to re-invent the C++ object. If you want to do much of
this, it's best to let the C++ compiler and run-time library worry
about all this.
????

See my post above. Why do you need a "C++ object" to do that?

can you explain?

Thanks
Some other fields you may want to include: the size of each element, a
code for the type of element, the number of dimensions, maybe a link to
the next object (for garbage collection)
Oct 24 '06 #4

jacob navia wrote:
See my post above. Why do you need a "C++ object" to do that?

You don't. And you can build your own jet plane with enough Electrolux
vacuum cleaners, aluminum cat food cans, "crazy glue", and duct tape.
But many people find it easier to just go and buy one off the shelf.

It's just that C++ is really good at doing all the error-prone busywork
of creating objects, and it lets you define operators, often overused,
on those objects.

You can do similar things in C, but the results are rarely pretty,
concise, and easy to use.

Not to polish the C++ apple too much, in many ways it's a horrible
design, but it does do a few things a whole lot better than most of us
can cobble up out of odd bits.

Oct 24 '06 #5
Ancient_Hacker posted:
>
Cyn wrote:
>Hi,
I want to create a general array structure which can hold all types.
Something like this:

struct ARRAY
{
void **array;
size_t size;
};

Work nice to store pointers to structures in it but what is a good way
to be able to both store pointers to structures and primitive types in
it?

You're trying to re-invent the C++ object.

Hmm... I don't see how it's anything like a class object in C++. If anything,
I'd say he's trying to implement the Variant type which exists in Visual
Basic.

--

Frederick Gotham
Oct 24 '06 #6
Cyn wrote:
>
Hi,
I want to create a general array structure which can hold all types.
Something like this:

struct ARRAY
{
void **array;
size_t size;
};

Work nice to store pointers to structures
in it but what is a good way to be
able to both store pointers to structures and primitive types in it?
With a generic list node:
struct list_node {
struct list_node *next;
void *data;
};
you can have lists of any data format.

--
pete
Oct 24 '06 #7
pete wrote:
Cyn wrote:
>>Hi,
I want to create a general array structure which can hold all types.
Something like this:

struct ARRAY
{
void **array;
size_t size;
};

Work nice to store pointers to structures
in it but what is a good way to be
able to both store pointers to structures and primitive types in it?


With a generic list node:
struct list_node {
struct list_node *next;
void *data;
};
you can have lists of any data format.
Yes but if you want to store a character, you waste sizeof(void *) bytes
in a pointer to... 1 byte.

Isn't a union (as I indicated in my answer) a better solution?
Oct 25 '06 #8
Cyn wrote:
Hi,
I want to create a general array structure which can hold all types.
Something like this:

struct ARRAY
{
void **array;
size_t size;
};

Work nice to store pointers to structures in it but what is a good way to be
able to both store pointers to structures and primitive types in it?
If you need dynamic typing why are you using C? A dynamically typed
language such as Scheme or Python would be a better choice for such
things.

Regards,
Bart.

Oct 25 '06 #9
Ancient_Hacker wrote:
Not to polish the C++ apple too much, in many ways it's a horrible
design, but it does do a few things a whole lot better than most of us
can cobble up out of odd bits.
But this case is not that much better suited for C++. See my other
reply for better alternatives.

Regards,
Bart.

Oct 25 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2655
by: 2mc | last post by:
Generally speaking, if one had a list (from regular Python) and an array (from Numerical Python) that contained the same number of elements, would a While loop or a For loop process them at the same speed? Or, would the array process faster? I'm new to Python, so my question may expose my ignorance. I appreciate anyone's effort to help me understand. Thanks. It is much appreciated.
2
2013
by: Ross Micheals | last post by:
All I have some general .NET questions that I'm looking for some help with. Some of these questions (like the first) are ones that I've seen various conflicting information on, or questions that I'm not sure are specific anomolies that I'm having, or if they are specific known issues I'm facing. 1. In VB.NET, are arrays stored on the stack or the heap? Why must arrays of value types be boxed in order for them to be (effectively) passed by...
1
1806
by: jason | last post by:
Hello everyone, I have some general questions about the DataTable object, and how it works. Moderately new to C#, I have plenty of texts describing the language, but not so much to reference ADO.NET objects (only the MSDN help files). I have written a C# Class Library that is responsible for encapsulating database information. All the objects work just fine for singleton record insert, update, select, and delete operations. But now we...
4
1152
by: Bill | last post by:
In vbscript and vb6 when you created a array via a join that contained no entries when you did a ubound against that array it returned -1, which of course was very helpful for using it as the upper bound in a for loop. In vb.net, in the same situation the ubound will return 0 and you get an array with 1 null entry. I am missing something here or does this seem totally incompatible?
4
3517
by: Saul775 | last post by:
Hello, all: Just a general question that's been bothering me. Suppose I'd like to create a 2D array of integers -- not using the vector template -- but I need to dynamically create the array as I do not know the dimensions. How could I do this using the "new" operator? For instance, suppose I need to create an array of size row x column, but I don't know the values being passed into the function...
3
1541
by: johnmann56 | last post by:
Hello. I have a small C# program which uses a number of large arrays of double. The program runs fine on my desktop PC which has 2 GB of memory, but when run on my laptop with 512 MB the laptop practically freezes up, from running out of memory. I would therefore like to modify my program so that memory space used by the various arrays can be freed up once I am done with each array. I have tried many things before calling GC.Collect(),...
94
4774
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring that I found inside of a string. Any ideas?
37
7180
by: dmoran21 | last post by:
I am a mathematician trying to write a program in C to do some curve fitting. When I get to the point where I attempt to enter data in my arrays, I get a General Protection Exception error message. What is this and how can I fix it. I am writing in Turbo C++. My source code is below. #include <stdio.h> #include <stdlib.h> #include <math.h>
2
3054
by: subhasree | last post by:
Hi, I am a student of mathematics and quite new to programming in C. I am trying to write a code to construct an array which at every step has to compare the value of the current element with all the existing elements in the array. The code is compiling without error. However whenever I try to run it, I get General Protection Exception. Here is my code: #include <stdio.h> #include <conio.h>
0
9639
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10311
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8967
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6733
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
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
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.