473,626 Members | 3,152 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Casting a generic or void pointer to point to a struct...

First, I would thank all of those that took the time to answer my
question about creating an array based on a numeric value stored in a
variable.

I realize after reading the responses and doing some more research,
that what I really need is known in C as a "dynamic array". Basically,
you surpass the array notation and use pointers with memory obtained
with malloc() or calloc(). I think this will do just what I needed.

That has brought up another question though. I'm not sure what syntax I
would use to cast a gneeric or void pointer to a struct that I have
defined. For example, if I have defined the "new_data_t ype" struct
previously in the source code file, would the following code be valid?

/* Create a generic pointer to the first element in the block of
memory obtained with the calloc() function. */
void generic_pointer = calloc(number_o f_elements, size_of_element );

/* If the memory has been sucessfully allocated, cast the generic
pointer to the correct data type. */
if(generic_poin ter != NULL)
(struct new_data_type *)generic_point er
else
/* We've got problems. */

Thanks again for the help everyone.

Scott Huey

Aug 1 '06 #1
11 4689
MQ

re************* ***@gmail.com wrote:
First, I would thank all of those that took the time to answer my
question about creating an array based on a numeric value stored in a
variable.

I realize after reading the responses and doing some more research,
that what I really need is known in C as a "dynamic array". Basically,
you surpass the array notation and use pointers with memory obtained
with malloc() or calloc(). I think this will do just what I needed.

That has brought up another question though. I'm not sure what syntax I
would use to cast a gneeric or void pointer to a struct that I have
defined. For example, if I have defined the "new_data_t ype" struct
previously in the source code file, would the following code be valid?

/* Create a generic pointer to the first element in the block of
memory obtained with the calloc() function. */
void generic_pointer = calloc(number_o f_elements, size_of_element );

/* If the memory has been sucessfully allocated, cast the generic
pointer to the correct data type. */
if(generic_poin ter != NULL)
(struct new_data_type *)generic_point er
else
/* We've got problems. */

Thanks again for the help everyone.

Scott Huey
struct new_data_type * ptr = calloc(number_o f_elements,
size_of_element );
if(ptr == NULL)
{
/*error */
}
else
{
/*do something */
}

MQ

Aug 1 '06 #2
MQ

MQ wrote:
re************* ***@gmail.com wrote:
First, I would thank all of those that took the time to answer my
question about creating an array based on a numeric value stored in a
variable.

I realize after reading the responses and doing some more research,
that what I really need is known in C as a "dynamic array". Basically,
you surpass the array notation and use pointers with memory obtained
with malloc() or calloc(). I think this will do just what I needed.

That has brought up another question though. I'm not sure what syntax I
would use to cast a gneeric or void pointer to a struct that I have
defined. For example, if I have defined the "new_data_t ype" struct
previously in the source code file, would the following code be valid?

/* Create a generic pointer to the first element in the block of
memory obtained with the calloc() function. */
void generic_pointer = calloc(number_o f_elements, size_of_element );

/* If the memory has been sucessfully allocated, cast the generic
pointer to the correct data type. */
if(generic_poin ter != NULL)
(struct new_data_type *)generic_point er
else
/* We've got problems. */

Thanks again for the help everyone.

Scott Huey

struct new_data_type * ptr = calloc(number_o f_elements,
size_of_element );
if(ptr == NULL)
{
/*error */
}
else
{
/*do something */
}

MQ

correction: size_of_element should be replaced by sizeof(struct
new_data_type)

MQ

Aug 1 '06 #3
re************* ***@gmail.com writes:
First, I would thank all of those that took the time to answer my
question about creating an array based on a numeric value stored in a
variable.

I realize after reading the responses and doing some more research,
that what I really need is known in C as a "dynamic array". Basically,
you surpass the array notation and use pointers with memory obtained
with malloc() or calloc(). I think this will do just what I needed.

That has brought up another question though. I'm not sure what syntax I
would use to cast a gneeric or void pointer to a struct that I have
defined. For example, if I have defined the "new_data_t ype" struct
previously in the source code file, would the following code be valid?

/* Create a generic pointer to the first element in the block of
memory obtained with the calloc() function. */
void generic_pointer = calloc(number_o f_elements, size_of_element );

