www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/11/11/19:01:56

X-ROUTED: Thu, 11 Nov 1999 18:36:14 -0500
Message-ID: <382B520C.2AEC917E@greenbelt.com>
Date: Thu, 11 Nov 1999 18:32:28 -0500
From: Ed James <edjames AT greenbelt DOT com>
Organization: GIAC
X-Mailer: Mozilla 4.51 [en] (WinNT; I)
X-Accept-Language: en
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Re: Please help
References: <0FL100CMRYY7ZI AT cpt-proxy1 DOT mweb DOT co DOT za>
Reply-To: djgpp AT delorie DOT com

one problem, when you use 'char *string", you define a pointer to a character,
but you don't actually allocate any space for the string.  Try replacing it
with  'char string[80]' and see if that helps.
Actually, it's prolly better to use a name like 'InStr' or 'tempStr', etc
instead of 'string' which is a likely candidate for being a reserved word with
some languages.
Also, since string is now an array, in your scanf, you won't need the '&' now.

Marius Myburg wrote:

> Can anyone please tell me why this program doesn't want to work?  It
> compiler fine, but when I run it, after I have entered the requested text,
> it returns with an error at the strlength=strlen(string) line.  Also, when
> I remove that line, set string length to say 8 and enter a 8-character
> string, it returns with an error when I want to access the string's
> individual characters, at t1 = string[i].
>
> #include <conio.h>
> #include <string.h>
> #include <stdio.h>
> #include <stdlib.h>
>
> char *string;
> int strlength;
>
> void getstring()
> {
>    clrscr();
>    printf("%s", "Enter a string > ");
>    scanf("%s", &string);
>    printf("%s", &string);
>    strlength = strlen(string);         -- error here
> };
>
> int scanstring()
> {
>    char t1;  // temp char
>
>    for (int i=0; i<strlength; i++)
>    {
>       t1 = string[i];                       -- error here if first error
> bypassed
>       for (int j=i; j<strlength; j++)  // scan to end
>       {
>          if (string[j] == t1) // match!
>          {
>             printf("%s\n", "match !");
>          }
>       }
>    }
> }
>
> void main()
> {
>    getstring();
>    scanstring();
>    getch();
> }
>
> mariusm AT parys DOT lia DOT net



- Raw text -


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