{"id":3673,"date":"2014-03-29T07:47:59","date_gmt":"2014-03-29T07:47:59","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/29\/create-c-structs-using-a-variable-as-filename-collection-of-common-programming-errors\/"},"modified":"2014-03-29T07:47:59","modified_gmt":"2014-03-29T07:47:59","slug":"create-c-structs-using-a-variable-as-filename-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/29\/create-c-structs-using-a-variable-as-filename-collection-of-common-programming-errors\/","title":{"rendered":"create C structs using a variable as filename-Collection of common programming errors"},"content":{"rendered":"<p>You probably want a linked list, rather than an array, if you will be changing its size often. Then, your code looks something like:<\/p>\n<pre><code>typedef struct node{  \n    char name;\n    char sname;\n    int number;\n    struct node* next;\n}foo;\n<\/code><\/pre>\n<p>And you would use functions like the following to add new nodes\/fetch nodes:<\/p>\n<pre><code>foo head = NULL;\n\nvoid addNode(foo newNode)\n{\n    if(head == NULL)\n        head = newNode;\n    else\n    {\n        foo temp = head;\n        while(foo-&gt;next != NULL)\n            foo = foo-&gt;next;\n        foo-&gt;next = newNode\n    }\n}\n\nfoo fetchNode(int index)\n{\n    if(index &lt; 0)\n        return NULL;\n    int n = 0\n    foo temp = head;\n    while(n &lt; index &amp;&amp; temp != NULL)\n    {\n        temp = temp-&gt;next;\n        n++;\n    }\n    return temp;\n}\n<\/code><\/pre>\n<p>The way this works is that each node has the necessary data, plus a pointer to the next node, which is NULL if its the last node. Then, all you need is a pointer to the first one and you can fetch nodes by walking the next pointers. This also makes it trivial to delete a node that is partway down the list.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You probably want a linked list, rather than an array, if you will be changing its size often. Then, your code looks something like: typedef struct node{ char name; char sname; int number; struct node* next; }foo; And you would use functions like the following to add new nodes\/fetch nodes: foo head = NULL; void [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3673","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3673","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3673"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3673\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}