Thread: instances of globally declared structures unavailable from within main() function

  1. #1
    Living to Learn
    Join Date
    Feb 2006
    Location
    Aberdeen, UK
    Posts
    33

    instances of globally declared structures unavailable from within main() function

    Hello.

    I was practicing working with structures and wrote out this program:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct tree {
           int size, pos;
           };
           
    int main()
    {
        tree leaf;
        tree *ptr;
        malloc(sizeof *ptr);
        // asterisk indicates get value of
        leaf.size = 1024;
        leaf.pos = 1;
        ptr = &leaf;
        printf("%d\n", ptr->size, "\n", ptr->pos);
        free(ptr);
        getchar();
    }
    But the compiler complains about 'Undeclared first use in function main()", when I'm trying to create instances of the tree structure.

    I've gone back over my notes, and can't find what's wrong, in fact the declaration for the leaf instance was taken right from the tutorial.

    Am I missing something? I'd be grateful if someone could point me in the right direction for this.

    Thanks.

    Hussein.

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Try changing all your tree declarations for struct tree declarations.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    you're freeing ptr, when ptr has no memory allocated to it. Just adding malloc(sizeof *ptr)
    doesn't allocate memory to ptr. by the looks of your program though, neither are needed.
    the malloc line and the free line can be taken out.

    and I doubt that printf is going to do what your expecting it to

  4. #4
    Living to Learn
    Join Date
    Feb 2006
    Location
    Aberdeen, UK
    Posts
    33
    Thanks. That worked great.

    BTW, that means there's also a mistype in the cprogramming.com tutorial on structures. They don't put the struct keyword in front of the instance.

    Thanks again. I'm surprised I didn't think of that myself, lol.

    Hussein.

  5. #5
    Living to Learn
    Join Date
    Feb 2006
    Location
    Aberdeen, UK
    Posts
    33
    Hi.

    True, the printf doesn't print the pos variable. Also, why won't malloc allocate memory for ptr? I read that in these cases it would allocate memory to the largest item in the structure. But as there are two ints here, I would expect it to allocate memory for an int?

    Hussein.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    let me rephrase, malloc is allocating memory, but your not assigning the memory to anything.
    malloc returns a pointer to the allocated memory

  7. #7
    Living to Learn
    Join Date
    Feb 2006
    Location
    Aberdeen, UK
    Posts
    33
    OK, also, why does the program wait for user input after printing the size variable (1024)? There are no input functions in the program.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    sure there is, getchar()

  9. #9
    Living to Learn
    Join Date
    Feb 2006
    Location
    Aberdeen, UK
    Posts
    33
    OK I've gotten the printf to work. Still don't understand the malloc issue. The explanation I'm working from is from the cprogramming lesson on Structures.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct tree {
           int size, pos;
           };
           
    int main()
    {
        struct tree leaf;
        struct tree *ptr;
        malloc(sizeof *ptr);
        // asterisk indicates get value of
        leaf.size = 1024;
        leaf.pos = 01;
        ptr = &leaf;
        printf("%d\n", ptr->size);
        printf("%d", ptr->pos);
        free(ptr);
        getchar();
    }

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    malloc(N): allocates N bytes and returns a pointer to the allocated memory

    ex.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct tree {
           int size, pos;
    };
    
    int main()
    {
        struct tree leaf;
        struct tree *ptr;
    
        ptr = malloc(sizeof(struct tree));
        ptr->size = 10;
        ptr->pos = 2;
    
        leaf.size = 1024;
        leaf.pos = 1;
    
        printf("LEAF: %d, %d\n", leaf.size, leaf.pos);
        printf("PTR: %d, %d\n", ptr->size, ptr->pos);
    
        free(ptr);
        return 0;
    }
    now you would have 2 tree structures, leaf and the one pointed to by ptr

  11. #11
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by sl4nted
    let me rephrase, malloc is allocating memory, but your not assigning the memory to anything.
    malloc returns a pointer to the allocated memory
    Quote Originally Posted by hpteenagewizkid
    OK I've gotten the printf to work. Still don't understand the malloc issue. The explanation I'm working from is from the cprogramming lesson on Structures.
    read sl4nted's comment as
    Code:
    struct tree *ptr =  malloc(sizeof( struct tree));
    Kurt

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    BTW, that means there's also a mistype in the cprogramming.com tutorial on structures. They don't put the struct keyword in front of the instance.
    You don't need to if you have a typedef or if you're using C++.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Correct, but he's right in saying there's a mistake. In the last example of the tutorial, they don't typedef either.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    No, they just copied it directly from the C++ tutorial.

    In case anyone's hopelessly confused by now, here's the tutorial in question: http://www.cprogramming.com/tutorial/c/lesson7.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by Happy_Reaper
    Correct, but he's right in saying there's a mistake. In the last example of the tutorial, they don't typedef either.
    here is a simple example which is in teststruct.cpp file

    Code:
    #include<stdio.h>
    
    struct test
    {
        int data;
    };
    
    int main()
    {
        test test1;
        
        test1.data = 10;
        
        printf("%d",test1.data);
        
        getchar();
        return 0;
    }
    this code should work fine with the .cpp extension with no error.

    with .c extension this what u will get

    Code:
    10 teststruct.c `test' undeclared (first use in this function)
    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. variables when declared inside or outside a function
    By jas_atwal in forum C Programming
    Replies: 6
    Last Post: 12-14-2007, 02:42 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM