Thread: structure pointer need more info

  1. #1
    Unregistered
    Guest

    structure pointer need more info

    O.K. guys I know this structure is declared in main but I don't understand why I can't send it's pointer to another function I knoe the functions need to know about it before they can use it put I thought the prototype did that, after all if this was a pointer to an int or char it would be done like this so why not with structures.



    #include<stdio.h>
    #include<conio.h>

    void details1(struct baby *kayleigh_ptr);

    int main()
    {

    struct baby{
    int age;
    char name[10];
    };


    struct baby kayleigh;
    struct baby *kayleigh_ptr=&kayleigh;


    details1(kayleigh_ptr);
    getch();

    return 0;
    }

    void detail1(struct baby *kayleigh_ptr)
    {
    kayleigh_ptr->age=10;
    }


    thanks leaner(wanting to be a master).

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    36
    declared the structure out of main, or u can't "see" it in details1.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    12

    Smile

    hi there,

    I'd modified your coding...I hope it is what you want.

    ***********************code**********************

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

    struct baby{
    int age;
    char name[10];
    };

    typedef struct baby *baby_ptr;


    void details1(baby_ptr);

    int main()
    {

    baby_ptr kayleigh_ptr;

    kayleigh_ptr = (baby_ptr) malloc(sizeof(struct baby));

    details1(kayleigh_ptr);

    return 0;
    }


    void details1(baby_ptr kayleigh_ptr)
    {

    kayleigh_ptr->age=10;

    printf ("%d\n", kayleigh_ptr->age);
    }


    **********************end****************

  4. #4
    Unregistered
    Guest
    When you are passing a pointer to a integer, character, or anything else that is a pre defined type in "C," the function that you are passing it too already "understands" what it is getting. When you define a structure and then try to pass it to a function, the function has to "know" what that structure looks like. If you define the structure inside of "main," it's definition is local in scope to main... main "knows" the structure and can use it, but any function outside of main will not be able to see it (just like if you define a variable inside of main). So, by moving the structure definition outside of main (to the top of the code) the structure becomes global to all functions so they all "understand" the structure and can use it as a datatype.

  5. #5
    Unregistered
    Guest
    thanks unregisterd just what I wanted to know.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > If you define the structure inside of "main," it's definition is local
    > in scope to main... main "knows" the structure and can use it,
    > but any function outside of main will not be able to see it (just
    > like if you define a variable inside of main).

    True, and false.

    True, in that if you define a structure locally, you cannot use that
    structure's name/tag in external functions. ie: this is wrong:

    int main ( void ) { struct a { int b,c } d; myfun( d ); return 0; }
    void myfun( struct a e ) { e.b = 10; }

    That wouldn't work. 'myfun' would have no idea how to access the
    internals of the structure 'a'.

    However! You could still access the internals by using the offset!
    Use a void pointer, and by offset, access the internals.

    myfun( void * x )


    Then you could pass a local structure and modify the variable at
    a given offset. I think. It should work. It'd be ugly, and you really
    shouldn't do it this way, but I believe you _could_ do it.

    Quzah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. structure info
    By linuxdude in forum C Programming
    Replies: 6
    Last Post: 05-09-2004, 06:44 PM
  4. how to cast a char *mystring to a structure pointer ??
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2004, 08:59 AM
  5. Function to return pointer to structure
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 06-25-2002, 06:10 AM