Skip to main content

Posts

Showing posts from August, 2020

strings in c

#include <stdio.h> int main() { char name1[] = { 's' , 'a' , 't' , 'y' , 'a' , 'm' , ' \0 ' ,}; printf ( "%s" ,name1); char name2[] = " \n satyam" ; printf ( "%s" ,name2); return 0 ; }

example of printing star pattern

#include <stdio.h> void starPattern( int rows ) { for ( int i = 0 ; i < rows ; i ++ ) { for ( int j = 0 ; j <= i; j ++ ) { printf ( "*" ); } printf ( " \n " ); } } void reverseStarPattern( int rows ) { for ( int i = 0 ; i < rows ; i ++ ) { for ( int j = 0 ; j <= rows - i - 1 ; j ++ ) { printf ( "*" ); } printf ( " \n " ); } } int main() { int rows, type; printf ( "Enter 0 for star pattern \n Enter 1 for reversed star pattern \n " ); scanf ( "%d" , & type); printf ( "How many rows do you want? \n " ); scanf ( "%d" , & rows); switch (type) { case 0 : starPattern (rows); break ; case 1 : reverseStarPattern (rows); break ; default: printf ( "You have entered an invalid choice" ); break ; } return 0 ; }

passing array to a function by declaring pointers in the function

#include <stdio.h> int fun1( int* ptr ) { for ( int i = 0 ; i < 4 ; ++ i) { printf ( "The value of %d is %d \n " ,i, ptr [i]); //or printf ( "The value of %d is %d \n " ,i, * ( ptr + i)); // both do the same work } * ( ptr + 2 ) = 58 ; //value gets changed of 2nd index of array } int main () { int arr[] = { 2 , 4 , 6 , 8 }; fun1 (arr); fun1 (arr); return 0 ; }

passing array to a function by declaring array as parameter in function

#include <stdio.h> int fun1( int array []) { for ( int i = 0 ; i < 4 ; ++ i) { printf ( "The value of %d is %d \n " ,i, array [i]); } array [ 0 ] = 89 ; /*if we change the formal(parameter) value effect will be reflected in actual(parameter) value as it passes the address of array*/ } int main () { int arr[] = { 2 , 4 , 6 , 8 }; printf ( "The value of index 0 is %d \n " ,arr[ 0 ]); fun1 (arr); printf ( "The value of index 0 is %d \n " ,arr[ 0 ]); return 0 ; }

Array pointers arithmetic in c

#include <stdio.h> int main () { int arr [] = { 1 , 4 , 5 , 9 , 7 , 6 }; printf ( "The value of array at position 3 is %d \n " , arr [ 3 ]); printf ( "The address of first element of array is %d \n " , & arr [ 0 ]); printf ( "The address of first element of array is %d \n " , arr ); printf ( "The address of second element of array is %d \n " , & arr [ 1 ]); printf ( "The address of first element of array is %d \n " , arr + 1 ); printf ( "The value of address at first element of array is %d \n " , * ( arr )); printf ( "The value of address at first element of array is %d \n " , * ( & arr [ 0 ])); printf ( "The value of address at second element of array is %d \n " , * ( & arr [ 1 ])); printf ( "The value of address at first element of array is %d \n " , arr [ 0 ]); printf ( "The value of address at second element of array is %d \n " , * ( ar

Pointer arithmetic in C

#include <stdio.h> int main () { int a = 24 ; int* ptra = & a ; char b = '3' ; char* ptrb =& b ; printf ( "%d \n\n " , ptrb ); //size of char is 1 ptrb ++ ; printf ( "%d \n\n " , ptrb ); // add 1 to &b ptrb -- ; printf ( "%d \n\n " , ptrb + 1 ); // add 1 to &b printf ( "%d \n\n " , ptrb - 1 ); // subtract 1 to &b printf ( "%d \n\n " , ptrb + 1 ); // add 1 to &b printf ( "%d \n\n " , ptrb - 2 ); // subtract 2*1 to &b printf ( "%d \n\n " , ptra ); printf ( "%d \n\n " , ptra + 1 ); // size of int is 4 so, it add 4 to &a/ptra printf ( "%d \n " , ptra - 2 ); // size of int is 4 so, it subtract 2*4 to &a/ptra return 0 ; }