-->

Facebook

Jasper Roberts - Blog
Showing posts with label cpu theory. Show all posts
Showing posts with label cpu theory. Show all posts

Wednesday, April 15, 2015

Pointers and Structures

For representing complex data structures, we can use structures and pointers together.

Pointer can be a member of structure, or a pointer to a structure, or an array of pointers to structure.

We can declare a pointer to structure as we do for pointers to basic data types.

Structure Syntax:
            struct structure_name
            {
                        datatype-1 variable-1;
                        datatype-2 variable-2;
                                    .
                                    .
                        datatype-n variable-n;
            }struct_variable, ponter-to-struct_variable;

Here, struct is a keyword which is used to define a structure template.
struct name is a name of the structure. It is user defined name.
The members of the structure are written in curly { } braces.
The structure is terminated by semicolon (;).

Structure to Pointer Example:
            struct student
            {
                        int rollno;
                        char name[50];
                        char branch[20];
                        float per;
            }s1, *sptr;

sptr = &s1;

Now, sptr points to s1 and can be used to access member variable of struct student.

To access member variables using pointer to structure, we have to use an arrow -> operator.
Syntax:
            struct_pointer -> member_variable;
Here, struct_pointer is a pointer to structure, and member_variable is the name of member variable.


Example:
            s1 -> rollno = 20;

We can also use dot (.) operator to access member variable with pointer to structure like:
            (*s1).rollno = 20;