delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/03/14/19:45:06

Message-Id: <199903150044.TAA23466@delorie.com>
Comments: Authenticated sender is <mert0407 AT sable DOT ox DOT ac DOT uk>
From: "George Foot" <george DOT foot AT merton DOT ox DOT ac DOT uk>
To: "Ron Higgins" <ron AT west29 DOT freeserve DOT co DOT uk>
Date: Mon, 15 Mar 1999 00:42:47 +0000
MIME-Version: 1.0
Subject: Re: Newbie Pointers
CC: djgpp AT delorie DOT com
X-mailer: Pegasus Mail for Win32 (v2.42a)
Reply-To: djgpp AT delorie DOT com

Firstly, your working program has a bug; the array is not large 
enough to hold the string.  You allocated 16 characters, and 
filled them all with the string; there is no room for the 
terminating zero character.  Never forget to allocate the extra 
space for this.

More comments interspersed below.

On 14 Mar 99 at 19:27, Ron Higgins wrote:

> my mind is going along the lines :     ptr1=ar1;
>                                                          while (ptr1
> !=0(???))

You were told to `Use a separate integer variable to count the 
characters to print'.  If you want to do it in the way that you 
have, you need to dereference since you actually want to look 
at the char pointed at, not the pointer itself.  `while (*ptr1 
!= 0)', or just `while (*ptr1)'.

>                                                 b) ptr1=ar1[15]

Again you've got confused between the pointer and what it 
points at.  Here you try to assign a char value (the 15th 
character) to a pointer.  You want either:

    ptr1 = &ar1[15];

using the address operator to get the address of that object 
rather than its value, or more simply (and equivalently):

    ptr1 = ar1 + 15;

which works because in this context `ar1' becomes a pointer to 
char.

>                                                     while (ptr1
> !=something???)

    while (ptr1 >= ar1)

... but you were told to use an integer counter again.

> I don't know whether I am miles away or close to solving the problem

You're pretty close.  I don't think this list/group is the 
right place for this question though; it is not specific to 
djgpp at all.

-- 
George

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019