Thread: need help on struct *pointer

  1. #1

    Question need help on struct *pointer

    I am a C newbie, and I really don't know what struct ** mean?
    I have read many books but no books explain detail and clearly.
    can anyone please explain what it mean?
    for example:

    struct student
    int ID;
    float GPA;

    struct student **stu; /*is this mean an array of struct or it mean
    a pointer to struct*/
    also what does it different between struct * stu and struct**stu?

    and when passing struct ** to a function what does it mean?
    for example:
    void searchStudent(int id, struct student **stud)
    /*how to use struct student**stud here
    does it imply an array of struct student? or a link list???
    Thank you very much for your help

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Start with this link

    It's a pretty good explanation

    If you have
    struct student
    int ID;
    float GPA;
    struct student fred;

    And you do
    fred.ID = 1;

    struct student *ptr_to_fred = &fred;

    And you do
    ptr_to_fred->ID = 1;

    struct student **ptr_to_ptr_to_fred = &ptr_to_fred;

    And you do
    (*ptr_to_ptr_to_fred)->ID = 1;

    > void searchStudent(int id, struct student **stud)
    I imagine in this example, that you would want to update stud with the pointer to the student you found

    Say you had an array
    struct student students[10];

    Then somewhere in this function, when you had found the student you were looking for, you would do this
    *stud = &students[i];

    In the calling function, you would have something like this
    struct student *found_stud;
    searchStudent( 1, &found_stud );
    printf( "Found = %d\n", found_stud->ID );

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Cape Town
    struct**, char**, int**, float**

    can be used to create a dynamic array of either

    struct*, char*, int*, float*

    struct **array;           //can be resized dynamically 
    struct *array[10];      //cannot be resized
    By resizing the char**, you can add more and more elements.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. Tutorial review
    By Prelude in forum A Brief History of
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM