473,804 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

2D array of structures

Hello,

I wonder how to resize such array of structures using realloc()?

#include <stdio.h>
#include <stdlib.h>
#define FIRST 7

typedef struct {
char *name;
int i;
int j;
} STRUCTURE;

STRUCTURE **p_structure;

int main() {

p_structure = (STRUCTURE **) malloc(FIRST * sizeof(STRUCTUR E));
if ( p_structure == NULL ) {
printf("Failed to allocate memory, exiting...");
return 1;
}
}

Thank you in advance

Svata

Nov 7 '06
44 5826
svata wrote:
>
I always use google to search an answer, but often results are not
relevant.
>I agree with what you've said, but the OP should also read section
6 of the comp.lang.c FAQ before posting back here. Section 6
includes ways of dynamically allocating space for things that work
like 2D arrays. The FAQ can be found at http://c-faq.com/
I'm sure you've been told this before, but DON'T TOP-POST. See the
links in my sig below.

--
Some informative links:
<news:news.anno unce.newusers
<http://www.geocities.c om/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html >
<http://www.netmeister. org/news/learn2quote.htm l>
<http://cfaj.freeshell. org/google/>
Nov 9 '06 #11
Richard Heathfield wrote:
svata said:
>Chris Dollin wrote:

I learn by doing. So I will see what results I get.
Something happend to your quoting, Richard: it looks like it was me that
said "I learn by doing. So I will see what results I get.", but it was
svata.

(svata's quoting was misleading anyway, since he half-top-posted.)
That's fine sometimes, but there are also times when it's best to learn from
other people's knowledge and experience. If you learn C "by doing", you're
likely to end up doing lots of things that aren't correct, but which happen
to behave in a particular way on your current system. Switch systems, and
all your code breaks. Oops.

Chris is an expert on C.
I'd hesitate to describe myself so, since there's so much C I don't
know (most of the C99 stuff, for example!) and I've used it in so
few environments. I know /some/ stuff about C. I hope it's useful
stuff.
Listen to Chris.
But not when I'm singing.

--
Chris "for that, you want Annie, Rachel, Anne-Marie, or Tina." Dollin
"Reaching out for mirrors hidden in the web." - Renaissance, /Running Hard/

Nov 9 '06 #12
Chris Dollin wrote:
Richard Heathfield wrote:
.... snip ...
>
>Listen to Chris.

But not when I'm singing.
Like me, you appear to be a member of the 'crows in heat' chorus.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>
Nov 9 '06 #13
CBFalconer wrote:
Chris Dollin wrote:
>Richard Heathfield wrote:
... snip ...
>>
>>Listen to Chris.

But not when I'm singing.

Like me, you appear to be a member of the 'crows in heat' chorus.
Very likely. Or hedgehogs ... snuffling. Perhaps we should
characterise Undefined Behaviour as "Chris or <your preferred
abbreviationwil l sing for/at you". /That/ will worry them
more than hyperbolic nasal demons.

--
Chris "we are BRO-KEN, without FEE-LING" Dollin
"Who do you serve, and who do you trust?" /Crusade/

Nov 9 '06 #14
Chris Dollin said:
Richard Heathfield wrote:
>svata said:
>>Chris Dollin wrote:

I learn by doing. So I will see what results I get.

Something happend to your quoting, Richard:
No, just a snip slip. Sorry about that.
>Chris is an expert on C.

I'd hesitate to describe myself so,
Think "relative". Compared to most OPs around here, you're a towering
genius!

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 9 '06 #15
svata:
Hello Frederick,

Hello svata :)

I know it doesn't make sense. I was wrong in my assumption.
I should use p_structure = (STRUCTURE **) malloc(FIRST *
sizeof(STRUCTUR E*));

but anyway... should rather use array of structures.
typedef structure {
code goes here...
} _structure;

_structure *p_structure;

One thing that you'll notice as you program more and more is that you'll
come to use abbreviations (most programmers do in anyway). Humans love
shortcuts. "should have" became "should've" , which became "shuda". The
words "until" and "because" are slowly but surely becoming "til" and
"cause".

When I myself define a pointer variable, I simply prefix "p" to the name; I
used to put an underscore with it too but then more months passed by and I
got lazier and lazier. The variable name "p_structur e" is a bit of a
mouthful, maybe you'd prefer to keep the names small.

There are a few different prevalent coding styles out there. My own style
works as follows:

(1) Functions and Types start with a capital letter. If it consists of more
than one word, than the next word starts with an initial capital, e.g.

void TruncateLastFiv eDigits(char*);

(2) Objects start with a lower case letter. If it consists of more than one
word, then underscores are used, e.g.

int len_main_str = 34;

(3) For a pointer, I prefix a simple "p" to the name where possible:

void (*pFunc)(int) = Func;
void (**ppFunc)(int) = &pFunc;

int *pobj = &obj;
int **ppobj = &pobj;
int ***pppobj = &ppobj;

(4) Macro names are in ALL CAPS. No other name should be written in ALL
CAPS, e.g.:

#define LEN 5
#define SQR(x) ((x)*(x))

Of course, this is just my own style. I'm not trying to shove it down your
throat, but I'm just suggesting it as I thought you might like it.

// and then malloc()

p_structure = malloc( INT * sizeof(_structu re));

if am I right?

Yes, you're right. It's handy though to not have to repeat the name of the
type:

int *const p = malloc(5 * sizeof(int));