/* If the memory has been sucessfully allocated, cast the generic
pointer to the correct data type. */
if(generic_poin ter != NULL)
(struct new_data_type *)generic_point er
else
/* We've got problems. */
Don't cast the result of malloc() or calloc(). It's unnecessary, and
it can mask certain errors.

BTW, it's "void *generic_pointe r", not "void generic_pointer ".

Also, keep in mind that calloc() isn't necessarily as useful as you
might think it is. It initializes the allocated memory to
all-bits-zero. For pointers or floating-point data, this is not
necessarily meaningful; there's no guarantee that either a null
pointer or a floating-point 0.0 is represented as all-bits-zero.

Usually it's easier to use malloc() (which doesn't initialize the
allocated object), and just make sure that you don't use any portion
of the object that you haven't assigned a value to.

Why do you want a generic pointer rather than a pointer to your
"new_data_t ype" struct? For example:

struct new_data_type {
/* member declarations here */
};

struct new_data_type *ptr;

ptr = malloc(number_o f_elements * sizeof *ptr);

Sometimes you do need to use void* pointers, but quite often it's
better to use a specific pointer type.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 1 '06 #4
MQ wrote:
MQ wrote:
>re************* ***@gmail.com wrote:
>>First, I would thank all of those that took the time to answer my
question about creating an array based on a numeric value stored in a
variable.

I realize after reading the responses and doing some more research,
that what I really need is known in C as a "dynamic array". Basically,
you surpass the array notation and use pointers with memory obtained
with malloc() or calloc(). I think this will do just what I needed.

That has brought up another question though. I'm not sure what syntax I
would use to cast a gneeric or void pointer to a struct that I have
defined. For example, if I have defined the "new_data_t ype" struct
previously in the source code file, would the following code be valid?

/* Create a generic pointer to the first element in the block of
memory obtained with the calloc() function. */
void generic_pointer = calloc(number_o f_elements, size_of_element );

/* If the memory has been sucessfully allocated, cast the generic
pointer to the correct data type. */
if(generic_poin ter != NULL)
(struct new_data_type *)generic_point er
else
/* We've got problems. */

Thanks again for the help everyone.

Scott Huey
struct new_data_type * ptr = calloc(number_o f_elements,
size_of_elemen t);
if(ptr == NULL)
{
/*error */
}
else
{
/*do something */
}

correction: size_of_element should be replaced by sizeof(struct
new_data_type)

In all probability neither is optimal. Firstly, the OP did not say that
it needed clearing to all bits 0 (remember that floating point 0 and
null pointers might not be all bits 0) so why pay the cost of zeroing
the memory? Secondly, there are better ways to use sizeof.

If you really do want calloc:

T *ptr = calloc(number_o f_elements, sizeof *ptr);
Then you only have to specify the type in one place, so maintenance is
easier.

Or, for malloc
T *ptr = malloc(number_o f_elements * sizeof *ptr);

Or, if mallocing some time after declaration:
ptr = malloc(number_o f_elements * sizeof *ptr);

Although the OP should also look up the references to the struct hack
others have posted and, if using a C99 compiler, the C99 sanctioned
alternative.
--
Flash Gordon
Still sigless on this computer.
Aug 1 '06 #5
On Wed, 02 Aug 2006 02:00:42 +0200, Flash Gordon wrote:
<snip>
If you really do want calloc:

T *ptr = calloc(number_o f_elements, sizeof *ptr); Then you only have to
specify the type in one place, so maintenance is easier.

Or, for malloc
T *ptr = malloc(number_o f_elements * sizeof *ptr);
calloc() might have the benefit of overflow detection.

Aug 2 '06 #6
MQ

Flash Gordon wrote:
>
In all probability neither is optimal. Firstly, the OP did not say that
it needed clearing to all bits 0 (remember that floating point 0 and
null pointers might not be all bits 0) so why pay the cost of zeroing
the memory?
I don't understand what you are talking about

MQ

Aug 2 '06 #7
MQ

Flash Gordon wrote:
>
In all probability neither is optimal. Firstly, the OP did not say that
it needed clearing to all bits 0 (remember that floating point 0 and
null pointers might not be all bits 0) so why pay the cost of zeroing
the memory?
Ah, OK just realized, calloc zeroes memory, malloc does not. I used
calloc because thats what the OP is using, so you have to assume they
are using it for a reason. Why would you use it otherwise?
Secondly, there are better ways to use sizeof.
sizeof *ptr
I dont understand how this is better though. Please explain.

