[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
 

Sv: [CPROG] Modificering af streng i funktion



En ganske normal fejl.

Peter Seidler <sslug@sslug> skrev i en
nyhedsmeddelelse:sslug@sslug
> Jeg har et program ala dette
>
> main(int argc, char **argv)
> {
> char filename[256];

Dette er svarende
    char *filename;
    filename=malloc(256);

Din filename variabel er derfor allerede en pointer til et array!

>
> parse_args(argc, argv, &filename);

Du kalder nu parse_args med en pointer til der, hvor adressen på dit array
ligger!

>
> printf("main: %s", filename);

Korrekt brug - printf forventer en pointer

>
> ...
> }
>
> void parse_args(int argc, char** argv, char** filename)
> {
> *filename = *(argv + 1); /* Filename is always here. */

Du overskriver nu din pointer til dit 256 tegns array. Dette er ret fy-fy,
og efterfølgende adressering vil gå galt.

> printf("parse_args: %s, %s", *filename, *(argv + 1));

Her benytter du din overskrevne pointer, hvilket vil gå godt. Programmet er
dog potentielt ustabilt nu.
> }

Der er to måder du kan gøre det på:

1:
int main(int argc, char **argv)
{
char *filename;
    parse_args(argc,argv,&filename); // Du vil kunne modificere din pointer,
så du bliver nødt til at kalde sådan
    printf("main: %s\n",filename;
    return 0;
}

void parse_args(int argc, char **argv, char **filename)
{
    *filename=*(argv+1); // Her gemmer du pointeren i din pointer variabel!
// eller    *filename=argv[1]; // giver samme resultat
    printf("parse_args: %s %s\n", *filename, *(argv+1));
}

2:
int main(int argc, char **argv)
{
char filename[256];
    parse_args(argc,argv,filename); // Du kopierer data fra argv[1] til
filename arrayet
    printf("main: %s\n",filename;
    return 0;
}

void parse_args(int argc, char **argv, char *filename)
{
    strcpy(filename,argv[1]); // her kopierer du DATA til dit filename
array!
    printf("parse_args: %s %s\n", filename, argv[1]);
}

Du kan eventuelt prøve begge metoder. I den første metode laver du en ny
pointer til argv[1] indholdet. Dette kan være farligt, idet der ikke er mere
plads i strengen end det, som oprindelig blev lavet (længden af dit
argument). I den anden metode laver du en kopi af data i argv[1]. Du kan nu
arbejde med og ændre indholdet så der er op til 255 tegn i strengen uden at
få fejl.
Bemærk at begrænsingen på 255 tegn er at der også skal være plads til
0-terminatoren i slutningen af strengen.

Håber dette belyste dit problem lidt.

Med venlig hilsen

Keld Laursen




 
Home   Subscribe   Mail Archive   Index   Calendar   Search

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