473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Beginner question: Malloc on array of pointers to structs

hal
Hi,

I'm trying to make an array of pointers to 'TwoCounts' structs, where
the size of the array is arraySize. Right now I'm just mallocing
enough space for all the pointers to the structs, and mallocing space
for the pointer 'countPtr' in each struct, but do I need to do
anything else? Thanks.

typedef struct TwoCounts {
int *countPtr;
int count;
} TwoCounts;

int j;
TwoCounts *twoCountsArray;
TwoCounts *tempPtr;

twoCountsArray = (TwoCounts *) malloc(arraySize * sizeof(TwoCounts
*));
for (j=0;j<arraySize;j++){
//get location of each pointer in the array
tempPtr = (twoCountsArray + arraySize * sizeof(TwoCounts *));
//initialize the countPtr
tempPtr->countPtr = (TwoCounts *) malloc (sizeof(int));
*(tempPtr->countPtr) = NULL;
}
Oct 26 '08 #1
2 11924
"hal" <z3****@gmail.comwrote in message news:
I'm trying to make an array of pointers to 'TwoCounts' structs, where
the size of the array is arraySize. Right now I'm just mallocing
enough space for all the pointers to the structs, and mallocing space
for the pointer 'countPtr' in each struct, but do I need to do
anything else? Thanks.

typedef struct TwoCounts {
int *countPtr;
int count;
} TwoCounts;

int j;
TwoCounts *twoCountsArray;
TwoCounts *tempPtr;

twoCountsArray = (TwoCounts *) malloc(arraySize * sizeof(TwoCounts
*));
for (j=0;j<arraySize;j++){
//get location of each pointer in the array
tempPtr = (twoCountsArray + arraySize * sizeof(TwoCounts *));
//initialize the countPtr
tempPtr->countPtr = (TwoCounts *) malloc (sizeof(int));
*(tempPtr->countPtr) = NULL;
}
No, you've got it wrong.

First a style issue. You've got a member named countPtr and member named
count. Then you call the struct TwoCounts. This is highly confusing, and I
can't actually work out what is intended.
If you want an arbitrary-length list of integers, call the count member "N"
and call the pointer "ptr" if the structure is a generic list of integers,
"counts" if you use it only for storing counts specifically. Better still,
say what you are counting.

twoCountsArray is a pointer that points to a buffer containing TwoCounts
structs, laid out contiguously in memory. So you need to say

twoCountsArray = (TwoCounts *) malloc( ArraySize * sizeof(TwoCounts) );

Not sizeof a pointer, sizeof the structure.
you don't actually need the cast, and some people will recommend sizeof
*twoCountsArray rather than sizeof(TwoCounts), although these points are a
bit contentious.

Now you want to intialise the array of structures you have created. So use
array notation

for(j=0;j<ArraySize;j++)
{
twoCountsArray[j].count = 1;
twoCountsArray[j].countPtr = malloc(sizeof(int));
twoCountsArray[i].countPtr[0] = 0;
}

I presume here that you want to initialise the structures to list of one
integer, which starts off with a value of zero.
There's no need to mess about with temporary pointers. Array notation is
much less confusing.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Oct 26 '08 #2
>I'm trying to make an array of pointers to 'TwoCounts' structs, where
>the size of the array is arraySize. Right now I'm just mallocing
enough space for all the pointers to the structs, and mallocing space
for the pointer 'countPtr' in each struct, but do I need to do
anything else? Thanks.
You have allocated space for an array of pointers, and for some ints,
but you have allocated nothing for any struct TwoCounts. I
don't know what your intentions are, but this screams error.
>
typedef struct TwoCounts {
int *countPtr;
int count;
} TwoCounts;

int j;
TwoCounts *twoCountsArray;
TwoCounts *tempPtr;

twoCountsArray = (TwoCounts *) malloc(arraySize * sizeof(TwoCounts
*));
for (j=0;j<arraySize;j++){
//get location of each pointer in the array
tempPtr = (twoCountsArray + arraySize * sizeof(TwoCounts *));
//initialize the countPtr
tempPtr->countPtr = (TwoCounts *) malloc (sizeof(int));
*(tempPtr->countPtr) = NULL;
}

Oct 26 '08 #3

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

Similar topics

36
by: Bhalchandra Thatte | last post by:
I am allocating a block of memory using malloc. I want to use it to store a "header" structure followed by structs in my application. How to calculate the alignment without making any assumption...
10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
7
by: Fabian Wauthier | last post by:
Hi list, I am trying to dynamically grow a 2 dimensional array (Atom ***Screen) of pointers to a struct Atom (i.e. the head of a linked list). I am not sure if this is the right way to do it: ...
35
by: David Cleaver | last post by:
Hello all, I was wondering if there were some sort of limitations on the "if" statement? I'm writing a program which needs to check a bunch of conditions all at the same time (basically). And...
11
by: rory | last post by:
Cna anyone point me in the right direction, I have a struture in my .h file: typedef struct mdata { int names; int dates; int ages; }m_Data;
5
by: Bidule | last post by:
Hi, I'm trying to sort structs defined as follows: struct combinationRec { float score; char* name; }; The number of structs and the length of the "name" field are not known
7
by: Jake Thompson | last post by:
Hello I have the following defined structure struct cm8linkstruc { char *type; /* type of item*/ char *desc; /* description of item ...
5
by: Yourko | last post by:
Hi there! I`me currently trying to write some simple programs in C. For one such program i created globals.h file. In that file i defined a structure of type _client, and a pointer of that type: ...
15
by: sethukr | last post by:
Hi everybody, While running the following program in GCC, i'm very much screwed. main() { char *ptr1; char arr; int i; char *ptr2;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...
0
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...
0
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...

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.