MQ

Aug 2 '06 #8
"MQ" <mi************ **@gmail.comwri tes:
Flash Gordon wrote:
>In all probability neither is optimal. Firstly, the OP did not say that
it needed clearing to all bits 0 (remember that floating point 0 and
null pointers might not be all bits 0) so why pay the cost of zeroing
the memory?

I don't understand what you are talking about
I don't understand what the problem is.

The main difference between malloc() and calloc() is that calloc()
initializes the allocated object to all-bits-zero. There's no
guarantee that this is useful; all-bits-zero may not be a
representation of a meaningful value for all types. In particular,
there's no guarantee that either a null pointer value or a
floating-point 0.0 is represented as all-bits-zero.

Because of this, zeroing allocated memory is often a waste of time. A
better solution is often to allocate using malloc(), and then be
careful not to read any portion of the allocated object that you
haven't explicitly assigned a value to.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 2 '06 #9
MQ wrote:
Flash Gordon wrote:
>In all probability neither is optimal. Firstly, the OP did not say that
it needed clearing to all bits 0 (remember that floating point 0 and
null pointers might not be all bits 0) so why pay the cost of zeroing
the memory?

Ah, OK just realized, calloc zeroes memory, malloc does not. I used
calloc because thats what the OP is using, so you have to assume they
are using it for a reason. Why would you use it otherwise?
Because the OP does not know how to program in C yet.
>Secondly, there are better ways to use sizeof.
sizeof *ptr

I dont understand how this is better though. Please explain.
What changes to you have to make to
ptr = malloc(N * sizeof *ptr);
if the type of ptr is changed? I'll give you a clue, the answer is none.

Do you have to find the declaration of ptr to see if the correct amount
of memory is being allocated?

Seach the groups archives for a bit for further discussion.
--
Flash Gordon
Still sigless on this computer
Aug 2 '06 #10

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

Similar topics

4
3458
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A good way to do this is by example. So I will give an example and please tell me what you think: I have a base class A with a virtual destructor, and a class B that is it inherits publicly from A and defines som extra stuff.
16
2430
by: He Shiming | last post by:
Hi, I'm having a little bit of trouble regarding pointer casting in my program. I don't understand why the following two cases produce different results. Case 1: IInterface *pInterface = new CImplementation(); pInterface->Method(); Case 2:
8
5797
by: Luc Le Blanc | last post by:
I have 2 APIs that store/recall a void *. Since all I need to store is a 32-bit struct, I pass the actual data (instead of a pointer to it) as a void *: typedef { UInt8 color; UInt8 index; UInt16 resID; } GadgetData;
14
2447
by: Enrico `Trippo' Porreca | last post by:
Given: typedef struct Node Node; struct Node { void *obj; Node *next; }; typedef struct Stack Stack; struct Stack {
35
2673
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
2
2040
by: Maurice | last post by:
Hi, Is it legal to cast from void(*)(A*) to void(*)(B*)? Is it legal to cast from struct Derived{Base b; ...} to struct Base? I'm trying to get some inheritance and polymorphism in C and I end up with the following code: struct Base { void(*func)(struct Base*);
7
1849
by: Alfonso Morra | last post by:
How can this work ? I have the following declarations in a header: Header ========= typedef void (*VFPTR)(void) ; void FOO_Callback(void*, char*, char*, int, unsigned long, void*,void*); int bar(int *, char *, VFPTR, void *);
3
3642
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules' that takes arguments of type (void *) because the ADT must be able to deal with any type of data. In my actual code, I will code the function to take arguments of their real types, then when I pass this pointer through an interface function, I...
3
1737
by: LongBow | last post by:
Hello all, First of all, sorry for multiple question per one thread. I have two questions. First what I think might be the easier problem. I am capturing data from an embedded device which I need to display data. One particular piece of data is a 32 bit floating point value. The problem I am having is converting the 4-byte value which is read via an RS-232 link. How can I cast the 4-byte value to a value type float correctly? I have...
0
8202
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8707
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
8641
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...
0
8510
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7199
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...
1
6125
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5575
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
4202
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1812
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.