linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "J." <mailing-lists@xs4all.nl>
To: linux-c-programming@vger.kernel.org
Subject: RE: dynamic allocation of array of structures
Date: Sat, 22 Mar 2003 21:48:12 +0100 (CET)	[thread overview]
Message-ID: <Pine.LNX.4.21.0303222141500.5852-100000@hestia> (raw)
In-Reply-To: <20030322202720.267284000B9@mwinf0501.wanadoo.fr>

On Sat, 22 Mar 2003, Anthony Nguyen wrote:

> typedef struct Apeople { 
> char *name;
>  int telnr;
> } Tpeople;
> 
> ...
> 
> Tpeople *toto;
> 
> toto = (Tpeople *)(malloc (sizeof Tpeople) * <the number of elements you
> want to allocate>);
> 
> But I think that you were searching a "proper way" to implement it, isn't it
> :?
> 
> A.N.

Lol :) euh ... to be honest .. actually yes ..

Of course before I decided to send a message to this list I have tried
something like you described above, but it doesn't work quitte as I
expected or better said, like I want it to work.

Maybe some of you could give a hunch what goes wrong. 
(Code compiles without Complaining)

#include <stdio.h>
#include <stdlib.h>

typedef struct {
 char initial;
 int roomnr;
} record;

int main(void) {
 int i = 0;
 record *ptr;

 if((ptr = (record *)malloc(10 * sizeof(record))) == NULL) {
  fprintf(stderr, "Error: failed malloc\n");
  return 1;
 }

 for(i = 0; i < 10; i++, ptr++) {
  ptr->initial = 'A' + i;
  ptr->roomnr = i;
 }

 for(i = 0; i < 10; i++, ptr++) {
  printf("ptr->initial = %c, ", ptr->initial);
  printf("ptr->roomnr = %d\n", ptr->roomnr);
 }

 return 0;
}

Thnkx.. J.


  reply	other threads:[~2003-03-22 20:48 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-02-28  3:26 ODBC Under Linux chuckw
2003-02-28 21:31 ` Glynn Clements
2003-03-19 13:22   ` How to open a file and to disable other processes from removing it Alexi Jordanov
2003-03-19 15:45     ` Glynn Clements
2003-03-22 19:47       ` dynamic allocation of array of structures J.
2003-03-22 20:27         ` Anthony Nguyen
2003-03-22 20:48           ` J. [this message]
2003-03-22 21:00             ` Elias Athanasopoulos
2003-03-22 21:28               ` Glynn Clements
2003-03-22 21:44             ` dynamic allocation of array of structures, Thnkx J.

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.21.0303222141500.5852-100000@hestia \
    --to=mailing-lists@xs4all.nl \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).