The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
No output showing in Dev C++ compiler..help please
Discuss No output showing in Dev C++ compiler..help please in the C Programming forum on Dev Shed. No output showing in Dev C++ compiler..help please C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 30th, 2012, 09:18 PM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 9
Time spent in forums: 1 h 13 m 54 sec
Reputation Power: 0
|
|
|
No output showing in Dev C++ compiler..help please
Hi All,
I'm entering a very basic code of appending nodes in a link list in ascending order. But the compiler shows a black screen as output.
Please Help. Here's the code...
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node{
int data;
struct node *link;
};
void addinasc(struct node **, int);
void display(struct node *);
void count(struct node *);
int main()
{
struct node *p;
p=NULL;
addinasc(&p , 10);
addinasc(&p , 7);
addinasc(&p , 74);
addinasc(&p , 91);
addinasc(&p , 56);
addinasc(&p , 24);
addinasc(&p , 35);
addinasc(&p , -5);
count(p);
display(p);
/*reverse(&p);
display(p);*/
getch();
return 0;
}
void addinasc(struct node **q, int num)
{
struct node *temp , *r;
temp = *q;
r=(struct node*)malloc(sizeof(struct node));
r->data = num;
if(*q==NULL || (*q)->data>num)
{
r->link = *q;
*q = r;
}
else
{
while(temp!=NULL)
if(temp->data<=num && (temp->link->data > num || temp->link == NULL))
{
r->link = temp->link;
temp->link = r;
return;
}
temp = temp->link;
}
}
void count(struct node *q)
{
int count=0;
while(q!=NULL)
{
count++;
q=q->link;
}
printf("\n\nNo. of elements in the linked list : %d\n" , count);
}
void display(struct node *q)
{
while(q!=NULL)
{
printf("\t%d", q->data);
q=q->link;
}
}
|

August 31st, 2012, 02:06 AM
|
|
Registered User
|
|
Join Date: Jun 2012
Posts: 25
Time spent in forums: 8 h 53 m 34 sec
Reputation Power: 0
|
|
|
Had you given the input for the function, to display the link list ? check for that
|

August 31st, 2012, 09:44 AM
|
 |
