473,779 Members | 2,047 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Where's the mistake???

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("Nasumic ni brojevi:\n");
while(q!=NULL)
{
printf("\n%d",q->element);
q=q->next;
}
printf("\nNaopa ko:\n");

return 0;
}
Nov 14 '05
25 1532
In article <ca**********@b agan.srce.hr>, Nikola <az*****@inet.h r> wrote:
compiler says: function undeclared how come???
Hint: spelling counts.
Hint: There is no standard C function called malllllllllllll lllllllllllllll lllllllllllllll lllllllllllllll lllllllllllllll lllllllllllllll lloc().
Hint: don't cast the return value of malloc().

Gordon L. Burditthelp!
#include<stdio .h>
#include<stdli b.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("Nasumic ni brojevi:\n");
while(q!=NULL)
{
printf("\n%d",q->element);
q=q->next;
}
printf("\nNaopa ko:\n");

return 0;
}

Nov 14 '05 #21
Ben Pfaff <bl*@cs.stanfor d.edu> spoke thus:
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.


I've had the misfortune of making this error in such a way that the
compiler was not obliged to complain (it was an "errror" in a strstr
test where I had intended the correctly-spelled version).

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #22
On Mon, 14 Jun 2004 18:26:26 +0200, "Nikola" <az*****@inet.h r> wrote:
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 ??
Which error do you mean? You have a syntax error. You have many
logic errors. You don't check to see if malloc succeeded. You create
memory leaks. You have global variables that don't need to be. You
cast the return from malloc. You use non-standard features.

The answer to your question is: there is more than one and they are
everywhere.

#include<stdio .h>
#include<stdli b.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(s truct lista));
You should check that malloc succeeded.
q->element=rand() %100;
pocetak=q;
q->next=NULL;
}
While this loop does allocate 5 different blocks of memory, you throw
the addresses of the first four away. This creates memory leaks.
q=pocetak;
printf("Nasumic ni brojevi:\n");
while(q!=NULL)
This loop is guaranteed to execute at most one time.
{
printf("\n%d",q->element);
q=q->next;
}

q=pocetak;
for(i=0;i<5;i++ )
{
nova=(struct lista*) malloc(sizeof(s truct lista));
nova->element=poceta k+(5-i)*(sizeof(stru ct lista));
This is a syntax error. element is an int. pocetak is a pointer.
The expression involves pointer arithmetic which will evaluate to a
pointer. You cannot assign a pointer to an int this way.
pocetak=nova;
nova->next=NULL;
}
This loop has the exact same memory leak problem.
nova=pocetak;
printf("\nNaopa ko:\n");
while(nova!=NUL L)
This loop also executes at most once.
{
printf("\n%d",n ova->element);
nova=nova->next;
}
system("pause") ;
This is not particularly portable. Many use a getchar to wait for a
key press.
return 0;
}


<<Remove the del for email>>
Nov 14 '05 #23
On 14 Jun 2004 15:55:53 GMT, Joona I Palaste <pa*****@cc.hel sinki.fi>
wrote in comp.lang.c:
Nikola <az*****@inet.h r> scribbled the following:
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.)


Actually he might need the cast, depending on what mallloc() returns.
;)

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #24
kal
"Nikola" <az*****@inet.h r> wrote in message news:<ca******* ***@bagan.srce. hr>...

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

Let us consider the first part. The objective is to allocate
a linked list of 5 structure instances. Schematically the first
two are as follows where "v" is the value you store (element)
and "p" is the pointer to the next structure (next.)
_______ _______
| v | p | .-> | v | p | .->
------- | ------- |
|____| |____|

We deduce that (1) we should store the first or starting structure
address somewhere where we can get at it, this is the "pocetak"
pointer that is allocated golbally, and (2) we should have access
to the currently allocated structure and the PREVIOUS structure,
if any, so that we can store the current structure address in the
"next" item of the previous structure.

