473,401 Members | 2,068 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,401 software developers and data experts.

beginner: structures...

Cna anyone point me in the right direction, I have a struture in my .h
file:

typedef struct mdata
{
int names[500];
int dates[500];
int ages[500];
}m_Data;

and in my .c file I declare a pointer to this strucutre and put aside
memory to hold it:

m_Data* firstlist;
firstlist = (m_Data*)malloc(sizeof(m_Data));

What I'm wondering about it is if there is a way that I can change each
of the elements from being a fixed size of 500 to a being user defined
size? I'm guessing I can make the elements pointers but do I use malloc
on each of the elemnnts, and if so isn't that changing the size of the
struture in which I have already put aside an initial memory of
'malloc(sizeof(m_data))'. As you can see I'm a little confused, any
ideas?

regards,
Rory.

Nov 15 '05 #1
11 1437
Given your orig. structure below there is no way to change the
memory already
allocated to the structural elements. Its fixed at '500'

If you want to change the mem. allocated to the structure elements at
runtime you need to define the original structural elements
as pointers first .
like -

typedef struct mdata
{
int *names;
int *dates;
int *ages;
}m_Data;

then in your program you can allocate memory (yes, using malloc) to
those elements depending on its usage scenario.
Ex: my_Data->name = malloc (sizeof (m_Data->name));
[given my_Data is declared as a pointer to m_Data.]

Get a C-book and read the section on pointers !

- Ravi
Cna anyone point me in the right direction, I have a struture in my .h
file:

typedef struct mdata
{
int names[500];
int dates[500];
int ages[500];
}m_Data;

and in my .c file I declare a pointer to this strucutre and put aside
memory to hold it:

m_Data* firstlist;
firstlist = (m_Data*)malloc(sizeof(m_Data));

What I'm wondering about it is if there is a way that I can change each
of the elements from being a fixed size of 500 to a being user defined
size? I'm guessing I can make the elements pointers but do I use malloc
on each of the elemnnts, and if so isn't that changing the size of the
struture in which I have already put aside an initial memory of
'malloc(sizeof(m_data))'. As you can see I'm a little confused, any
ideas?

regards,
Rory.


Nov 15 '05 #2
ajm
Hi Rory,

If you want variable sized members within your structure then you have
no choice but to use pointers (i.e., malloc and co.) so you have:

typedef struct mdata
{
int *names;
int *dates;
int *ages;
} m_Data;

it might be good to define a function to dimension your structure,
e.g., with prototype

m_Data * mdata_init(size_t names_sz, size_t dates_sz, size_t ages_sz);

which uses the *_sz arguments to perform the necessary mallocs and
return the
pointer to the caller. you still use the (m_Data *)malloc call before
returning the pointer
to the caller since this is accounts for the memory cost of holding
your int pointers and
so can be determined consistently.

you might also want to write a mdata_destroy to perform the necessary
frees too.

if in doubt just write a couple of short programs to try out the ideas
and post any
queries you still have.

hth,
ajm.

Nov 15 '05 #3
rory wrote:

Cna anyone point me in the right direction, I have a struture in my .h
file:

typedef struct mdata
{
int names[500];
int dates[500];
int ages[500];
}m_Data;

and in my .c file I declare a pointer to this strucutre and put aside
memory to hold it:

m_Data* firstlist;
firstlist = (m_Data*)malloc(sizeof(m_Data));
#include <stdlib.h>
firstlist = malloc(sizeof *firstlist);

The reference to m_Data is not needed with the malloc call.
The cast can hide your failure to #include <stdlib.h>

What I'm wondering about it is
if there is a way that I can change each
of the elements from being a fixed size of 500
to a being user defined size?
Yes.
I'm guessing I can make the elements pointers but do I use malloc
on each of the elemnnts,
Yes.
and if so isn't that changing the size of the
struture in which I have already put aside an initial memory of
'malloc(sizeof(m_data))'.
No.
The structure will only hold three pointers.
As you can see I'm a little confused, any
ideas?


Use pointers like you were thinking.

--
pete
Nov 15 '05 #4
Cheers, I am now defining the original structure as you suggested with
pointers, and I try to allocate the space for each like this

m_Data* list1;
list1 = (m_Data*)malloc(sizeof(m_Data));
list1->note = (m_Data->names*)malloc(sizeof(m_Data->names)*50);
list1->channel = (m_Data->dates*)malloc(sizeof(m_Data->dates)*50);
list1->duration = (m_Data->ages*)malloc(sizeof(m_Data->ages)*50);

however I get errors on the lines that I try to allocate memory to the
elements on:

example.c:23: parse error before '->' token
example.c:24: parse error before '->' token
example.c:25: parse error before '->' token

I hope it isn't a simple syntax error, if so I apologise in advance!

Nov 15 '05 #5
ajm
you don't need the m_Data-> etc. in the sizeof() calls and you can
replace the cast with (int *) since your "cast" is not a type.

Ravi's comment (jibe ;) to read the pointers section of a C book is not
bad advice...

hth
ajm.

Nov 15 '05 #6
rory wrote:

Cheers, I am now defining the original structure as you suggested with
pointers, and I try to allocate the space for each like this

m_Data* list1;
list1 = (m_Data*)malloc(sizeof(m_Data));
list1->note = (m_Data->names*)malloc(sizeof(m_Data->names)*50);
list1->channel = (m_Data->dates*)malloc(sizeof(m_Data->dates)*50);
list1->duration = (m_Data->ages*)malloc(sizeof(m_Data->ages)*50);


