{"id":16787,"date":"2022-10-22T04:26:14","date_gmt":"2022-10-21T22:56:14","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-linked-list-pointers-prob\/"},"modified":"2022-10-22T04:26:14","modified_gmt":"2022-10-21T22:56:14","slug":"solved-linked-list-pointers-prob","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-linked-list-pointers-prob\/","title":{"rendered":"[Solved] Linked List pointers prob"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-42440921\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"42440921\" data-parentid=\"42439913\" data-score=\"2\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>There are several issues:<\/p>\n<p>Instead of calling<\/p>\n<pre><code> initialisation(&amp;LBO);\n<\/code><\/pre>\n<p>which is not really wrong, just write:<\/p>\n<pre><code>LBO = NULL;\n<\/code><\/pre>\n<p>Then don&#8217;t hide pointers with <code>typedef<\/code>s, it only adds confusion.<\/p>\n<p>Instead of:<\/p>\n<pre><code>typedef struct noeud\n{\n  int adresse, taille, temp;\n  struct noeud* suivant;\n\n} *liste;\n<\/code><\/pre>\n<p>Write:<\/p>\n<pre><code>struct noeud\n{\n  int adresse, taille, temp;\n  struct noeud* suivant;    \n};\n<\/code><\/pre>\n<p>and use <code>struct noeud*<\/code> instead of <code>liste<\/code>.<\/p>\n<p><strong>Now the real problem:<\/strong><\/p>\n<p>This is wrong. Here you allocate the size for a pointer, but you need to allocate the size for the whole structure:<\/p>\n<pre><code>q = malloc(sizeof(liste));\n<\/code><\/pre>\n<p>which is actually the same as:<\/p>\n<pre><code>q = malloc(sizeof(struct noeud*))\n<\/code><\/pre>\n<p>but you need:<\/p>\n<pre><code>q = malloc(sizeof(struct noeud))\n<\/code><\/pre>\n<p>You see now why hiding pointers with typedefs is a bad idea.<\/p>\n<p>So here is the corrected version of your program (<code>#include<\/code>s ommitted for brevity):<\/p>\n<pre><code>struct noeud\n{\n  int adresse, taille, temp;\n  struct noeud* suivant;\n};\n\nint random(int a, int b)\n{\n  return (a + (rand() % ((b + 1) + a)));\n}\n\nvoid creation(struct noeud** LBO)\n{\n  struct noeud* q, *prec = NULL;\n  int i = 0;\n  \/\/ srand(time(NULL));  &lt;&lt;&lt;&lt;&lt; don't call srand here, call it once at the \n                            \/\/ beginning of the program\n  while (i &lt; 3)\n  {\n    printf(\"%d\", i);\n    q = malloc(sizeof(struct noeud));\n\n    if (*LBO == NULL)\n    {\n      q-&gt;adresse = 0;\n      q-&gt;taille = random(5, 45);\n      q-&gt;temp = random(5, 15);\n      q-&gt;suivant = *LBO;\n      *LBO = q;\n      i++;\n    }\n    else\n    {\n      prec = *LBO;\n      q-&gt;taille = random(5, 45);\n      q-&gt;temp = random(5, 15);\n      q-&gt;adresse = prec-&gt;adresse + prec-&gt;taille;\n      q-&gt;suivant = *LBO;\n      *LBO = q;\n      i++;\n    }\n  }\n}\n\nvoid affichage(struct noeud* LBO)\n{\n  printf(\"\\nvoici ta struct noeud* \\n \");\n  while (LBO != NULL)\n  {\n    printf(\"%d--&gt;\", LBO-&gt;taille);\n    LBO = LBO-&gt;suivant;\n  }\n  \/\/ if (LBO == NULL)  &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; drop this, LBO is always NULL here\n                                \/\/ but it doesn't hurt, it's just useless\n    printf(\"NULL\");\n}\n\nint main()\n{\n  srand(time(NULL));   \/\/ &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; call srand here\n  struct noeud* LBO;\n  LBO = NULL;\n\n  creation(&amp;LBO);\n\n  affichage(LBO);\n  return 0;\n}\n<\/code><\/pre>\n<p>There is still room for improvement, especially the <code>creation<\/code> function is somewhat awkward.<\/p>\n<p>Also look at the comments with <code>&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;<\/code>, there are minor corrections<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Linked List pointers prob <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] There are several issues: Instead of calling initialisation(&amp;LBO); which is not really wrong, just write: LBO = NULL; Then don&#8217;t hide pointers with typedefs, it only adds confusion. Instead of: typedef struct noeud { int adresse, taille, temp; struct noeud* suivant; } *liste; Write: struct noeud { int adresse, taille, temp; struct noeud* suivant; &#8230; <a title=\"[Solved] Linked List pointers prob\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-linked-list-pointers-prob\/\" aria-label=\"More on [Solved] Linked List pointers prob\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324],"class_list":["post-16787","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] Linked List pointers prob - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-linked-list-pointers-prob\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Linked List pointers prob - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] There are several issues: Instead of calling initialisation(&amp;LBO); which is not really wrong, just write: LBO = NULL; Then don&#8217;t hide pointers with typedefs, it only adds confusion. Instead of: typedef struct noeud { int adresse, taille, temp; struct noeud* suivant; } *liste; Write: struct noeud { int adresse, taille, temp; struct noeud* suivant; ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-linked-list-pointers-prob\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-21T22:56:14+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-linked-list-pointers-prob\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-linked-list-pointers-prob\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Linked List pointers prob\",\"datePublished\":\"2022-10-21T22:56:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-linked-list-pointers-prob\\\/\"},\"wordCount\":126,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"c++\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-linked-list-pointers-prob\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-linked-list-pointers-prob\\\/\",\"name\":\"[Solved] Linked List pointers prob - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2022-10-21T22:56:14+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-linked-list-pointers-prob\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-linked-list-pointers-prob\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-linked-list-pointers-prob\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Linked List pointers prob\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Linked List pointers prob - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-linked-list-pointers-prob\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Linked List pointers prob - JassWeb","og_description":"[ad_1] There are several issues: Instead of calling initialisation(&amp;LBO); which is not really wrong, just write: LBO = NULL; Then don&#8217;t hide pointers with typedefs, it only adds confusion. Instead of: typedef struct noeud { int adresse, taille, temp; struct noeud* suivant; } *liste; Write: struct noeud { int adresse, taille, temp; struct noeud* suivant; ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-linked-list-pointers-prob\/","og_site_name":"JassWeb","article_published_time":"2022-10-21T22:56:14+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-linked-list-pointers-prob\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-linked-list-pointers-prob\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Linked List pointers prob","datePublished":"2022-10-21T22:56:14+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-linked-list-pointers-prob\/"},"wordCount":126,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-linked-list-pointers-prob\/","url":"https:\/\/jassweb.com\/solved\/solved-linked-list-pointers-prob\/","name":"[Solved] Linked List pointers prob - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-21T22:56:14+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-linked-list-pointers-prob\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-linked-list-pointers-prob\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-linked-list-pointers-prob\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Linked List pointers prob"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/16787","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=16787"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/16787\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=16787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=16787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=16787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}