Contributing User
|
|
|
|
You've discovered the infinite loop!
I amended your addinasc function to display a message every time it's called:
Code:
void addinasc(struct node **q, int num) {
struct node *temp , *r;
puts("addinasc"); /*THIS LINE INSERTED **************/
temp = *q;
And when I ran the program I got this output, which I had to kill because it runs forever:
$ a=./c && make -k $a && $a
make: `c' is up to date.
addinasc
addinasc
addinasc
I'd say that
addinasc(&p , 74);
failed. Look there.
__________________
[code] Code tags[/code] are essential for python code!
|

September 1st, 2012, 02:13 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 9
Time spent in forums: 1 h 13 m 54 sec
Reputation Power: 0
|
|
|
Corrected Code
Again i forgot to enclose the code within while loop in the { }.
Anyways, I have done that now and I am posting the code again.
Please help . Cant understand why no output is showing.
Again, I'm using Dev C++ IDE.
Code:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node{
int data;
struct node *link;
};
void addinasc(struct node **, int);
void display(struct node *);
void count(struct node *);
int main()
{
struct node *p;
p=NULL;
addinasc(&p,10);
addinasc(&p,7);
addinasc(&p,74);
addinasc(&p,91);
addinasc(&p,56);
addinasc(&p,24);
addinasc(&p,35);
addinasc(&p,-5);
count(p);
display(p);
/*reverse(&p);
display(p);*/
getch();
return 0;
}
void addinasc(struct node **q, int num)
{
struct node *temp , *r;
temp = *q;
r=(struct node*)malloc(sizeof(struct node));
r->data = num;
if(*q==NULL || (*q)->data>num)
{
r->link = *q;
*q = r;
}
else
{
while(temp!=NULL)
{
if(temp->data<=num && (temp->link->data > num || temp->link == NULL))
{
r->link = temp->link;
temp->link = r;
return;
}
temp = temp->link;
}
}
}
void count(struct node *q)
{
int count=0;
while(q!=NULL)
{
count++;
q=q->link;
}
printf("\n\nNo. of elements in the linked list : %d\n" , count);
}
void display(struct node *q)
{
while(q!=NULL)
{
printf("\t%d", q->data);
q=q->link;
}
}
|

September 1st, 2012, 02:57 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Er, b49P23TIvg informed you that you were trapped within an infinite loop. What do you not understand about infinite loop?
In my undergraduate days, we were on an IBM/370 system that emulated the IBM/360 OS. The vast majority of error messages we received told us almost absolutely nothing useful ("Probable Cause: Programmer error." "Corrective Action: Correct the programming error and resubmit". Except for the ones that told us that our time allocation had timed out, which told us that we had been trapped within an infinite loop.
An infinite loop is one in which there is no escape. For example, the code
would be an infinite loop. You would be forever trapped within the loop, unless some kind of condition allowed you to jump out of it (eg, something to trigger a break). As long as you're trapped within an infinite loop, there is no way out and you will remain there forever. Which means that any code
that lies after that infinite loop will never ever execute.
Again, b49P23TIvg demonstrated that your code, addinasc in particular, is trapped within an infinite loop. You need to correct that.
|

September 1st, 2012, 03:07 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 9
Time spent in forums: 1 h 13 m 54 sec
Reputation Power: 0
|
|
|
I do know what an infinite loop is.
But what i'm not able to fathom is how and where i have entered that infinite loop. Infact i've cross checked the code from a book also and it's also pretty similar.
Please post a solution to the problem(probably some corrected lines of code) if possible. Would help a lot
|

September 1st, 2012, 03:12 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
I would suggest that the simplest way to find out what is happening is to run and step the code in teh debugger. However Dev-C++'s debugger sucks to the point of uselessness, so I strongly suggest that you abandon it - it has not been maintained since 2005 and is distributed with an equally old version of MinGW/GCC. Use VC++ 2010 Express Edition instead. Free, more fully featured, and has a best-in-class debugger.
Your code compiles without complaint in VC++ 2010 with one minor change; replace getch() with _getch().
When run it crashes, but the debugger helpfully stops and shows you exactly where it has crashed. At this line
Code:
if(temp->data<=num && (temp->link->data > num || temp->link == NULL))
in addinasc().. At the call addinasc(&p, 74), the value of temp->link at the point it crashes is NULL - you are dereferencing a NULL pointer. You need to figure out either why it is NULL if it shouldn't be or if NULL is acceptable, you need to add a check for it before attempting to dereference it.
Really - use a tool with a working debugger and use it!. The analysis above took me no longer than the time it took to compile and run the code. Debugging by forum is impractical and a waste of everyone's time including yours. I only took the time here to illustrate how effective a debugger can be in the hope that you would see the light.
Last edited by clifford : September 1st, 2012 at 03:14 AM.
|

September 1st, 2012, 03:21 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
Looking at that line again, I see that you do check for temp->link == NULL, but only after you have attempted to inspect temp->link->data, which will fail if link is NULL.
If you swap the test as follows:
Code:
if( temp->data <= num && (temp->link == NULL || temp->link->data > num ) )
The short-circuit evaluation of || will prevent the dereferencing of the NULL pointer. That is to say when temp->link == NULL is true, the evaluation of || stops without evaluating the right-hand side expression, because the left-hand side is already true therefore the whole expression is known to be true already.
Last edited by clifford : September 1st, 2012 at 03:24 AM.
|

September 1st, 2012, 03:29 AM
|
|
Registered User
|
|
Join Date: Aug 2012
Posts: 9
Time spent in forums: 1 h 13 m 54 sec
Reputation Power: 0
|
|
|
Problem Solved
Thanks a lot Clifford..
The problem is resolved and I got to learn a new thing.
Thanks so much 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|