pointer increment

Wolfgang Deifel wolfgang at ruso.UUCP
Tue Aug 15 20:39:40 AEST 1989


dkonerding at 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



More information about the Comp.lang.c mailing list