[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: [CPROG] Header udtraek



Jakob Hilarius wrote:
> 
> Hejsa,
> 
> Måske er det bare fordi jeg er dum, ellers er det bare fordi jeg
> har fået lidt for meget koffein og ikke kan tænke ordenligt, men
> jeg har en C funktion jeg ikke kan få til at virke, som jeg vil
> have den til at gøre.
> Jeg har en streng der indeholder nogle email headere,
> "From: ...\nDate: ...\n...". Jeg vil gerne have en funktion der
> returnere indholdet af en given header.
> Jeg har lavet nedenstående funktion, den giver også indholdet af
> den header jeg angiver, men den ændre også indholdet af strengen
> jeg angiver som buffer parametren.
> Altså hvis "buf" indholder mine headere, og jeg kalder funktionen:
> date_header = get_header("Date:", buf, i);
> Får date_header  indholdet af Date: headeren som den skal, men
> bagefter indeholder buf kun indholdet af Date: headeren, og det
> var ikke menningen :-).
> 
> char *get_header(char *header, char *buffer, int taeller) {
>   char *name;
>   int i,j;
> 
>   if(buffer[taeller] != header[0])
>     return NULL;
> 
>   for(i = 1; i < strlen(header); i++) {
>     if(buffer[taeller+i] != header[i])
>       return NULL;
>   }
> 
>   for(j = 0; buffer[taeller+i+j+1] != '\n'; j++)
>     name[j] = buffer[taeller+i+j+1];
>   name[j] = '\0';
> 
>   return name;
> }
> 

Der mangler ligesom at blive allokeret nogle bytes til 'name'! Det er jo
ikke nok, at det er en pointer!
Det der nok sker i praksis er, at name får lov at pege på
buffer[taeller+i+0+1]
og derfor bliver din string terminering også til en terminering i
buffer.
Du kunne såmænd også have fået 'core' på besøg ;-)
Prøv med compiler option -Wall og se om den ikke brokker sig bare lidt.

Med venlig hilsen
Egon Andersen

> --
>   Jakob Hilarius
>   http://rrk.kollegienet.dk/~jhsn


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:08 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] *