[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] malloc ter sig underligt



Anders Melchiorsen <sslug@sslug> writes:

> Garantien for at et int*-array er mere nul-afsluttet end et int-array
> er da vist til at overskue. Så vi skal i begge tilfælde vide noget om
> hvad funktionen returnerer.

Hvis vi returnerer en int* ser det således ud:

   pointer 
     |       +----+----+----+----+----+----+----
     +-----> | 42 | 17 |  9 |  0 | -6 | -2 | ...  
             +----+----+----+----+----+----+----

Uden mulighed for at vide hvornår vi slutter da vi ikke har et
afvigende element.


Hvis vi bruger int** ser det således ud:

   pointer
     |        +---+---+---+---+---+---+------+----
     +----->  | * | * | * | * | * | * | NULL | ...
              +-+-+-+-+-+-+-+-+-+-+-+-+------+----
                |   |   |   |   |   |  
              +---+ | +-+-+ | +-+-+ |
              | 42| | | 9 | | | -6| |
              +---+ | +---+ | +---+ |
                    |       |       |
                  +-+-+   +-+-+   +-+-+
                  | 17|   | 0 |   | -2|
                  +---+   +---+   +---+

Her kan vi tydelig se at når *pointer er NULL så er der ikke flere værdier.


Man kan selvfølgelig beslutte sig for at vores funktion kun kan
returnerer positive tal og man derfor kan bruge 0 som
stop-element. Men den generelle konvention er altså at bruge
dobbelt-pointere, undtagen ved tekststrenge hvor char netop kan
indeholde et stopelement (tegn 0).


>>>> int **pointer;
>>>> pointer = find_numbers;
>>>> while(pointer != NULL) {
>>>>     printf("%d\n",**pointer)
>>>>     pointer++;
>>>> }
>>>
>>> Der mangler vist lige en stjerne foran "pointer" i while-linjen.
>>
>> Nej.
>
> Så afsluttes løkken aldrig (hvis den går i gang)?

Doh ja, i selve betingelsen.

-- 
 Peter Makholm     |                The four letter word beginning with L?
 sslug@sslug |                It's life, love, libc or lisp         
 http://hacking.dk |                    --  Depending on you point of view


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

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