{"id":7192,"date":"2025-10-11T00:01:29","date_gmt":"2025-10-11T07:01:29","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7192"},"modified":"2025-10-18T10:00:07","modified_gmt":"2025-10-18T17:00:07","slug":"the-ever-expanding-pointer-array","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7192","title":{"rendered":"The Ever-Expanding Pointer Array"},"content":{"rendered":"<p>Perhaps the most difficult part of learning C is understanding and coding pointers. Even when you have a grip on things, it helps to keep fresh and practice new and utterly obnoxious ways to play with pointers.<br \/>\n<!--more--><br \/>\nFirst, I&#8217;m creating a new rule rising from a post I made on <a href=\"https:\/\/www.linkedin.com\/in\/dan-gookin-46995713\/\" target=\"_blank\">LinkedIn<\/a>. The feedback I&#8217;ve received from fellow C programmers is to always initialize your pointers variables to NULL. In fact, one coder said to initialize <em>all<\/em> of your variables, no matter what the data type.<\/p>\n<p>I like this idea. In fact, in many programming languages, all variables are initialized to zero or some default value like NULL; strings are declared as empty.<\/p>\n<p>Second, I was trying to figure out a way to incrementally expand an array of pointers. Not just a construction like an array of strings, but a pointer that holds the addresses of allocated buffers. This goal proved to be an interesting mind game that I believe I can attempt only while sober.<\/p>\n<p>To start, here is sample code, the base upon which I built my monster:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_10_11-Lesson.c\" rel=\"noopener\" target=\"_blank\">2025_10_11-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\n<span class=\"comments\">\/* allocate storage for ten integers *\/<\/span>\r\nint *ten(void)\r\n{\r\n    int *iptr = NULL;\r\n\r\n    iptr = malloc( sizeof(int) * 10 );\r\n\r\n    return(iptr);\r\n}\r\n\r\nint main()\r\n{\r\n    int *tenint = NULL;\r\n    int x;\r\n\r\n    <span class=\"comments\">\/* seed the randomizer *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n\r\n    <span class=\"comments\">\/* get the storage *\/<\/span>\r\n    tenint = ten();\r\n    if( tenint==NULL )\r\n    {\r\n        fprintf(stderr,\"Unable to allocate\\n\");\r\n        exit(1);\r\n    }\r\n    puts(\"Storage for 10 integers allocated\");\r\n\r\n    <span class=\"comments\">\/* assign the values *\/<\/span>\r\n    for( x=0; x&lt;10; x++ )\r\n        *(tenint+x) = rand() % 10 + 10;\r\n\r\n    <span class=\"comments\">\/* output the results *\/<\/span>\r\n    for( x=0; x&lt;10; x++ )\r\n        printf(\"%d\\n\",*(tenint+x) );\r\n\r\n    <span class=\"comments\">\/* clean-up *\/<\/span>\r\n    free(tenint);\r\n    return 0;\r\n}<\/pre>\n<p>The <em>ten()<\/em> function allocates storage for ten integers. Variable <code>iptr<\/code> is initialized to NULL, which is my new thing. The function does no error-checking, returning either a valid address for allocated storage or NULL for some kind of booboo.<\/p>\n<p>In the <em>main()<\/em> function, <em>int<\/em> pointer <code>tenint<\/code> holds an address for integer storage. It&#8217;s initialized to NULL: <code>int *tenint = NULL;<\/code><\/p>\n<p>The randomizer is seeded: <code>srand( (unsigned)time(NULL) );<\/code><\/p>\n<p>The <em>ten()<\/em> function is called with the value returned stored in variable <code>tenint<\/code>. A check is made to confirm proper allocation; upon failure, the program exits.<\/p>\n<p>Upon success, a <em>for<\/em> loop allocates random values from 10 through 19 to the ten integer-sized cubbies in memory, stored in pointer <code>tenint<\/code>:<\/p>\n<p><code>for( x=0; x&lt;10; x++ )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;*(tenint+x) = rand() % 10 + 10;<\/code><\/p>\n<p>The expression <code>*(tenint+x)<\/code> references each subsequent integer-sized cubby stored at address <code>tenint<\/code>. The result of <code>tenint+x<\/code> is calculated by using pointer math: each value of <code>x<\/code> increases the offset by an integer-sized chunk in memory. The <code>*<\/code> (dereferencing) operator refers to the value at the location, which is where the random integer is stored.<\/p>\n<p>The next <em>for<\/em> loop outputs the stored values. Then storage is freed and the program ends.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Storage for 10 integers allocated<br \/>\n11<br \/>\n16<br \/>\n15<br \/>\n14<br \/>\n15<br \/>\n16<br \/>\n10<br \/>\n15<br \/>\n17<br \/>\n12<\/code><\/p>\n<p>Each run generates a different set of integers. Nothing guarantees that values aren&#8217;t repeated or in any specific order.<\/p>\n<p>This code represents only the base. My goal is to add another stick of ten integers, and then another one after that. The program keeps adding batches of ten integers and manages them all. This process begins starting with <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7201\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Managing a buncha pointers. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7192\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-7192","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7192"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7192\/revisions"}],"predecessor-version":[{"id":7214,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7192\/revisions\/7214"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}