{"id":30663460,"url":"https://github.com/simpsonresearch/carray","last_synced_at":"2025-08-31T17:11:27.864Z","repository":{"id":186732784,"uuid":"675653722","full_name":"simpsonresearch/carray","owner":"simpsonresearch","description":"Pointer and non-pointer array strcture in C","archived":false,"fork":false,"pushed_at":"2023-08-07T12:20:11.000Z","size":10,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-08-11T15:30:02.283Z","etag":null,"topics":["array","c","pointers","structs"],"latest_commit_sha":null,"homepage":"https://simpson-computer-technologies-research.github.io/carray/","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/simpsonresearch.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}},"created_at":"2023-08-07T12:18:27.000Z","updated_at":"2023-08-07T12:19:56.000Z","dependencies_parsed_at":null,"dependency_job_id":"dc96a421-720f-4a0a-ae8e-b12fdf2bf7c0","html_url":"https://github.com/simpsonresearch/carray","commit_stats":null,"previous_names":["simpson-computer-technologies-research/carray","simpsonresearch/carray"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/simpsonresearch/carray","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simpsonresearch%2Fcarray","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simpsonresearch%2Fcarray/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simpsonresearch%2Fcarray/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simpsonresearch%2Fcarray/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simpsonresearch","download_url":"https://codeload.github.com/simpsonresearch/carray/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simpsonresearch%2Fcarray/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273010994,"owners_count":25030369,"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","status":"online","status_checked_at":"2025-08-31T02:00:09.071Z","response_time":79,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["array","c","pointers","structs"],"created_at":"2025-08-31T17:11:24.041Z","updated_at":"2025-08-31T17:11:27.858Z","avatar_url":"https://github.com/simpsonresearch.png","language":"C","readme":"# carray\nPointer and non-pointer array strcture in C\n\n```C\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n\n/**\n * Array structure\n */\ntypedef struct Array\n{\n    int *values;\n    size_t size;\n} Array;\n\n/**\n * Pointer array function prototypes\n */\nArray *new_array_ptr(size_t n);\nvoid fill_array_ptr(Array *arr);\n\n/**\n * Non-pointer array function prototypes\n */\nArray new_array(size_t n);\nvoid fill_array(Array arr);\nvoid free_array(Array arr);\n\n/**\n * Main function\n */\nint main(void)\n{\n    /**\n     * Array of integers\n     */\n    int arr[] = {};\n\n    /**\n     * Pointer array\n     */\n    Array *array = new_array_ptr(10);\n    fill_array_ptr(array);\n\n    if (array)\n    {\n        for (int i = 0; i \u003c array-\u003esize; i++)\n        {\n            printf(\"%d \", array-\u003evalues[i]);\n        }\n        free(array);\n    }\n\n    // Print a new line\n    printf(\"\\n\");\n\n    /**\n     * Non-pointer array\n     */\n    Array array2 = new_array(10);\n    fill_array(array2);\n    for (int i = 0; i \u003c array2.size; i++)\n    {\n        printf(\"%d \", array2.values[i]);\n    }\n    free_array(array2);\n\n    /**\n     * Which one is better and safer?\n     * \n     * The non-pointer array is better and safer because it is\n     * easier to use and it is less likely to cause memory leaks.\n     * \n     * The pointer array is more flexible and can be used to\n     * create dynamic arrays.\n     * \n     * Although, freeing memory for the pointer array is much easier\n     * and is less likely to forget when to actually free the memory.\n     * \n    */\n\n    // Return the main fucntion\n    return 0;\n}\n\n/**\n * Initialize a new array pointer type\n */\nArray *new_array_ptr(size_t n)\n{\n    Array *new_arr = malloc(sizeof(Array));\n    if (new_arr)\n    {\n        new_arr-\u003esize = n;\n        new_arr-\u003evalues = malloc(n * sizeof(int));\n    }\n    return new_arr;\n}\n\n/**\n * Fill array with numbers from 1 to n\n */\nvoid fill_array_ptr(Array *array)\n{\n    if (array)\n    {\n        for (int i = 0; i \u003c array-\u003esize; i++)\n        {\n            array-\u003evalues[i] = i + 1;\n        }\n    }\n}\n\n/**\n * Create a new non-ptr array\n */\nArray new_array(size_t n)\n{\n    Array new_arr;\n    new_arr.size = n;\n    new_arr.values = malloc(n * sizeof(int));\n    return new_arr;\n}\n\n/**\n * Fill array with numbers from 1 to n\n */\nvoid fill_array(Array array)\n{\n    for (int i = 0; i \u003c array.size; i++)\n    {\n        array.values[i] = i + 1;\n    }\n}\n\n/**\n * Free the array\n */\nvoid free_array(Array array)\n{\n    free(array.values);\n}\n```\n\n# License\nMIT License\n\nCopyright (c) 2023 Simpson Computer Technologies Research\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimpsonresearch%2Fcarray","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimpsonresearch%2Fcarray","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimpsonresearch%2Fcarray/lists"}