{"id":3826,"date":"2024-02-04T10:31:07","date_gmt":"2024-02-04T05:01:07","guid":{"rendered":"https:\/\/cthecosmos.com\/?p=3826"},"modified":"2024-02-04T10:31:07","modified_gmt":"2024-02-04T05:01:07","slug":"common-combinations-and-their-usage-in-c","status":"publish","type":"post","link":"https:\/\/cthecosmos.com\/?p=3826","title":{"rendered":"Common Combinations and their usage in C"},"content":{"rendered":"\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-68475d2f5c4b89ad422bc47d20ee95be\" style=\"font-size:17px\"><strong>Pointer to Pointer<\/strong> \u2013 A simple pointer to another pointer Commonly used in various scenarios where indirection or multiple levels of indirection are required.<br><br>Example:<br><br><strong><em>char *ptr =(char*)malloc(sizeof(char));<\/em><\/strong><br><strong><em>char **ptr_to_ptr =&amp;ptr;<\/em><\/strong><br><br><br><strong>Pointer to Arrays<\/strong> \u2013 A point to an array. Can be used for manipulating the array or collecting an array from a function.<br><br>Example:<br><br><strong><em>char array[] ={ \u2018w\u2019,\u2019o\u2019,\u2019r\u2019,\u2019k\u2019,\u2019s\u2019};<\/em><\/strong><br><strong><em>char *ptr_to_array = array;<\/em><\/strong><br><br><br><strong>Pointer to multidimensional arrays<\/strong> \u2013 A pointer to collect a multidimensional array. Can be used to traversal and manipulation of multidimensional array values.<br><br>Example:<br><br><strong><em>int multi_arr[][4] ={ {10,19,20,12}, {30,59,32,77}, {11,2578,15,117} };<br>int (*ptr_to_multi_arr)[4] = multi_arr;<\/em><\/strong><br><br><strong>*(ptr_to_multi_arr) <\/strong>gives first array<br><strong>*(*(ptr_to_multi_arr) + 1) gives<\/strong> first array second element<br><br><br><strong>Pointer to functions<\/strong>: \u00a0Can be uses to pass function pointers as arguments. Ex: Can be used in file operations, system calls, operations on device drivers.<br><br>Example 1:<br><br><strong><em>loff_t (*llseek) (struct file *, loof_t, int);<br>ssize_t (*read) (struct file *, char __user *,size_t, loof_t*);<\/em><\/strong><br><br>Example 2:<br><br><strong><em>int fun1(int a, int b) { \/\/ Operation1\u2026}<br>int fun2(int c, int d) { \/\/ Operation 2\u2026}<br>void main()<br>{<br>int some_val1,some_val2,some_val3;<br>int store_some_val;<br>int (*ptr_to_function)(int,int);<br>ptr_to_function = fun1;<br>store_some_val = ptr_to_function( some_val1, some_val2);<br>ptr_to_function = fun2;<br>store_some_val = ptr_to_function( some_val3, some_val1);<br><br>}<\/em><\/strong><br><br><br><strong>Array of structures<\/strong> \u2013 Used to store multiple strcutures in continuous block of memory. Can be useful when working on multiple instances of structure and perform operations as a whole. Ex: Process Control Blocks. Scull_dev strcut to avoid race conditions.<br><br>Example:<br><br><strong><em>struct scull_dev{<br>\u2026.<br>int quantum;<br>int qset;<br>unsigned long size; \u2026<br>struct semaphore sem;<br>\u2026.<br>};<br>struct scull_dev scull_devices[10];<br><\/em><\/strong><br><br><strong>Pointers to structures<\/strong>: Used to dynamically allcoate memory for structires and access its contents indirectly. Useful to modify the structure the structure member contents without making a copy.<br><br>Example:<br><br><strong><em>struct scull_pipe{<br>\u2026<br>char *rp ,*wp;<br>int nreaders, nwriters;<br>\u2026<br>struct cdev cdev; \/\/ Char driver structure<br>}<br><br>static ssize_t scull_p_read( struct file *filp, char __user *bud, size_t count, loff_t *f_pos)<br>{<br>Struct scull_pipe *dev = filp-&gt;private_data; \/\/ dev is a pointer to scull_pipe structure used to store private_data from filp structure.<br>\u2026<br>}<\/em><\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-white-color has-text-color has-background has-link-color has-medium-font-size wp-elements-486e7799b35920eece0029316d5e98bd\" style=\"background:linear-gradient(135deg,rgb(35,23,11) 0%,rgb(254,45,45) 50%,rgb(107,0,62) 100%)\">~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br><strong>An Article by: <\/strong>Yashwanth Naidu Tikkisetty<br>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article by Yashwanth Naidu Tikkisetty discusses the usage of pointers in C programming. It covers pointer to pointer, pointer to arrays, pointer to multidimensional arrays, pointer to functions, array of structures, and pointers to structures, providing examples and applications for each.<\/p>\n<a href=\"https:\/\/cthecosmos.com\/?p=3826\" class=\"more-link\">Read More <span class=\"screen-reader-text\">Common Combinations and their usage in C<\/span><\/a>","protected":false},"author":120055267,"featured_media":3829,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":false,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_wpas_customize_per_network":false},"categories":[769112296,36985],"tags":[584090,1963876,201570117,769116859,427291309,769112315,20863,67544],"class_list":{"0":"post-3826","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","6":"hentry","7":"category-c-concepts-hub","8":"category-c-programming","9":"tag-arrays","10":"tag-clearning","11":"tag-codereading","12":"tag-cprogram-2","13":"tag-cprogramming-2","14":"tag-cwithyash","15":"tag-pointers","16":"tag-structures","18":"fallback-thumbnail"},"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/02\/commondeclarations.webp?fit=1024%2C1024&ssl=1","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8CiEf-ZI","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/3826","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/users\/120055267"}],"replies":[{"embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3826"}],"version-history":[{"count":4,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/3826\/revisions"}],"predecessor-version":[{"id":3831,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/3826\/revisions\/3831"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/media\/3829"}],"wp:attachment":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}