Solved

Problem about variables

Posted on 1998-09-10
4
187 Views
Last Modified: 2012-05-04
I have written a program in TurboC 3.0.I am facing a serious problem.One of the variables automatically changes
during the execution of the program,which is not desirable.Can anybody tell me the solution?
0
Comment
Question by:SHAHZAD
4 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 1252617
Such an occurrence is almost always (99.9% of the time) caused by a logic problem in your code. To persue it here you should pinpoint exactly where/when the variable changes unexpectedly (which might by itself clarify the problem) and then post the code with an explanation.

0
 
LVL 2

Expert Comment

by:duneram
ID: 1252618
it could also be a problem of limits:
here is an example:

char buf[3]={{0}};;
short x = 0;
char buf2[20] = {{0}};
strcpy(buf,"Hello World");

at this point buf has been overwritten.  'x' will more than likely (depends on the compiler) contain the 'l' from Hello instead of being 0.  buf2 will have the rest of the string.

post the code
0
 
LVL 3

Accepted Solution

by:
elfie earned 20 total points
ID: 1252619
If you can debug your program, use a debugger to pinpoint the line of code which has the 'side effect'.

If you don't have a debugger, use the simple way of inserting printf lines in your code, and by a number of recompile/reruns
and always narrowing the piece of code between the printf your can also find the 'side-effect' line. (printf the variable that changing).

Then examing the variable modified in this piece of code, and look for things like 'dunaram' explained.
0
 

Author Comment

by:SHAHZAD
ID: 1252620
Thanks for your answer elfie.
I have pinpointed the instant when the variable changes using
the debugger.Now tell me how to stop that variable from changing.
That variable actually occurs in a loop.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question