473,804 Members | 3,748 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1470
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(s izeof(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(s izeof(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************ **********@z14g 2000cwz.googleg roups.com>,
rory <ro*******@gmai l.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(s izeof(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*******@gmai l.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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

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

Similar topics

6
3675
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 what conditions can I use two or more frameworks? Sorry for the beginners question.
23
6034
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 causing me to "lose" a card. How do I avoid this??? I've attached my code below for reference. Thanks in advance. ------------------------ #include <iostream> #include <cstdlib> #include <ctime>
9
1631
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 suitable development environment for me to use for developing a Windows application? - If I am developing a Windows application I presume I am better using a GUI
10
2093
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 data, data for the shares and a form where I register how many shares each owner owns. Now to the difficult(?) issue. I need to set a number for the shares so I can make an ownership report showing wich sharenumbers people owns. I have thougt...
3
1442
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 needed? I'd like to read more about it, but there's nothing in K&R on this theme, AFAIR. 2. Suppose, I want to see the source code of the "fopen" function used
4
1670
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 serializable isolation level: T1 begin T2 begin T1 modifies an item A
20
2299
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
2672
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 rather complex without examples of compiling modules or making and using classes. Is there some C++ books that takes you through the whole process of making modules, compiling them and using classes?
2
11945
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 mallocing space for the pointer 'countPtr' in each struct, but do I need to do anything else? Thanks. typedef struct TwoCounts { int *countPtr;
0
9706
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
9584
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
10583
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
10337
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
9160
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
7622
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
6854
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
5525
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.