Where's the mistake???

N

Nikola

compiler says: function undeclared how come???
help!
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

struct lista{
int element;
struct lista *next;
}*pocetak;

main()
{
struct lista *q, *nova;
int i;
pocetak=NULL;
srand(time(NULL));
for(i=0;i<5;i++)
{
q=(struct lista*) mallloc(sizeof(struct lista));
q->element=rand()%100;
pocetak=q;
q->next=NULL;
}
q=pocetak;
printf("Nasumicni brojevi:\n");
while(q!=NULL)
{
printf("\n%d",q->element);
q=q->next;
}
printf("\nNaopako:\n");

return 0;
}
 
J

Joona I Palaste

Nikola said:
compiler says: function undeclared how come???
help!
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
q=(struct lista*) mallloc(sizeof(struct lista));

You'll want to have a second look at this line here.

(Besides, you don't need the cast.)
 
C

Case

Nikola said:
compiler says: function undeclared how come???

You ask a simple/silly question and don't even supply
us what funtion the compiler complains about. Be more
specific next time.
help!
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

struct lista{
int element;
struct lista *next;
}*pocetak;

main()
{
struct lista *q, *nova;
int i;
pocetak=NULL;
srand(time(NULL));
for(i=0;i<5;i++)
{
q=(struct lista*) mallloc(sizeof(struct lista));

mallloc() -> malloc()
 
N

Nikola

here's the full code :

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

struct lista{
int element;
struct lista *next;
}*pocetak;

main()
{
struct lista *q, *nova;
int i;
pocetak=NULL;
srand(time(NULL));
for(i=0;i<5;i++)
{
q=(struct lista*) mallloc(sizeof(struct lista));
q->element=rand()%100;
pocetak=q;
q->next=NULL;
}
q=pocetak;
printf("Random numbers:\n");
while(q!=NULL)
{
printf("\n%d",q->element);
q=q->next;
}

q=pocetak;
for(i=0;i<5;i++)
{
nova=(struct lista*) mallloc(sizeof(struct lista));
nova->element=*(pocetak+(5-i)*(sizeof(struct lista)));
pocetak=nova;
nova->next=NULL;
}
nova=pocetak;
printf("\nOther way round:\n");
while(nova!=NULL)
{
printf("\n%d",nova->element);
nova=nova->next;
}
system("pause");
return 0;
}
 
J

Joona I Palaste

Nikola said:
here's the full code :

(snip)

q=(struct lista*) mallloc(sizeof(struct lista));
(snip)

nova=(struct lista*) mallloc(sizeof(struct lista));

It's even consistent and everything. Do you have a C textbook? If so,
please re-read the chapter about memory alllocation, sorry, I mean
allocation.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The large yellow ships hung in the sky in exactly the same way that bricks
don't."
- Douglas Adams
 
B

Ben Pfaff

Christopher Benson-Manica said:
Heh, been there, done that several times myself, unfortunately.

Don't you enable suitable warnings? This class of mistake should
be pointed out by the compiler. But even if not, then the linker
message should make the error obvious.
 
C

Case

Nikola said:
I took a wild shot guessing dtat you have a compiler :)

It takes me time to copy-paste-compile. You're the one
that's asking for help. Be kind to the people you ask
help from and let me decide how to help you.

All the best,

Case
 
C

Case

Joona said:
It's even consistent and everything. Do you have a C textbook? If so,
please re-read the chapter about memory alllocation, sorry, I mean
allocation.

You made me laugh. Thanks!

Case
 
N

Nikola

I need to write a program that generates 5 random numbers and puts them into
a linked list. (Print the list) From that list it forms another list in a
way if the arrangement of elements in the first one was 5 8 3 1 in the new
one it should be
1 3 8 5 (print the new list)

where's the error now ??

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

struct lista{
int element;
struct lista *next;
}*pocetak;

main()
{
struct lista *q, *nova;
int i;
pocetak=NULL;
srand(time(NULL));
for(i=0;i<5;i++)
{
q=(struct lista*) malloc(sizeof(struct lista));
q->element=rand()%100;
pocetak=q;
q->next=NULL;
}
q=pocetak;
printf("Nasumicni brojevi:\n");
while(q!=NULL)
{
printf("\n%d",q->element);
q=q->next;
}

q=pocetak;
for(i=0;i<5;i++)
{
nova=(struct lista*) malloc(sizeof(struct lista));
nova->element=pocetak+(5-i)*(sizeof(struct lista));
pocetak=nova;
nova->next=NULL;
}
nova=pocetak;
printf("\nNaopako:\n");
while(nova!=NULL)
{
printf("\n%d",nova->element);
nova=nova->next;
}
system("pause");
return 0;
}
 
E

Eric Sosman

Nikola said:
I need to write a program that generates 5 random numbers and puts them into
a linked list. (Print the list) From that list it forms another list in a
way if the arrangement of elements in the first one was 5 8 3 1 in the new
one it should be
1 3 8 5 (print the new list)

where's the error now ??
[...]
q=(struct lista*) malloc(sizeof(struct lista));
q->element=rand()%100;
pocetak=q;
q->next=NULL;

Here.

An excellent way to teach yourself how to manipulate
links in data structures is to draw "before and after"
diagrams of the operation you want to perform. Study the
diagrams to see which links must change, and to what.
 
D

Default User

Nikola said:
I took a wild shot guessing dtat you have a compiler :)

For the most part, I don't go and run every piece of code posted here.
Most often, when the person asking the question gives us complete
information, the problem can be solved by inspection.

Had you pointed to the correct error line, we could have pointed out
that you mispelled the function name. No need to run it.




Brian Rodenborn
 
N

Nikola

q=(struct lista*) malloc(sizeof(struct lista));

ok I rewrote the code, no mistakes but it still doesn't work ...


#include<stdio.h>
#include<stdlib.h>
#include<time.h>

struct lista{
int element;
struct lista *next;
}*pocetak1,*pocetak2;

main()
{
struct lista *q, *nova;
int i;
pocetak1=NULL;
srand(time(NULL));
for(i=0;i<5;i++)
{
q=(struct lista*) malloc(sizeof(struct lista));
q->element=rand()%100;
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));
nova->element=(q+(5-i)*sizeof(struct lista))->element;
q=q->next;
nova=nova->next;
nova->next=pocetak2;
pocetak2=nova;
}
nova=pocetak2;
while(nova!=NULL)
{
printf("\n%d",nova->element);
nova=nova->next;
}
system("pause");
return 0;
}
 
