Advertisements

Search results for struct

Example:[1]

            Write a program to swap the 4bytes of an integer. If input is provided as 0x12345678 output is generated as 0x78563412.
          

            #include <stdio.h>
union out
{
   struct
   {
      unsigned char ll;
      unsigned char lh;
      unsigned char hl;
      unsigned char hh;
   }in;
   unsigned int b;
};
int main()
{
   union out aa;
   aa.b = 0x12345678;
   printf("Before swapping:%x\n", aa.b);
   unsigned char ll = aa.in.ll;
   unsigned char lh = aa.in.lh;
   aa.in.ll = aa.in.hh;
   aa.in.hh = ll;
   aa.in.lh = aa.in.hl;
   aa.in.hl = lh;
   printf("After swapping:%x\n", aa.b);
   return 0;
}
          
Example:[2]

            Write a C program function to enter different book information(pages, author, search id) in a school library.
          

            #include <stdio.h>
#define NO_BOOKS 3
struct Book
{
	int pages;
	char author[10];
	int search_id;
};
void EnterBooks(struct Book *);
int main()
{
	struct Book b[NO_BOOKS];
	EnterBooks(b);
	return 0;
}
void EnterBooks(struct Book *a)
{
	int i;
	for(i = 0;i < NO_BOOKS;i++)
	{
		printf("Enter %d book pages\n", i+1);
		scanf("%d",&a->pages);
		printf("Enter %d book author\n", i+1);
		scanf("%s",&a->author);
		printf("Enter %d search id\n", i+1);
		scanf("%d",&a->search_id);
		a++;
	}
}
          
Example:[3]

            Write a C program function to enter different book information(pages, author, search id) in a school library and display the existing books using structure pointers.

          

            #include <stdio.h>
#include <stdlib.h>
#define NO_BOOKS 3
struct Book
{
	int pages;
	char author[10];
	int search_id;
};
void EnterBooks(struct Book *);
void DisBooks(struct Book *);
int main()
{
	struct Book *b;
	b = (struct Book *)malloc(sizeof(struct Book)*NO_BOOKS);
	if(b == NULL)
	{
		printf("Req memory allocation failed\n");
	}else
	{
		EnterBooks(b);
		DisBooks(b);
	}
	free(b);
	return 0;
}
void EnterBooks(struct Book *a)
{
	int i;
	for(i = 0;i < NO_BOOKS;i++)
	{
		printf("Enter %d book pages\n", i+1);
		scanf("%d",&a->pages);
		printf("Enter %d book author\n", i+1);
		scanf("%s",&a->author);
		printf("Enter %d search id\n", i+1);
		scanf("%d",&a->search_id);
		a++;
	}
}
void DisBooks(struct Book *a)
{
	int i;
	for(i = 0;i < NO_BOOKS;i++)
	{
		printf("Book %d pages %d\n",i+1,a->pages);
		printf("Book %d author %s\n",i+1,a->author);
		printf("Book %d book search id %d\n",i+1,a->search_id);
		a++;
	}
}

          
Example:[4]

            Write a C program function to enter different book information(pages, author, search id) in a school library and display the existing books using structure arrays.

          

            #include <stdio.h>
#define NO_BOOKS 3
struct Book
{
	int pages;
	char author[10];
	int search_id;
};
void EnterBooks(struct Book *);
void DisplayBooks(struct Book *);
int main()
{
	struct Book b[NO_BOOKS];
	EnterBooks(b);
	DisplayBooks(b);
	return 0;
}
void EnterBooks(struct Book *a)
{
	int i;
	for(i = 0;i < NO_BOOKS;i++)
	{
		printf("Enter %d book pages\n", i+1);
		scanf("%d",&a->pages);
		printf("Enter %d book author\n", i+1);
		scanf("%s",&a->author);
		printf("Enter %d search id\n", i+1);
		scanf("%d",&a->search_id);
		a++;
	}
}
void DisplayBooks(struct Book a[])
{
	int i;
	for(i = 0;i < NO_BOOKS;i++)
	{
		printf("Book %d pages %d\n",i+1,a[i].pages);
		printf("Book %d author %s\n",i+1,a[i].author);
		printf("Book %d book search id %d\n",i+1,a[i].search_id);
	}
}
          
Example:[5]

            Write a C program function to enter different book information(pages, author, search id) in a school library and search any book using search id and structure arrays.

          

            #include <stdio.h>
