pointer and struct

This is a discussion on pointer and struct within the C Programming forums, part of the General Programming Boards category; hi all i'm learning the use of pointers within struct definitions. currently following the instructions here: http://www.le.ac.uk/cc/tutorials/c/ccccstrc.html my code below ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    18

    Question pointer and struct

    hi all

    i'm learning the use of pointers within struct definitions. currently following the instructions here:
    http://www.le.ac.uk/cc/tutorials/c/ccccstrc.html

    my code below produces:
    syntax error before "struct" on line 15 (maked)

    can somebody please explain why?

    thanks a lot..
    Code:
    main() {
    
    
            struct p {
    
            int data;
            int voo, loo, soo;
            struct p *moo;
    
            }*koo;
    
    >>15>>  koo=(*struct p) malloc(sizeof(*koo));
    
            koo->data=333;
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    TRY koo= (struct p *) malloc(sizeof(p));

    i may be wrong

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    18
    Quote Originally Posted by misplaced
    TRY koo= (struct p *) malloc(sizeof(p));

    i may be wrong
    thanks... that worked. i notoced that the last update on the page i'm following instructions from is from 3 and a half years ago. has something in the annotation of the type casting changed or it's just a typing error on that page? how come the * is after the struct name in the casting?

    thanks...

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    I suggest changing tutorial.
    I recommend:
    http://faq.cprogramming.com/cgi-bin/...&id=1073086407

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    That is one sorry looking tutorial you have there.
    So many faults, and I've only skimmed one page of it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    casting 'malloc()' is not needed in a C code. It works fine for me in Dev C++ under C projects.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    18
    thanks.... so far so good...

    another question. here is a piece of code:
    Code:
    main() {
    
    
    typedef struct p {
    
            int data;
            int voo, soo;
            struct p *moo;
    
            }koo;
    
            koo *loo;
            loo->data=333;
            loo->voo=loo->soo=999;
    
           void print_it(koo *some_pointer) {
                    printf("the thing is %d\n",some_pointer->soo);
            }
    
            print_it(loo);
    //      print_it(&loo);
       //     printf("the int is %d\n",loo->soo);
    }
    here loo is pointing to the address of a variable of type koo, right?
    so how do i access the data variable of the *moo pointer in loo?
    does this make sense?

    also if i pass the loo to print_it by refernce - like print_it(&loo) - gcc says:
    warning: passing arg 1 of `print_it' from incompatible pointer type and it prinst a wrong number?
    according to the suggested tutorial that's how pointers to structures are passed to functions...

    i'm trying to figure out all about the struct weired stuff, so please bare with me...

    thanks....
    Last edited by toshog; 10-12-2004 at 05:00 AM.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Well since you have a nested function, I'm surprised that it compiles
    Also, since your pointers are uninitialised, I'm surprised that it runs.

    This also means that the answers you are printing could be bogus for reasons nobody can figure out.

    Start by posting actual code you compiled, not random snippets of stuff you think is important arranged in some order bearing no resemblance to the code you compiled.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    18

    Unhappy

    this was actually the whole file without the include statement. here it is again:
    Code:
    #include <stdio.h>
    
    main() {
    
    
    typedef struct p {
    
            int data;
            int voo, soo;
            struct p *moo;
    
            }koo;
    
            koo *loo;
            loo->data=333;
            loo->voo=loo->soo=999;
    
            void print_it(koo *some_pointer) {
                    printf("the thing is %d\n",some_pointer->soo);
            }
    
    
            print_it(loo);
    
     //     printf("the int is %d\n",loo->soo);
    }
    i understand what you are saying about the nested function...
    this code actually compiles and runs and it works fine...

    isn't the line "koo *loo" initiate the loo pointer? declaring pointer to loo of type koo?

    thanks....

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sure, it declares a pointer. But what is your pointer pointing at? Try allocating some memory for the actual structure instance before you try and use your pointer. And no, it doesn't "work[s] fine", it "lucks out".

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Heh - so many problems
    Code:
    gcc -W -Wall -ansi -pedantic -O2  hello.c
    hello.c:3: warning: return type defaults to `int'
    hello.c: In function `main':
    hello.c:18: warning: ISO C forbids nested functions
    hello.c:20: warning: ISO C89 forbids mixed declarations and code
    hello.c:26: warning: control reaches end of non-void function
    hello.c:14: warning: `loo' might be used uninitialized in this function
    Start beefing up your compiler options - they can tell you quite a lot about the problems you have.

    Try this
    Code:
    #include <stdio.h>
    
    typedef struct p {
        int data;
        int voo, soo;
        struct p *moo;
    } koo;
    
    void print_it(koo * some_pointer)
    {
        printf("the thing is %d\n", some_pointer->soo);
    }
    
    int main()
    {
        koo bar;
        koo *loo = &bar;
        loo->data = 333;
        loo->voo = loo->soo = 999;
        print_it(loo);
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Oct 2004
    Posts
    18
    thank you. i appreciate the corrections. one last thing:
    why are these declarations needed:
    Code:
        koo bar;
        koo *loo = &bar;
    sorry about the stupid question... just trying to clear the fog... thank you.

  13. #13
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Quote Originally Posted by toshog
    thank you. i appreciate the corrections. one last thing:
    why are these declarations needed:
    Code:
        koo bar;
        koo *loo = &bar;
    sorry about the stupid question... just trying to clear the fog... thank you.
    Because in first line you declare a variable of type koo and in the second line you decalre a pointer variable named loo. Because loo is a pointer variable it needs to point to something which means that the value of loo should be address of variable bar.
    So the second line initialize pointer to type koo with addressof variable bar.

  14. #14
    Registered User
    Join Date
    Oct 2004
    Posts
    18
    Quote Originally Posted by Micko
    Because in first line you declare a variable of type koo and in the second line you decalre a pointer variable named loo. Because loo is a pointer variable it needs to point to something which means that the value of loo should be address of variable bar.
    So the second line initialize pointer to type koo with addressof variable bar.
    makes sense. i think i understand now... thank you.

  15. #15
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    lol........i love it...forget the inline keyword.....just go ahead and put your functions inline

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21