[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] Bog om C



Den Thu, 13 May 2004 08:03:32 +0200 skrev Kenneth
Geisshirt

> 
> Michael Rasmussen wrote:
> > On Wed, 12 May 2004 11:29:12 +0200, henrik wrote:
> > 
> > 
> >>På novice-niveau siger du? Tak skal du have! Nå,
> > 
> > Nu var det ikke en henvisning til, at du er novice.
> Blot en oplysning til
> > dig om, at du ikke havde grund til at anskaffe den,
> hvis du selv mente,
> > du var over novice stadiet:-)
Jeg er nok stadig på novicestadiet i C, så det er helt
OK:-)
> > 
> >>faktisk har jeg et novicespørgsmål: Jeg
> > 
> > har set i C-kode at strukturer
> > 
> >>af variabel størrelse defineres noget lignende sådan
> her: struct text
> >>{
> >>    int length;
> >>    char data[1];
> >>};
> >>hvor data så kan have hvilken som helst længde. Er
> der nogen som kan
> >>sige mig hvordan det fungerer og hvad fidusen er med
> denne konstruktion?
> >>Hvorfor ikke ... char *data; ...?
> >>
> > 
> > En god grund kunne være, at struct's selv reserverer
> den nødvendige
> > hukommelse, der er behov for, så hvis længde sættes
> til 10, vil struct
> > have længden 10 + 1 bytes (char data[1], der så kan
> bruges til '\0').
> > 
> 
> Det vil jeg gerne have forklaret nærmere. Michael,
> kunne du ikke vise 
> det med et lille eksempel?
> 
> Pft Kneth
> 
> -- 
> Kenneth Geisshirt, M.Sc., Ph.D.  -- 
> http://kenneth.geisshirt.dk
>           Good judgment comes from experience.
>                Experience comes from bad judgment. --
> Jim Horning
Jeg kan se at man allokerer den mængde hukommelse man
har brug for til en given strenglænge dvs. størrelsen
på structen + antal tegn:

text tt = (text *)malloc(sizeof(text) + 6 *
sizeof(char));

hvorefter man kan tilgå data[0] .. data[6] fordi C ikke
har check af array-grænser. Man skal sørge for at
'data' står sidst i structen.

Jeg stødte ind i det her i PostgreSQL's doc om
C-funktioner - PostgreSQL's variabel længde typer er
defineret på denne måde.
---
Hilsen
Henrik Zederkof


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 22:43 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *