[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] pointer gymnastik



At 12:22 11-09-99 +0200, you wrote:
>Thomas M. wrote:
>> Hejsa
>
>Hej!
>
>> Jeg har en streng på x antal bytes,som "abcdefg..xyzABCDEFG..XYZ123..890"
>> Jeg vil gerne finde ud af index'et på et bestemt tegn 'B' f.eks. Problemet
>> er at index() returnerer en pointer til tegnet, i stedet for en index ind
>> i strengen (som f.eks. at B er nr. 29. tegn i strengen). Hvordan for jeg
>> lige omregnet dette til et brugbart index? (bruger det til at indexere med
>> ind i strengen igen med chars[indx])
>
>Hmm.. Jeg maa indroemme at jeg ikke kan huske en stdlibc funktion til at
>koerer noejagtigt dette (proev dog at kigge paa strstr()), men dette
>burde virke (maaske en smule langsomt):
>
>int lookupChar(const char *str, char c)
>{
>  char *p;
>  int i;
>
>  for (p = str, i = 0; *p != '\0'; *p++, i++)
>  {
>    if (*p == c)
>      return i;
>  }
>
>  return -1;
>}

  assignmenten: *p++ virker kun på grund af at hvad du har puttet ind i den
pointer to char incrementerer liniært, men vil ikke virke på alle
compilers, at sige p++ derimod vil incrementere p til den næste hukummelses
adresse af den type den er allocated til. Se måske "The C++ Programming
language" omkring det. Det virker men er ikke tilrådeligt.

  Med Venlig Hilsen
  Kim Pedersen
>
>> Thomas Mørch
>
>-- 
>| Per Vognsen (Randers - Denmark)| "Superstition is a  word the |
>| Mail: <sslug@sslug>   | ignorant  use   to  describe |
>| Voice: +45 20639009            | their own ignorance." - Sifu |
>



 
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] *