Let us now do the coding.
#include<stdio. h>
#include<stdlib .h>
#include<time.h >
ok.
struct lista{
int element;
struct lista *next;
}*pocetak;
"pocetak" stores the address of the very first structure.
main()
{
struct lista *q, *nova;
"q" will have the previous structure address, if any.
"nova" has the currently allocated structure address.
int i;
pocetak=NULL;
srand(time(NULL ));
"pocetak" is initialized with NULL to start with.
for(i=0;i<5;i++ )
{
q=(struct lista*) malloc(sizeof(s truct lista));
q->element=rand() %100;
pocetak=q;
q->next=NULL;
}
Here the LAST allocated structure address is stored in "pocetak."
But what is required is the FIRST allocated structure address.
Also, nothing is stored in the "next" item.

What is needed is something like the following.

for(i=0;i<5;i++ )
{
nova=(struct lista*) malloc(sizeof(s truct lista));
nova->element=rand() %100;
nova->next=NULL;
if (pocetak == NULL) /* or i==0 */
pocetak=nova;
else
q->next=nova; /* i>0 hence q will be valid */
q=nova; /* store the last address in q */
}
q=pocetak;
printf("Nasumic ni brojevi:\n");
while(q!=NULL)
{
printf("\n%d",q->element);
q=q->next;
}
Ok. Include the following for salutations after output.

printf("\n\nSay onara\n\n");
system("pause") ;
return 0;
}


Ok.
Nov 14 '05 #25
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<stdli b.h>
#include<time. h>

struct lista{
int element;
struct lista *next;
}*pocetak1,*poc etak2;
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().
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(s truct 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.
q->element=rand() %100;
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=pocetak 1;
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(s truct lista));
See above.
nova->element=(q+( 5-i)*sizeof(struc t 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=pocetak 2;
pocetak2=nova;
}
nova=pocetak2;
while(nova!=NUL L)
{
printf("\n%d",n ova->element);
nova=nova->next;
}
system("pause") ;
What is this system specific dreck supposed to be in aid of?
return 0;
}


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 "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Nov 14 '05 #26

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

Similar topics

3
1732
by: Paul T. Rong | last post by:
Dear all, My aim is to compact and repair current database, I got the following code from http://www.mvps.org/access/general/gen0041.htm Option Compare Database ' ***** Code Start ***** Public Sub CompactDB()
15
2252
by: Paul T. RONG | last post by:
Hello, I am making a restaurant database (it is much more complicated than I thought before!), now it comes to the last stage and I come across a problem. I will explain it in detail. In a restaurant, though rarely, but it happens, that one guy from desk1 would like to pay for bills of both desk1 and desk2 (I avoid using Access term "table"), that means the dishes that ordered seperately before by desk1 and desk2 now have to be commixed...
23
1785
by: Red Dragon | last post by:
I am self study C program student. Hope someone can help me with this problem. This program generates random numbers over a user defined range using call function I used the call function " GenRndNum". The range is 2 and 10. The problem is that I get the same 2 random numbers generated over 2 calls. I should get 2 different random numbers. Can someone please point out my mistake? Thanks Khoon.
3
14939
by: bughunter | last post by:
IMHO, statements like this is mistake typically. May be more better made this construction - I said about empty WHERE - invalid? A lot of data will saved... :-) Andy
2
1151
by: Lad | last post by:
I use the following code to sort dictionary. Olddict={'r':4,'c':1,'d':2,'e':3,'f':2} Newdict={} i = i.sort() # by val i.reverse() # Get largest first. for (val, key) in i: print key,val
20
5246
by: Frank-O | last post by:
Hi , Recently I have been commited to the task of "translating" some complex statistical algorithms from Matlab to C++. The goal is to be three times as fast as matlab ( the latest) . I've used various techniques ( loop unrolling, loop jamming...) and tried some matrix libraries : newmat (slow for large matrix) , STL (fast but ..not usefull) , hand coding (brain consuming...), and recently Meschach...
4
2184
by: Winston | last post by:
Where is the mistake? I want to make a simple menu. These are two pieces of two files... function ShowMenu(objeto) { is_open = document.getElementById(objeto).style.display; document.getElementById("menu_p").style.display='none'; if (is_open == 'none') { document.getElementById(objeto).style.display='block'; }
4
3571
by: | last post by:
I have learned about compartmentalizing my code base using Class Libraries. I have my common code such as my ORM framework broken out into their own Class Libraries, which are referenced as projects from my Website. I also have a common set of DLLs I use across all of my applications. It would be great if I could put all of those DLLs into their own projects, and to reference that project from various solutions. The way my solution is...
2
1217
by: Eglute | last post by:
Hello. I have a problem. I am a begginer in PHP. I wrote the code: <? $variable=5; $variable<10? {$ans="less"; echo $ans."<br>"; echo "variable=".$variable."<br>";}: {$ans="more"; echo $ans."<br>"; echo "variable =".$variable."<br>";} ?>
0
9632
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9925
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8958
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7478
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4036
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2867
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.