can be written as:

int *const p = malloc(5 * sizeof*p);

Now, if we change the type to double, we only have to change "p":

double *const p = malloc(5 * sizeof*p);

Here's a taste of how I might write the code:

#include <stddef.h /* To use "size_t" */
#include <stdlib.h /* To use "malloc" and "free" */

typedef struct MyStruct {
int i;
} MyStruct;

size_t GetNumberFromSo mewhere(void); /* Defined elsewhere */

int main(void)
{
size_t const len = GetNumberFromSo mewhere();

MyStruct *const p = malloc(len * sizeof*p);

/* ... */

free(p);

return 0;
}

--

Frederick Gotham
Nov 9 '06 #16
svata wrote:

Please do not top post. Your reply belongs under the text you are
replying to, not above.
svata
>I agree with what you've said, but the OP should also read section 6 of
the comp.lang.c FAQ before posting back here. Section 6 includes ways of
dynamically allocating space for things that work like 2D arrays. The
FAQ can be found at http://c-faq.com/
I always use google to search an answer, but often results are not
relevant.
I did not suggest using Google, I suggested a specific resource and a
particular area in it. From another post here it is obvious you have not
followed that advice, and if you don't bother following advice or
posting properly I see no reason to bother giving you advice.
--
Flash Gordon
Nov 9 '06 #17
Chris Dollin <ch**********@h p.comwrites:
[...]
If you /must/ use a typedef for a structure -- you don't
need to, and some wise people argue that you shouldn't
(although others argue that those arguments aren't
convincing) -- you should also give it a /sensible/
name. "structure" isn't.

typedef struct yourStructTagHe re
{
int x;
int y;
} Point;
There's also no need to use different identifiers for the struct tag
and the typedef name:

typedef struct Point {
int x;
int y;
} Point;

Now you can refer to the type either as "Point" or as "struct Point".

The purpose of the typedef is to allow you to use a single identifer
to refer to the type. The argument Chris alluded to above *against*
using a typedef is that gives a second name to a type that already has
a perfectly good name. For example, I would have declared it as:

struct Point {
int x;
int y;
};

and just refer to the type as "struct Point".

But plenty of smart people prefer to use the typedef. And if your
structure doesn't contain any pointers to itself (as for a linked list
node, for example), you don't need the struct tag:

typedef struct {
int x;
int y;
} Point;

The drawback of this is that the name "Point" doesn't become visible
until the end of the declaration, so you can't declare a member of
type Point*.

--
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.
Nov 9 '06 #18
Frederick Gotham wrote:
>
.... snip ...
>
One thing that you'll notice as you program more and more is that
you'll come to use abbreviations (most programmers do in anyway).
Humans love shortcuts. "should have" became "should've" , which
became "shuda". The words "until" and "because" are slowly but
surely becoming "til" and "cause".
Not the smarter ones. They try to stick to English, and remain
fairly clear to their readers. Should've is legitimate English,
shuda is an execresence. Misuse of the word 'cause' causes nothing
but confusion.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

Nov 9 '06 #19
Keith Thompson said:
Chris Dollin <ch**********@h p.comwrites:
[...]
>If you /must/ use a typedef for a structure -- you don't
need to, and some wise people argue that you shouldn't
(although others argue that those arguments aren't
convincing) -- you should also give it a /sensible/
name. "structure" isn't.

typedef struct yourStructTagHe re
{
int x;
int y;
} Point;

There's also no need to use different identifiers for the struct tag
and the typedef name:

typedef struct Point {
int x;
int y;
} Point;
It's true that there's no C reason to use different identifiers, but I do so
anyway because it helps Microsoft's "Intellisen se" to work out what you
mean when you hit the button that says "take me to your definition". Not
that I use Microsoft C terribly often - but when I do, I usually end up
thanking myself for using a unique tag name. My preferred style nowadays
is:

struct foo_
{
char coal;
short wait;
unsigned letter;
long time;
float away;
double trouble;
bar baz;
ad nauseam;
};

typedef struct foo_ foo;

<snip>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 9 '06 #20

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

Similar topics

8
1962
by: michi | last post by:
Hello everybody, I have following problem: I have an array of pointers to structures: table* tab = new table; and structure table is like this: struct table{ CSLL::node* chain;
4
7300
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving from an interface and only implementing some of it means something is wrong: either the interface specification is wrong e.g. not minimal or the derivation is wrong e.g. the type can't actually honour this contract.
8
2474
by: ulyses | last post by:
I'm trying to put pointer to flexible array of structures in other structure. I want to have pointer to array of pixels in screen structure. Here is mine code, but I think it isn't quite all right: struct pixel { int x; int y; int color; };
104
17027
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from that array Could you show me a little example how to do this? Thanks.
7
3170
by: Sam | last post by:
Hello I have a structure called Company. struct Company { char *employee; char *employee_address; }; I want to build an array of this structure but the number of employees will change thorughout the course the programs use so it will need to
12
3892
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm trying to access // that has two ports. Each port has 10 sequential // registers. Create a...
11
3788
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
17
2331
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the same as its address. The second one simply depends on a term that is not well-defined. Most people consider the type to be an important part of the notion of
5
3804
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);" I thought that it is very hard to memory map structure array. I need both read and write memory mapped file at both side of C# and C++.
0
9715
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
9595
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
9175
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
7642
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
6867
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
5535
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
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4313
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
2
3835
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.