list1 = malloc(sizeof *list1);
list1 -> note = malloc(50 * sizeof *list1 -> note);
list1 -> channel = malloc(50 * sizeof *list1 -> channel);
list1 -> duration = malloc(50 * sizeof *list1 -> duration);

--
pete
Nov 15 '05 #7
In article <11**********************@z14g2000cwz.googlegroups .com>,
rory <ro*******@gmail.com> wrote:
Cheers, I am now defining the original structure as you suggested with
pointers, and I try to allocate the space for each like this m_Data* list1;
list1 = (m_Data*)malloc(sizeof(m_Data));
list1->note = (m_Data->names*)malloc(sizeof(m_Data->names)*50);
list1->channel = (m_Data->dates*)malloc(sizeof(m_Data->dates)*50);
list1->duration = (m_Data->ages*)malloc(sizeof(m_Data->ages)*50); however I get errors on the lines that I try to allocate memory to the
elements on: example.c:23: parse error before '->' token


The parse error is in the second use of -> on each line.
m_Data is a structure type, not a pointer (not even a pointer
to a structure type -- it is list1 which is the pointer to a structure),
so m_Data->names is not valid.

You also appear to be doing something funky with what you are
trying to take the size of. The left hand side of your assignments
implies that list1 is a pointer to a structure which has elements
'note', 'channel', and 'duration', but the sizeof() part of
your expression implies that the structure has elements named
'names', 'dates', and 'ages' instead. Are you trying to imply
that 'note' is an array of structures of type 'names' ??

--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Nov 15 '05 #8
Cheers guys that's sorted it, I'm off now to the library to get a few
pointers on C programming (pun fully intended!).

Rory.

Nov 15 '05 #9
"rory" <ro*******@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Cna anyone point me in the right direction, I have a struture in my .h
file:

typedef struct mdata
{
int names[500];
int dates[500];
int ages[500];
}m_Data;

and in my .c file I declare a pointer to this strucutre and put aside
memory to hold it:

m_Data* firstlist;
firstlist = (m_Data*)malloc(sizeof(m_Data));
Firstly, the cast here is unnecessary. Unnecessary casts can mask errors; in
this case, forgetting to #include <stdlib.h>. If you forget, the compiler
assumes malloc() returns an int. The result is undefined behaviour, but the
cast typically avoids any warning.

Secondly, it makes more sense to specify the size of the object rather than
the object's type. That way, if the type changes you don't need to change
the call to malloc().

With these changes, the above is written:

firstlist = malloc(sizeof *firstlist);
What I'm wondering about it is if there is a way that I can change each
of the elements from being a fixed size of 500 to a being user defined
size?
Sure.
I'm guessing I can make the elements pointers but do I use malloc on each
of the elemnnts,
Yes; make the members pointers and allocate space for them with malloc(). It
is often useful to add members to the struct to hold the size of each
allocated array, eg:

typedef struct mdata {
int *names;
size_t names_size;
int *dates;
size_t dates_size;
int *ages;
size_t ages_size;
} m_Data;

If all the sizes are the same, you could have just a single size member, or
perhaps an array of structs would make more sense.
and if so isn't that changing the size of the struture in which I have
already put aside an initial memory of 'malloc(sizeof (m_data))'.


It increases the amount of memory "associated with" the structure, but
sizeof(m_Data) is just the memory required to store the members, and doesn't
include the memory that any pointer members point to.

Alex
Nov 15 '05 #10
rory wrote:
Cheers, I am now defining the original structure as you suggested with
pointers, and I try to allocate the space for each like this

Please read my sig below.
Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Nov 15 '05 #11
ajm wrote:
you don't need the m_Data-> etc. in the sizeof() calls and you can
replace the cast with (int *) since your "cast" is not a type.

Please read my sig below.
Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Nov 15 '05 #12

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

Similar topics

6
by: Al Dykes | last post by:
I've just grabbed a PHP book and can deal with the syntax and now I need to decide to learn specific packages and features. Define "framework". What are the major framework flavors ? Under...
23
by: hedylogus | last post by:
I've just begun learning C++ and I'm trying to write a program to shuffle a deck of cards. I've succeeded....for the most part....but every now and then rand() produces duplicate random numbers...
9
by: Steve | last post by:
Hi, I am completely new to C++. I have good experience in Java, VB and some experience in C (as well as a few other languages). Bearing in mind my experience - - What would be the most...
10
by: StenKoll | last post by:
Hi! I am fairly new to access and not very familiar with vba programming. I am trying to setting up a database of shareholders in a company. So far I have managed to build tables containing owner...
3
by: gruzdnev | last post by:
Hi all, I've started to program in C not long ago, and I've got some questions: (I work on Linux 2.4.22/Debian) 1. Why the "**var" construct is used? What are the cases when it is commonly...
4
by: Yoram Biberman | last post by:
I have a few questions concerning concurrency control. I shall thank whoever can help me. Question #1 ========= Assume the following (concurrent) schedule, in which both transactions run in a...
20
by: weight gain 2000 | last post by:
Hello all! I'm looking for a very good book for an absolute beginner on VB.net or VB 2005 with emphasis on databases. What would you reccommend? Thanks!
4
by: Johs | last post by:
I am looking for a good C++ book for beginners. I have some experience with C and a lot of experience with Java. I am currently reading Bjarne Stroustrups C++ Programming Language but it starts off...
2
by: hal | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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
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.