J

Jens.Toerring

Nikola said:
ok I rewrote the code, no mistakes but it still doesn't work ...

struct lista{
int element;
struct lista *next;
}*pocetak1,*pocetak2;

There doesn't seem to be any good reason to make these variables
globals, so why don't you define them within main()?

Better make that

int main()

or

int main(void)

in order to be prepared for C99-compliant compilers that don't
allow functions without an explicit return type anymore.
{
struct lista *q, *nova;
int i;
pocetak1=NULL;
srand(time(NULL));
for(i=0;i<5;i++)
{
q=(struct lista*) malloc(sizeof(struct lista));

There isn't much of a good reason (well a few of the regulars here
disagree but...) for casting the return value of malloc()...
q->element=rand()%100;

I hope you're aware that this isn't a good way to restrict the range
of the random generator - the lower bits it returns usually have less
"randomness", so its preferable to use something like

q->element = ( int ) ( 100.0 * rand( ) / ( RAND_MAX + 1.0 ) );
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));
nova->element=(q+(5-i)*sizeof(struct lista))->element;

You seem to be assuming that the elements of the first linked list
somehow are located in memory in a densely packed fashion, one after
another, with the last allocated list element being the one at the
highest memory address. That's definitely not a valid assumption
and you will probably end up with pointing to memory you don't even
own. (And even if your assumption would be correct you would need
"(4 - i)" instead of "(5 - i)"...) A linked list is _not_ an array
and malloc() is perfectly within its rights when it gives you some
memory at a lower address, possibly several megabytes away, than
it returned in the previous call.
Regards, Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

program - help 5
Remving an element from Queue 37
How to fix this code? 1
Why this doesn't work 19
Queue in C 0
Where is my mistake? Why is s equal to minus infinity at some loop iterations? 0
Queue in C 25
Fibonacci 0

Members online

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top