#define NO_BOOKS 3
struct Book
{
	int pages;
	char author[10];
	int search_id;
};
void EnterBooks(struct Book *);
struct Book *SrchBook(struct Book a[]);
int main()
{
	struct Book b[NO_BOOKS];
	EnterBooks(b);
	struct Book *s;
	s = SrchBook(b);
	printf("Book pages:%d\n", s->pages);
	printf("Book author:%s\n", s->author);
	printf("Book search_id:%d\n", s->search_id);
	return 0;
}
void EnterBooks(struct Book *a)
{
	int i;
	for(i = 0;i < NO_BOOKS;i++)
	{
		printf("Enter %d book pages\n", i+1);
		scanf("%d",&a->pages);
		printf("Enter %d book author\n", i+1);
		scanf("%s",&a->author);
		printf("Enter %d search id\n", i+1);
		scanf("%d",&a->search_id);
		a++;
	}
}
struct Book *SrchBook(struct Book a[])
{
	int i,id;
	printf("Enter the book search id\n");
	scanf("%d",&id);
	for(i = 0;i < NO_BOOKS;i++)
	{
		if(id == a[i].search_id)
		{
			return &a[i];
		}
	}
	if(i == NO_BOOKS)
	{
		printf("Invalid Search_id, try again\n");
		return NULL;
	}
}
          
Example:[6]

            Write a C program function to enter different book information(pages, author, search id) in a school library and search any book using search id and structure pointers.
          

            #include <stdio.h>
#include <stdlib.h>
#define NO_BOOKS 3
struct Book
{
	int pages;
	char author[10];
	int search_id;
};
void EnterBooks(struct Book *);
struct Book *SrchBook(struct Book *);
int main()
{
	struct Book *b;
	b = (struct Book *)malloc(sizeof(struct Book)*NO_BOOKS);
	if(b == NULL)
	{
		printf("Req memory allocation failed\n");
	}else
	{
		EnterBooks(b);
		struct Book *s;
		s = SrchBook(b);
		printf("Book pages:%d\n", s->pages);
		printf("Book author:%s\n", s->author);
		printf("Book search_id:%d\n", s->search_id);
	}
	free(b);
	return 0;
}
void EnterBooks(struct Book *a)
{
	int i;
	for(i = 0;i < NO_BOOKS;i++)
	{
		printf("Enter %d book pages\n", i+1);
		scanf("%d",&a->pages);
		printf("Enter %d book author\n", i+1);
		scanf("%s",&a->author);
		printf("Enter %d search id\n", i+1);
		scanf("%d",&a->search_id);
		a++;
	}
}
struct Book *SrchBook(struct Book *a)
{
	int i,id;
	printf("Enter the book search id\n");
	scanf("%d",&id);
	for(i = 0;i < NO_BOOKS;i++)
	{
		if(id == a->search_id)
		{
			return &a[i];
		}
		a++;
	}
	if(i == NO_BOOKS)
	{
		printf("Invalid Search_id, try again\n");
		return NULL;
	}
}
          
Example:[7]

            Write a c program function to display all node data present in a singly linked list.
          

            #include <stdio.h>
#include <stdlib.h>
struct Node
{
	int data;
	struct Node *next;
};
struct Node *head = NULL;
void AddAtBegin(int );
void Traverse();
void main()
{
	AddAtBegin(10);
	Traverse();
}
void AddAtBegin(int val)
{
	struct Node *ptr;
	if(head == NULL)
	{
		ptr = (struct Node *)malloc(sizeof(struct Node ));
		if(ptr == NULL)
		{
			printf("Req memory allocation failed\n");
		}else
		{
			ptr->data = val;
			ptr->next = NULL;
			head = ptr;
		}
	}else
	{
		ptr = (struct Node *)malloc(sizeof(struct Node ));
		if(ptr == NULL)
		{
			printf("Req memory allocation failed\n");
		}else
		{
			ptr->data = val;
			ptr->next = head;
			head = ptr;
		}
	}
}
void Traverse()
{
	struct Node *ptr = head;
	int n = 1;
	while(ptr != NULL)
	{
		printf("Node %d data %d\n", n,ptr->data);
		ptr = ptr->next;
		n++;
	}	
}
          
Example:[8]

            Write a c singly linked list program to add a node at the beginning of the list.
          

            #include <stdio.h>
