Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A problem with Borland C++Builder for Microsoft Windows 2

Status
Not open for further replies.

Islamshahin

Technical User
Aug 26, 2007
2
EG
I used Borland turbo C++3 and Borland turbo C++ 3.1 (Under dos and under windows) and I didn't face this problem ever.

The problem is, when I use a very simple cosole project in Borlan Turbo C++6.0 Explorer and write this code:
/* Starting of the code*/
#include <stdio.h>

void main(void)
{
char *t;
printf("Input a string line: ");
scanf("%s",t);
printf("%s",t);
getch();
}
//End

the code works well, but if I identify any other pointer or variable in the same function like that:
/* Starting of the code*/
#include <stdio.h>
/*it works if I identify any other variable globaly*/
void main(void)
{
char *t,*y; /* adding any poiner or variable*/
printf("Input a string line: ");
scanf("%s",t);
printf("%s",t);
getch();
}
//End
this code can't excute (scanf) line, and gives me a window of (access Violation)which gives me two options (break or continue).

If I change the pointer identification with an array,it works well as well.

I think it's a very simple, but it stops me to use this version.
thanks
 
In your function (both versions), when you declare t and/or y, they are assigned random values, so that they both point to some section of memory which is probably being used by other variables or even program code. Then when you scan data into t, it overwrites the memory being pointed to.

You need to allocate a buffer before scanning the data:
[tt]
void main(void)
{
char t[100];
printf("Input a string line: ");
scanf("%s",t);
...
[/tt]
By the way, using scanf for storing user-input strings is not safe, as if the user enters more than 99 characters (the 100 allocated minus the null terminator), the buffer will overflow and overwrite other memory and possibly either cause bugs or a crash.
 
You can use width in %s format.
And don't forget that for char t[100] maximum string length is 99:
scanf("%99s",t);
 
thanks TonyGroves;

I already did what you said, But I didn't know the reason of that.

But, there's a gap,when I use the same code in Borland turboC++ 3.1, there won't be any problem.


to your note about using scanf for storing user-input strings, what do you suggest?

thanks;
///////////////////////////////////////

thanks in advance tsh73 :)
 
The behaviour of the code you posted is unpredictable, regardless of what compiler you use. It may seem to work with a certain compiler but that doesn't mean you can rely on it.

Regarding scanf, tsh73 seems to be right. However, it seems that using "%s" or "%99s" will only scan until the first whitespace character in the string, which is probably not what you want. You could instead use:
[tt]
fgets(t,99,stdin);
[/tt]
There are also more powerful solutions using the C++ standard library, but that is a whole new topic.
 
Islamshahin,
>>But I didn't know the reason of that.
ypu have to allocate memory before you read to it with scanf.
Or you just writing at some random place at memory, may be over-writing critical data (part of DOS ;)) )
Windows does not allow for such things (or at least not supposed to). So you get Violation if you write to memory you did not allocate firts.

Using "%99s" will read up to 99 chars and just skip the rest. So buffer overflow will not be possible.
But as TonyGroves said, scanf stops reading at first space - so you'll get only first word of the line. If you need whole line, use fgets.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top