Session 1 Recursive Function Pointer Structure and Self-Referential Structure - Manual
Session 1 Recursive Function Pointer Structure and Self-Referential Structure - Manual
Exercises:
1. Write a recursive function that computes the sum of all numbers from 1 to n, where n is given as
parameter.
2. Write a recursive function that finds and returns the minimum element in an array, where the array
and its size are given as parameters.
Basic Structure:
Pointer:
When setting up data structures like lists, queues and trees, it is necessary to have pointers to help
manage how the structure is implemented and controlled. The basic syntax to define a pointer is:
int *ptr;
In any case, once a pointer has been declared, the next logical step is for it to point at something:
In brief, One of the member of the structure is pointer of the same type.
For example:
struct node //{here marked portion is “data type}
{
int data1;
int data2;
struct node *next; //{here red marked portion is “self referential member”}
};
Here, the structure 'node' contains a pointer named 'next', which is of the same type (struct node) as the
structure it contains in (struct node).
The above illustrated structure prototype describes one node that comprises of two logical segments.
One of them stores data/information and the other one is a pointer indicating where the next
component can be found. .Several such inter-connected nodes create a chain of structures.
typedef struct node point; //green marked is old data type and blue marked is new data type
typedef really does simply declare a new name for a type. It does not create a new type. #simple
program on creating nodes and assigning data:
X=10 Y=15 Pt
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int x,y;
struct Node *next;
};
typedef struct Node node;
int main()
{
node *head;
head=(node*)malloc(sizeof(node));
head->x=10;
head->y=15;
head->pt=NULL;
printf("%d %d\n",head->x,head->y);
return 0;
}
Structures in C:
What is Structure?
• A structure is a user defined data type in C/C++. A structure creates a data type that can be used
to group items of possibly different types into a single type
How to create Structure?
• ‘struct’ keyword is used to create a structure. Following is an example.
struct address
{
char name[50];
char street[100];
char city[50];
char state[20]
int pin;
};
struct database {
int id_number;
int age;
float salary;
};
int main()
{
struct database employee; /* There is now an employee variable that has
modifiable variables inside it.*/
employee.age = 22;
employee.id_number = 1;
employee.salary = 12000.21;
}
#include <stdio.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main( ) {
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
return 0;
}
When the above code is compiled and executed, it produces the following result −
#include <stdio.h>
struct xampl {
int x;
};
int main()
{
struct xampl structure;
struct xampl *ptr;
structure.x = 12;
ptr = &structure; /* Yes, you need the & when dealing with
structures and using pointers to them*/
printf( "%d\n", ptr->x ); /* The -> acts somewhat like the * when
does when it is used with pointers
It says, get whatever is at that memory
address Not "get what that memory address
is"*/
getchar();
}