Path: utzoo!utgpu!watmath!att!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!mcvax!unido!rwthinf!ruso!wolfgang
From: wolfgang@ruso.UUCP (Wolfgang Deifel)
Newsgroups: comp.lang.c
Subject: Re: pointer increment
Message-ID: <829@ruso.UUCP>
Date: 15 Aug 89 10:39:40 GMT
References: <484@eagle.wesleyan.edu>
Organization: Dr. Ruff Software GmbH, Aachen, W.Germany
Lines: 22

dkonerding@eagle.wesleyan.edu writes:


>...  int *ptr;
>        Now, say I make ptr=1000 or hex 3e8.  I want to add one to the ptr, to
>make it point to 1001, or hex 3e9.  Every time I do a ptr=ptr+1, it becomes
>3ec.  How can I simply increment ptr by one?

    ptr is a pointer to an int or array of ints. When you add 1 or increment
ptr it always refers to the next element e.g. it becomes sizeof(*ptr) more.
If you want to increment ptr only by one you should use a cast.

    ((char*)ptr)++ ;
or
    ((char*)ptr) + 1 ;

Now ptr becomes 1 ( = sizeof(char) ) more.

----------------------------------------------------------------------------
Wolfgang Deifel
Dr. Ruff Software GmbH, 5100 Aachen, Juelicherstr. 65-67, W-Germany
uucp: ...!uunet{!mcvax}!unido!rwthinf!ruso!wolfgang - phone : +49 241 156038