Solved

Problem about variables

Posted on 1998-09-10
4
186 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
In desperate need of help 8 137
Passing command line arguments in C 10 91
convert char array to number in c 5 85
nested if statement in excel help 4 27
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures 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.

777 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