473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

while loop and printf

I have found a problem while using a while statement and a linked list.
I had never met this matter before....and I am wondering that if you
have , please tell me what it is wrong.

I am traversing the linked list using a pointer to cycle through.

int Function(...){

node * p;

p=linkedlist; // p is a pointer to the list first node .

while (p!=NULL){

if(....){

// do something with p

}

printf("\nIf I place a printf here , the while loop woks out;
it doesn't if not);
p=p->siguiente;

}

}

The matter is that if the printf statement is removed, the loop break
and send a NULL exception message.

The printf can be placed after advancing linked list, it can be the
first statement of the loop.... even it can be before this function is
called , and all works right.
But not without it.

Any idea?

Thanks a lot, SONIA

Nov 15 '05 #1
7 2984
ss***************@hotmail.com wrote:
The matter is that if the printf statement is removed, the loop break
and send a NULL exception message. What do you mean by a NULL exception message? There are no exceptions
in C. If you get an error message, please post it.
The printf can be placed after advancing linked list, it can be the
first statement of the loop.... even it can be before this function is
called , and all works right.
But not without it.

This doesnt' make much sense. Can you post the complete while loop?
Nov 15 '05 #2
ss***************@hotmail.com wrote:
I have found a problem while using a while statement and a linked list.
I had never met this matter before....and I am wondering that if you
have , please tell me what it is wrong.

I am traversing the linked list using a pointer to cycle through.

int Function(...){

node * p;

p=linkedlist; // p is a pointer to the list first node .

while (p!=NULL){

if(....){

// do something with p

}

printf("\nIf I place a printf here , the while loop woks out;
it doesn't if not);
p=p->siguiente;

}

}

The matter is that if the printf statement is removed, the loop break
and send a NULL exception message.

The printf can be placed after advancing linked list, it can be the
first statement of the loop.... even it can be before this function is
called , and all works right.
But not without it.

Any idea?

Thanks a lot, SONIA

Please do not post pseudo-code which cannot be compiled but rather
a minimal example.
How to get a minimal example: Start throwing out all functions you
do not need to reproduce the error or undesired/strange behaviour.
Then throw out all code in the functions you need until you cannot
remove any line, statement or whatever without destroying what you
want to show us.
By then, you probably will have found the problem -- if not, you
will have made it easy for us to help you.

Without that, we have to revert to crystal balls.

Some guesses:
- you might access a deallocated node because your cleanup at
deallocation does not work (i.e. the preceding node's link is
not set to NULL). The deallocated memory contains different
things depending on the printf() placement.
- you might use a pointer which is uninitialised and points
_somewhere_, maybe not even a valid storage location (or contains
a null pointer representation). In order to avoid that, initialise
each and every pointer to NULL at declaration.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #3
ss***************@hotmail.com wrote:
I have found a problem while using a while statement and a linked list.
I had never met this matter before....and I am wondering that if you
have , please tell me what it is wrong.

I am traversing the linked list using a pointer to cycle through.

int Function(...){

node * p;

p=linkedlist; // p is a pointer to the list first node .

while (p!=NULL){

if(....){

// do something with p

}

printf("\nIf I place a printf here , the while loop woks out;
it doesn't if not);
p=p->siguiente;

}

}

The matter is that if the printf statement is removed, the loop break
and send a NULL exception message.

The printf can be placed after advancing linked list, it can be the
first statement of the loop.... even it can be before this function is
called , and all works right.
But not without it.

Unfortunately, it's impossible to say what's going on here from what
you've posted. Please post the smallest compilable (appropriately
formatted) code snippet that exhibits this behavior; that will greatly
enhance your chances of getting the help you need.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 15 '05 #4
On 14 Jul 2005 08:56:16 -0700, ss***************@hotmail.com wrote:
I have found a problem while using a while statement and a linked list.
I had never met this matter before....and I am wondering that if you
have , please tell me what it is wrong.

I am traversing the linked list using a pointer to cycle through.

int Function(...){

node * p;

p=linkedlist; // p is a pointer to the list first node .

while (p!=NULL){

if(....){

// do something with p

}

printf("\nIf I place a printf here , the while loop woks out;
it doesn't if not);
p=p->siguiente;

}

}

The matter is that if the printf statement is removed, the loop break
and send a NULL exception message.

The printf can be placed after advancing linked list, it can be the
first statement of the loop.... even it can be before this function is
called , and all works right.
But not without it.


As others have indicated, we need more data. As a general rule of
thumb, if adding a printf statement like yours alters the behavior of
the program this drastically, you probably have some form of undefined
behavior, usually overrunning an array or an allocated area.
<<Remove the del for email>>
Nov 15 '05 #5
Barry Schwarz <sc******@deloz.net> writes:
[...]
As others have indicated, we need more data. As a general rule of
thumb, if adding a printf statement like yours alters the behavior of
the program this drastically, you probably have some form of undefined
behavior, usually overrunning an array or an allocated area.


Either that, or one of the arguments to printf() has a side effect
that alters the execution of the loop.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #6
Keith Thompson wrote:
Barry Schwarz <sc******@deloz.net> writes:
[...]
As others have indicated, we need more data. As a general rule of
thumb, if adding a printf statement like yours alters the behavior of
the program this drastically, you probably have some form of undefined
behavior, usually overrunning an array or an allocated area.


Either that, or one of the arguments to printf() has a side effect
that alters the execution of the loop.


Another possibility is that the statement before the printf()
ended with a comma instead of a semicolon, or perhaps it was
an if(), for() etc.

Nov 15 '05 #7

<ss***************@hotmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
I have found a problem while using a while statement and a linked list.
I had never met this matter before....and I am wondering that if you
have , please tell me what it is wrong.

I am traversing the linked list using a pointer to cycle through.

int Function(...){

node * p;

p=linkedlist; // p is a pointer to the list first node .

while (p!=NULL){

if(....){

// do something with p

}

printf("\nIf I place a printf here , the while loop woks out;
it doesn't if not); In the unlikely event that this is a verbatim copy of your code, note the
missing end quote. Also, I suggest as a rule you put the newline character
at the end of the line unless the logic of the function requires it the way
you have it.
p=p->siguiente;

}

}

The matter is that if the printf statement is removed, the loop break
and send a NULL exception message.

The printf can be placed after advancing linked list, it can be the
first statement of the loop.... even it can be before this function is
called , and all works right.
But not without it.

Any idea?

Thanks a lot, SONIA

Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Redduck | last post by:
Hello everyone. I am frustrated, I have written the simple program below for a class and I am having problems with the DO-WHILE loop. On the first run through the loop, everything works well, the...
9
by: JS | last post by:
#include <stdio.h> main(){ int c, i, nwhite, nother; int ndigit; nwhite = nother = 0; for (i = 0; i < 10; ++i)
3
by: rmijares | last post by:
I am sure you guys have seen this before. I am working on the famous Employee Payroll program for a class assignment. In any case, the while loop at the end of the Employee class just keeps looping;...
36
by: Scott | last post by:
Hi, Now this one does not make any sense to me at all. I have a while loop and it is not working anything like it is suppose to. I have placed it below. ------------------------- Count =...
10
by: rohitjogya | last post by:
Can anyone tell me the difference bet for loop and while loop execution? ____________________ for (i=0 ; i<10 ; i++) ; /* Do nothing*/ print i; ___________________ i=0;
1
by: somenath | last post by:
Hi All, I have doubt regarding how compiler understands about while loop. For example the bellow mentioned code produce the output as mentioned bellow. #include<stdio.h> int main(void) {
23
by: lisp9000 | last post by:
I wrote a small test program to read a file of data and print each line, but it's only printing the 2nd line out of 3 total lines. The test file, "foo.txt", has 3 lines: 7388: Zn->Z0 Run...
6
by: nickels | last post by:
Ok i already made a program that uses char and when someone enters a letter it will give them a conversion i made for that letter. That looks like this. import java.util.Scanner; public class...
4
by: jemccarthy13 | last post by:
I am a very very low level beginner programmer, trying to teach myself C, and I could use some help with the following program for guessing a random number. Please, when helping, try to use low...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.