{"id":17464548,"url":"https://github.com/hqarroum/the-pointer-arithmetics-challenge","last_synced_at":"2025-07-05T13:34:44.902Z","repository":{"id":141119537,"uuid":"392263071","full_name":"HQarroum/the-pointer-arithmetics-challenge","owner":"HQarroum","description":"🙇‍♂️ Retrieving a pointer to a structure given a pointer to one of its member.","archived":false,"fork":false,"pushed_at":"2021-08-03T22:21:15.000Z","size":2990,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-02T08:17:42.465Z","etag":null,"topics":["c","pointer-arithmetics"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/HQarroum.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-08-03T09:32:37.000Z","updated_at":"2022-01-16T12:21:36.000Z","dependencies_parsed_at":null,"dependency_job_id":"3f6bfc11-c593-4cad-9860-d5a1475e8c14","html_url":"https://github.com/HQarroum/the-pointer-arithmetics-challenge","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HQarroum%2Fthe-pointer-arithmetics-challenge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HQarroum%2Fthe-pointer-arithmetics-challenge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HQarroum%2Fthe-pointer-arithmetics-challenge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HQarroum%2Fthe-pointer-arithmetics-challenge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/HQarroum","download_url":"https://codeload.github.com/HQarroum/the-pointer-arithmetics-challenge/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245985933,"owners_count":20705105,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","pointer-arithmetics"],"created_at":"2024-10-18T10:46:16.625Z","updated_at":"2025-07-05T13:34:44.897Z","avatar_url":"https://github.com/HQarroum.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\u003cp align=\"center\"\u003e\n  \u003cimg width=\"750\" src=\"assets/arithmetics.jpg\" /\u003e\n  \u003cbr\u003e\u003cbr\u003e\n  \u003ch2 align=\"center\"\u003eThe Pointer Arithmetics Challenge\u003c/h2\u003e\n  \u003cp align=\"center\"\u003eA \u003cem\u003ebyte-sized\u003c/em\u003e C coding brain teaser.\u003c/p\u003e\n  \u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/codespaces/new/HQarroum/the-pointer-arithmetics-challenge\"\u003e\u003cimg alt=\"Github Codespaces\" src=\"https://github.com/codespaces/badge.svg\" /\u003e\u003c/a\u003e\n  \u003c/p\u003e\n\u003c/p\u003e\n\u003cbr\u003e\n\n## 🚀 The Challenge\n\n\u003e I participated to a programming contest a few years ago, and came across an interesting challenge related to pointer arithmetic in C. I decided to create a write-up on the subject to expose the problem and the proposed solution.\n\nThe goal of the challenge is to retrieve the pointer to a structure given a pointer to one of its member. The challenge must be done independently of the number of members in the structure, the size of the structure, or the compiler which can vary during the test of implementation.\n\nFor the purpose of the challenge, we will consider the following structure of type `t_struct` which holds a float, an int (our member, aptly named member) and a char :\n\n```c\ntypedef struct {\n  float f;\n  int member;\n  char c;\n} t_struct;\n```\n\nWe need to provide a function that takes a pointer to a member of a structure and returns a pointer to the actual structure. The function must be prototyped as follow :\n\n```c\nt_struct* get_struct_ptr(void* member);\n```\n\nThe `get_struct_ptr` function is expected to be used as in the following example by the testing application provided during the contest.\n\n```c\nint main(void) {\n  t_struct  test = {\n    .f = 1.0,\n    .member = 42,\n    .c = 0x42\n  };\n  t_struct* ptr = get_struct_ptr(\u0026test.member);\n  return (ptr == \u0026test ? EXIT_SUCCESS : EXIT_FAILURE);\n}\n```\n\n## 💡 The Solution\n\n\u003e For a full explanation of the solution, please see the [deep-dive article on Medium](https://medium.com/@HalimQarroum/the-pointer-arithmetics-challenge-42c5a8d58314).\n\nThis repository contains a working solution that should be working with all ANSI/C compilers. It currently has been tested on `GCC 10` and `Clang 12`.\n\n## 👀 See also\n\n- The [`offsetof()`](http://en.wikipedia.org/wiki/Offsetof) macro.\n- The [deep-dive article on Medium](https://medium.com/@HalimQarroum/the-pointer-arithmetics-challenge-42c5a8d58314).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhqarroum%2Fthe-pointer-arithmetics-challenge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhqarroum%2Fthe-pointer-arithmetics-challenge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhqarroum%2Fthe-pointer-arithmetics-challenge/lists"}