Groovy hepcat Nikola was jivin' on Mon, 14 Jun 2004 19:11:24 +0200 in
comp.lang.c.
Re: Where's the mistake???'s a cool scene! Dig it!
ok I rewrote the code, no mistakes but it still doesn't work ...
Well, that was a precise and to the point description of the
problem! You wanna narrow it down, just a tad? What do you mean by "it
still doesn't work"? Does it run and display incorrect output? Does it
run and display no output? Does it not run at all? Does it crash at a
certain point? Does it run indefinitely? Does it not compile? And if
not, what are errors reported by the compiler? Help us to help you by
being specific.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct lista{
int element;
struct lista *next;
}*pocetak1,*pocetak2;
Global variables are bad, unless you have a good reason to use them.
But in a program that occupies only one function, they are utterly
pointless. Put these variable definitions in main().
This will be rejected by any C99 conforming compiler. Implicit int
return type was removed from the standard nearly five years ago. You
must, now, specify the return type. (In main()'s case this is int, of
course. I see by the return statement you know that already, but it
bears spelling out for any newbies who may be reading this.) It was
always a good idea to specify the return type of all functions anyhow.
It is also, and has always been, a good idea to supply a prototype for
every function. As I hope you are aware, a prototype is a function
declaration that specifies the number and types of the parameters.
Even a function that takes no arguments can benefit from a prototype.
So, provide a prototype and specify a return type like so (remembering
that a function definition is also a declaration and can, therefore,
provide a prototype):
int main(void)
{
struct lista *q, *nova;
int i;
pocetak1=NULL;
This line, though not an error, is pointless, since pocetak1 is
defined at file scope, and, therefore, is already implicitly
initialised to a null pointer value. (I notice you didn't do the same
for pocetak2 though.)
srand(time(NULL));
for(i=0;i<5;i++)
{
q=(struct lista*) malloc(sizeof(struct lista));
Don't cast malloc()'s return. You have been told this before. Do
CHECK malloc()'s return, in case of an allocation error. Also, use the
thing being allocated, not the type, as the operand of sizeof. Ie.,
use sizeof *q, not sizeof(struct lista). The reasons for this have
been expounded time and time again here, so I won't repeat them.
It is your resposibility to find out these things before you post.
You should have read at least a month worth of newsgroup posts first.
And you should have read the FAQ
(
http://www.eskimo.com/~scs/C-faq/top.html) second.
This isn't the best way to get a random number within a certain
range. But it'll do for now, and I'll leave it to you to consult the
FAQ on that.
q->next=pocetak1;
pocetak1=q;
}
q=pocetak1;
printf("Random numbers:\n");
while(q!=NULL)
{
printf("\n%d",q->element);
q=q->next;
}
printf("\nOther way round:\n");
q=pocetak1;
for(i=0;i<5;i++)
{
nova=(struct lista*) malloc(sizeof(struct lista));
See above.
nova->element=(q+(5-i)*sizeof(struct lista))->element;
Huh? What is that supposed to achieve? You just entered undefined
behaviour territory. A linked list is not an array. You need to follow
the links to traverse the list.
q=q->next;
nova=nova->next;
nova->next has not been given a value before this, so you again
invoke undefined behaviour.
nova->next=pocetak2;
pocetak2=nova;
}
nova=pocetak2;
while(nova!=NULL)
{
printf("\n%d",nova->element);
nova=nova->next;
}
system("pause");
What is this system specific dreck supposed to be in aid of?
I have tried to point out all the problems and potential problems in
your code, but I may have missed something. The rest is up to you.
--
Dig the even newer still, yet more improved, sig!
http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?