Uploaded by devansh ganguly

Pointers in C Programming: Definition, Declaration, Examples

advertisement
POINTERS
Pointer: a variable which stores the
address of another
variable.
int n = 10;
int* p = &n; /* Variable p of type
pointer is pointing to the address of
the variable n of type integer. */
Declaring a pointer
int *a;//pointer to int
char *c;//pointer to char
Pointer Example
Structure Pointer
Pointers
in
Self-Referential Structure
Pointers should be properly
initialized.
Otherwise Garbage Values will be
assigned.
#include<stdio.h>
#include<stdlib.h>
void main()
{
struct node
{
int data;
struct node *next;
};
struct node *head,*newnode,*temp;
int choice=1, count=0;
head=0;
while(choice)
{
count=count+1;
newnode=(struct node*)malloc(sizeof(struct node));
/*Get the value for the data part of the new node through printf() and scanf()*/
printf("Enter the data of the new node");
scanf(“%d”,&newnode->data);
if(head==0)
head=temp=newnode;//head=newnode
else
temp->next=newnode;//head->next=newnode
temp=newnode;
//temp=head;
//Take the value for choice through output
printf("Do you want to insert more node(s)-Enter 1 for Yes or 0 for No");
scanf("%d",&choice);
}
//printf("Total Number of nodes=%d",count);
temp=head;
while (temp!=0)
{
printf("%d\n",temp->data);
temp=temp->next;
//++count;
}
printf("Total Number of nodes=%d",count);
}
Download