Skip to content

Instantly share code, notes, and snippets.

@pareksha
Created November 28, 2017 03:11
Show Gist options
  • Save pareksha/99c9b3285a9b539cbe715034eaf87bd4 to your computer and use it in GitHub Desktop.
Save pareksha/99c9b3285a9b539cbe715034eaf87bd4 to your computer and use it in GitHub Desktop.
c programs
#include<stdio.h>
#include<stdlib.h>
void main()
{
int a[2][3];
int (*p)[3];
p = a;
int i,j,k;
printf("Enter elements of '2 x 3' array:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
scanf("%d",(*(p+i)+j));
}
printf("\n\nAddresses of array is:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
printf("%p\t",(*(p+i)+j));
printf("\n");
}
printf("\n\nArray is:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
printf("%d\t",*(*(p+i)+j));
printf("\n");
}
}
#include<stdio.h>
#include<string.h>
struct author{
char first[25];
char last[25];
};
struct book{
char title[50];
struct author name;
long int isbn_number;
};
void print_book(struct book *bk){
printf("Title of the book : ");
puts(bk -> title);
printf("Name of the author is : ");
printf("%s %s\n",bk -> name.first, bk -> name.last);
printf("ISBN Number = %ld\n",bk -> isbn_number);
printf("\n\n");
}
void input_book(struct book *bk){
printf("Enter title of the book : ");
scanf("%s",bk -> title);
printf("Enter first name of the author : ");
scanf("%s",bk -> name.first);
printf("Enter last name of the author : ");
scanf("%s",bk -> name.last);
printf("Enter ISBN Number : ");
scanf("%ld",&(bk -> isbn_number));
printf("\n\n");
}
void main(){
int i;
struct book BOOK[2];
printf("Input information on books: \n\n");
for(i=0;i<2;i++)
{
input_book(BOOK + i);
}
printf("Books are:\n\n");
for(i=0;i<2;i++)
{
print_book(BOOK + i);
}
}
#include<stdio.h>
#include<stdlib.h>
void main()
{
int* int_;
float* float_;
int i;
int_ = (int*)malloc(sizeof(int));
*int_ = 10;
printf("%p : %d\n",int_,*int_);
free(int_);
printf("After deallocating the memory using free:\n");
printf("%p : %d\n",int_,*int_);
float_ = (float*)malloc(sizeof(float));
*float_ = 12.4546;
printf("\n\n%p : %f\n",float_,*float_);
free(float_);
printf("After deallocating the memory using free:\n");
printf("%p : %f\n\n\n",float_,*float_);
int_ = (int*)malloc(5*sizeof(int));
printf("Allocating the memory for an array: \n");
for(i=0;i<5;i++)
{
*(int_+i) = i+1;
printf("%p : %d\n",int_ + i,*(int_ + i));
}
free(int_);
}
#include<stdio.h>
#include<string.h>
struct book{
char name[50];
char author[50];
long int isbn_number;
};
void print_book(struct book bk){
printf("Title of the book : ");
puts(bk.name);
printf("Name of the author : ");
puts(bk.author);
printf("ISBN Number = %ld\n",bk.isbn_number);
}
void print_by_ref(char* name,char* author,long int* isbn_number){
printf("Title of the book : ");
puts(name);
printf("Name of the author : ");
puts(author);
printf("ISBN Number = %ld\n",*isbn_number);
}
void passing_struct_pointer(struct book *bk){
printf("Title of the book : ");
puts(bk -> name);
printf("Name of the author : ");
puts(bk -> author);
printf("ISBN Number = %ld\n",bk -> isbn_number);
}
void input_book(struct book *bk){
printf("Enter title of the book : ");
gets(bk -> name);
printf("Enter name of the author : ");
gets(bk -> author);
printf("Enter ISBN Number : ");
scanf("%ld",&(bk -> isbn_number));
printf("\n\n");
}
void main(){
struct book book_1,book_2;
strcpy(book_1.name,"You Can Win");
strcpy(book_1.author,"Shiv Khera");
book_1.isbn_number = 1234567;
printf("Printing from function (Call by value) : \n");
print_book(book_1);
printf("\n\n");
printf("Printing from function (Call by reference) : \n");
print_by_ref(book_1.name,book_1.author,&book_1.isbn_number);
printf("\n\n");
printf("Printing from function (Passing reference of whole structure) : \n");
passing_struct_pointer(&book_1);
printf("\n\n");
printf("Enter input for book 2 (from function) : \n");
input_book(&book_2);
printf("Printing from function (Call by value) : \n");
print_book(book_2);
printf("\n\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment