Where's the mistake???

Discussion in 'C Programming' started by Nikola, Jun 14, 2004.

  1. Nikola

    Nikola Guest

    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;
    }
     
    Nikola, Jun 14, 2004
    #1
    1. Advertising

  2. Nikola <> 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.)

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "War! Huh! Good God, y'all! What is it good for? We asked Mayor Quimby."
    - Kent Brockman
     
    Joona I Palaste, Jun 14, 2004
    #2
    1. Advertising

  3. Nikola

    Case Guest

    Nikola wrote:
    > 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()

    > 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;
    > }
    >
    >
     
    Case, Jun 14, 2004
    #3
  4. Joona I Palaste <> spoke thus:

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


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


    Heh, been there, done that several times myself, unfortunately.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Jun 14, 2004
    #4
  5. Nikola

    Nikola Guest

    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;
    }
     
    Nikola, Jun 14, 2004
    #5
  6. Nikola

    Nikola Guest

    I took a wild shot guessing dtat you have a compiler :)
     
    Nikola, Jun 14, 2004
    #6
  7. Nikola

    Nikola Guest


    > Heh, been there, done that several times myself, unfortunately.


    I don't see it ... :-(
     
    Nikola, Jun 14, 2004
    #7
  8. Nikola <> scribbled the following:
    > here's the full code :


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


    (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 () ------------- 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
     
    Joona I Palaste, Jun 14, 2004
    #8
  9. Nikola

    Ben Pfaff Guest

    Christopher Benson-Manica <> writes:

    > Joona I Palaste <> spoke thus:
    >
    >>> q=(struct lista*) mallloc(sizeof(struct lista));

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

    >
    > 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.
    --
    Ben Pfaff
    email:
    web: http://benpfaff.org
     
    Ben Pfaff, Jun 14, 2004
    #9
  10. Nikola

    Case Guest

    Nikola wrote:
    > 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
     
    Case, Jun 14, 2004
    #10
  11. Nikola

    Case Guest

    Joona I Palaste wrote:
    > Nikola <> scribbled the following:
    >
    >>here's the full code :

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

    >
    >
    > (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.
    >


    You made me laugh. Thanks!

    Case
     
    Case, Jun 14, 2004
    #11
  12. One of your mistakes is in the following line:

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


    - Dario (the compiler and/or linker already told that?)
     
    =?UTF-8?B?IkRhcmlvIChkcmlua2luZyBjb++sgGVlIGluIHRo, Jun 14, 2004
    #12
  13. Nikola

    Nikola Guest

    Oops, I guess my finger kinda stuck ...
    sorry for that ...

    all well ...
     
    Nikola, Jun 14, 2004
    #13
  14. Nikola

    Nikola Guest

    .... and thanks
     
    Nikola, Jun 14, 2004
    #14
  15. Nikola

    Nikola Guest

    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;
    }
     
    Nikola, Jun 14, 2004
    #15
  16. Nikola wrote:

    > compiler says: function undeclared how come???


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

    ^^^^^^^
    spelling error
     
    Martin Ambuhl, Jun 14, 2004
    #16
  17. Nikola

    Eric Sosman Guest

    Nikola 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 ??
    > [...]
    > 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.

    --
     
    Eric Sosman, Jun 14, 2004
    #17
  18. Nikola

    Default User Guest

    Nikola wrote:
    >
    > 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
     
    Default User, Jun 14, 2004
    #18
  19. Nikola

    Nikola Guest

    > > q=(struct lista*) malloc(sizeof(struct lista));
    > > q->element=rand()%100;
    > > pocetak=q;
    > > q->next=NULL;

    >
    > Here.


    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;
    }
     
    Nikola, Jun 14, 2004
    #19
  20. Nikola

    -berlin.de Guest

    Nikola <> wrote:
    > 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;


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

    > 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
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Jun 14, 2004
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. \(beta-\) Frank Nitzsche

    where is the mistake?

    \(beta-\) Frank Nitzsche, Jun 25, 2004, in forum: VHDL
    Replies:
    4
    Views:
    554
  2. Jesse

    Beginner Mistake

    Jesse, Jul 22, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    376
    Jesse
    Jul 22, 2003
  3. Joerg Jooss
    Replies:
    1
    Views:
    418
    DotNetJunkies User
    May 22, 2004
  4. John
    Replies:
    6
    Views:
    673
  5. Leon

    Does anyone see my mistake?

    Leon, Nov 10, 2004, in forum: ASP .Net
    Replies:
    9
    Views:
    346
Loading...

Share This Page