#include <stdlib.h>
struct Node
{
	int data;
	struct Node *next;
};
struct Node *head = NULL;
void AddAtBegin(int );
void main()
{
	AddAtBegin(10);
}
void AddAtBegin(int val)
{
	struct Node *ptr;
	if(head == NULL)
	{
		ptr = (struct Node *)malloc(sizeof(struct Node ));
		if(ptr == NULL)
		{
			printf("Req memory allocation failed\n");
		}else
		{
			ptr->data = val;
			ptr->next = NULL;
			head = ptr;
		}
	}else
	{
		ptr = (struct Node *)malloc(sizeof(struct Node ));
		if(ptr == NULL)
		{
			printf("Req memory allocation failed\n");
		}else
		{
			ptr->data = val;
			ptr->next = head;
			head = ptr;
		}
	}
}
          
Example:[9]

            Write a c program function to add new nodes at the end of a singly linked list.
          

            #include <stdio.h>
#include <stdlib.h>
struct Node
{
	int data;
	struct Node *next;
};
struct Node *head = NULL;
void AddAtEnd(int );
void Traverse();
void main()
{
	AddAtEnd(10);
	AddAtEnd(20);
	Traverse();
	AddAtEnd(30);
	Traverse();
}
void AddAtEnd(int val)
{
	struct Node *ptr,*aptr = head,*bptr;
	if(head == NULL)
	{
		ptr = (struct Node *)malloc(sizeof(struct Node ));
		if(ptr == NULL)
		{
			printf("Req memory allocation failed\n");
		}else
		{
			ptr->data = val;
			ptr->next = NULL;
			head = ptr;
		}
	}else
	{
		ptr = (struct Node *)malloc(sizeof(struct Node ));
		if(ptr == NULL)
		{
			printf("Req memory allocation failed\n");
		}else
		{
			while(aptr != NULL)
			{
				bptr = aptr;
				aptr = aptr->next;
			}
			ptr->data = val;
			bptr->next = ptr;
			ptr->next = NULL;
		}
	}
}
void Traverse()
{
	struct Node *ptr = head;
	int n = 1;
	while(ptr != NULL)
	{
		printf("Node %d data %d\n", n,ptr->data);
		ptr = ptr->next;
		n++;
	}	
}
          
Example:[10]

            Write a c program function to add new nodes in the middle of a singly linked list.
          

            #include <stdio.h>
#include <stdlib.h>
struct Node
{
	int data;
	struct Node *next;
};
struct Node *head = NULL;
void AddAtEnd(int );
void AddAtMid(int ,int );
void Traverse();
void main()
{
	AddAtEnd(10);
	AddAtEnd(20);
	AddAtEnd(30);
	AddAtMid(25,2);
	Traverse();
}
void AddAtEnd(int val)
{
	struct Node *ptr,*aptr = head,*bptr;
	if(head == NULL)
	{
		ptr = (struct Node *)malloc(sizeof(struct Node ));
		if(ptr == NULL)
		{
			printf("Req memory allocation failed\n");
		}else
		{
			ptr->data = val;
			ptr->next = NULL;
			head = ptr;
		}
	}else
	{
		ptr = (struct Node *)malloc(sizeof(struct Node ));
		if(ptr == NULL)
		{
			printf("Req memory allocation failed\n");
		}else
		{
			while(aptr != NULL)
			{
				bptr = aptr;
				aptr = aptr->next;
			}
			ptr->data = val;
			bptr->next = ptr;
			ptr->next = NULL;
		}
	}
}
void Traverse()
{
	struct Node *ptr = head;
	int n = 1;
	while(ptr != NULL)
	{
		printf("Node %d data %d\n", n,ptr->data);
		ptr = ptr->next;
		n++;
	}	
}
void AddAtMid(int val,int pos)
{
	struct Node *aptr = head,*bptr = head,*cptr;
	int count = 0;
	if(head == NULL)
	{
		printf("List is empty\n");
	}else
	{
		while(aptr != NULL)
		{
			aptr = aptr->next;
			count++;
		}
		printf("%d\n", count);
		if(count < pos)
		{
			printf("We have less nodes than %d\n", pos);
		}else
		{
			aptr = (struct Node *)malloc(sizeof(struct Node ));
			if(aptr == NULL)
			{
				printf("Req memory allocation failed\n");
			}else
			{
				count = 0;
				while(count != pos)
				{
					cptr = bptr;
					bptr = bptr->next;
					count++;	
				}	
				aptr->data = val;
				cptr->next = aptr;
				aptr->next = bptr; 
			}
		}
	}
}
          

 Search Links


 Popular C search MCQs


 Popular C search examples