osdir.com
mailing list archive

Subject: RE: dynamic allocation of array of structures - msg#00021

List: linux.c-programming

Date: Prev Next Index Thread: Prev Next Index
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.

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming"
in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html



Was this page helpful?
Yes No
Thread at a glance:

Previous Message by Date: click to view message preview

retriving a dhcp assigned ip

How can I retrive the ip address assigned to my host by a dhcp server? I think I need to use ioctl with SIOCGIFADDR parameter, but I don't know where the address is saved. thanks -- Massimiliano Cialdi cialdi@xxxxxxxxxxx m.cialdi@xxxxxxxx cialdi@xxxxxxxxxxxxxxxxxxxx - To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html

Next Message by Date: click to view message preview

Re: retriving a dhcp assigned ip

On Sat, 2003-03-22 21:42:16 +0100, Massimiliano Cialdi <cialdi@xxxxxxxxxxx> wrote in message <20030322214216.45bc5681.cialdi@xxxxxxxxxxx>: > How can I retrive the ip address assigned to my host by a dhcp server? > I think I need to use ioctl with SIOCGIFADDR parameter, but I don't know > where the address is saved. Look for a sample program that does the job. For example, "ifconfig" from the net-tools package fetches the IP address... MfG, JBG -- Jan-Benedict Glaw jbglaw@xxxxxxxxxx . +49-172-7608481 "Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! ret = do_actions((curr | FREE_SPEECH) & ~(IRAQ_WAR_2 | DRM | TCPA)); pgpalfVsluaX6.pgp Description: PGP signature

Previous Message by Thread: click to view message preview

RE: dynamic allocation of array of structures

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. -----Original Message----- From: linux-c-programming-owner@xxxxxxxxxxxxxxx [mailto:linux-c-programming-owner@xxxxxxxxxxxxxxx] On Behalf Of J. Sent: Saturday, March 22, 2003 8:48 PM To: linux-c-programming@xxxxxxxxxxxxxxx Hello, struct { char *name; int telnr; } people[100]; Instead of statically allocating an array of 100 of these structures like I have done, I want to dynamically allocate the array at run-time and resize the number of structures some where at any time in the main program. I know how to resize char arrays and int arrays but I never (re)sized an array of structres which can be accessed with something as: ``people[4].name''. In Google nothing really usefull turns up. Can this be done without having to resort to other implementations like linked lists etc.. ? If so how ? Thank you in advance. J. - To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html - To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html

Next Message by Thread: click to view message preview

Re: dynamic allocation of array of structures

On Sat, Mar 22, 2003 at 09:48:12PM +0100, J. wrote: > Maybe some of you could give a hunch what goes wrong. You should use linked lists. The code below can work if you save the starting offset of you elements, but in general it is not safe. > #include <stdio.h> > #include <stdlib.h> > > typedef struct { > char initial; > int roomnr; > } record; > > int main(void) { > int i = 0; - record *ptr; + record *ptr, *t; > if((ptr = (record *)malloc(10 * sizeof(record))) == NULL) { > fprintf(stderr, "Error: failed malloc\n"); > return 1; > } + t = ptr; > for(i = 0; i < 10; i++, ptr++) { > ptr->initial = 'A' + i; > ptr->roomnr = i; > } + ptr = t; > for(i = 0; i < 10; i++, ptr++) { > printf("ptr->initial = %c, ", ptr->initial); > printf("ptr->roomnr = %d\n", ptr->roomnr); > } > > return 0; > } Elias -- University of Athens I bet the human brain Physics Department is a kludge --Marvin Minsky - To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html
Loading Comments...
Home | News | Patents | Sitemap | FAQ | advertise

Advertising by