# C:  lenght of array

**URL:** <https://community.unix.com/t/c-lenght-of-array/328751>\
**Category:** Programming\
**Created:** [May 17, 2013, 7:43pm UTC](https://community.unix.com/t/c-lenght-of-array/328751 "2013-05-17T19:43:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tornow](https://community.unix.com/letter_avatar/tornow/32/5_5575768a8748004e209b776fc1b2916d.png) [@tornow](https://community.unix.com/u/tornow)\
**Post date:** [May 17, 2013, 7:43pm UTC](https://community.unix.com/t/c-lenght-of-array/328751/1 "2013-05-17T19:43:56Z")

</div>

Doing some training code with arrays i run into the following issue: If i ask the user how long the array is supposed to be, malloc it and then ask for the values inside the array and return the list to main and assing it to a pointer to list of integers:

```nohighlight
#include <stdio.h>
#include <stdlib.h>

int *make_list(void); 
void get_num(int *); 
void print_reverse(int *, int); 

int main(void)
{
    int *v = NULL; 

    
    v = make_list(); 

    return 0; 
}

int *make_list(void)
{
    int n = 0; 
    int i = 0; 
    int *tmp = NULL; 
    
    printf("how long? "); 
    get_num(&n); 
    tmp = (int *) malloc(n * sizeof(int)); 

    for (i = 0; i < n; i++) {
        printf("value "); 
        get_num(tmp+i); 
    }
    return tmp; 
}

void get_num(int *n)
{
    char buf[BUFSIZ]; 

    printf("number: "); 
    fgets(buf, sizeof(buf), stdin); 
    *n = atoi(buf); 
}

```

i lost all ways to find out how much elements that list has, right?

And the other way around: If i have a list of integers and add it as an argument to a fucntion:  
print\_list(v)  
there is no way to find out how long the list is inside the function called print\_list() ?

If both is true, would using a global variable for the length be a good solution?

---

<div class="post-metadata">

**Author:** ![migurus](https://community.unix.com/letter_avatar/migurus/32/5_5575768a8748004e209b776fc1b2916d.png) [@migurus](https://community.unix.com/u/migurus)\
**Post date:** [May 17, 2013, 9:41pm UTC](https://community.unix.com/t/c-lenght-of-array/328751/2 "2013-05-17T21:41:30Z")

</div>

Yes, you have to keep a variable wich keeps number of elements in your array.

In many situations I find it convenient to hold ptr to array and counter together in one simple structure. If element of an array is a struct itself then I add another int to my structure to hold sizeof of one element. This way you can have a lib function to init an array, to add an element to it etc...

---

<div class="post-metadata">

**Author:** ![tornow](https://community.unix.com/letter_avatar/tornow/32/5_5575768a8748004e209b776fc1b2916d.png) [@tornow](https://community.unix.com/u/tornow)\
**Post date:** [May 17, 2013, 10:24pm UTC](https://community.unix.com/t/c-lenght-of-array/328751/3 "2013-05-17T22:24:11Z")

</div>

Thanks for your answer. I like the idea.  
The following is how i implemented it, asking if that would be the way you proposed it:

```nohighlight
#include <stdio.h>
#include <stdlib.h>

struct numlist {
    int *v; 
    int n; 
}; 

struct numlist make_list(void); 
void print_list(struct numlist list); 
void get_num(int *); 

int main(void) 
{
    struct numlist numbers; 

    numbers = make_list(); 
    print_list(numbers);     

    return 0; 
}

struct numlist make_list(void)
{
    struct numlist tmp; 
    int n = 0; 
    int i = 0; 

    printf("list len "); 
    get_num(&n); 

    tmp.n = n; 
    tmp.v = (int *) malloc(tmp.n * sizeof(int)); 
    if ( tmp.v == NULL ) {
        fprintf(stderr, "memory error\n"); 
        exit(-1); 
    }

    for (i = 0; i < n; i++) {
          printf("list element "); 
          get_num(tmp.v + i); 
    }

    return tmp; 
}

void print_list(struct numlist list)
{
    int i = 0; 
    
    printf("\n\tresulting list\n"); 
    for (i = 0; i < list.n; i++) {
        printf("element with index %d has value: %d\n",
                i, list.v); 
    }
}

void get_num(int *x)
{
    char buf[BUFSIZ]; 

    printf("number: "); 
    fgets(buf, sizeof(buf), stdin); 
    *x = atoi(buf); 
}    

```

I mainly got two questions:  
1) I was not sure if to use a "struct numlist numbers" or a pointer to a numlist: "struct numlist \*numbers"

2) I am not sure if memory for the structure (numbers in main) is set up correctly.

---

<div class="post-metadata">

**Author:** ![migurus](https://community.unix.com/letter_avatar/migurus/32/5_5575768a8748004e209b776fc1b2916d.png) [@migurus](https://community.unix.com/u/migurus)\
**Post date:** [May 20, 2013, 12:55pm UTC](https://community.unix.com/t/c-lenght-of-array/328751/4 "2013-05-20T12:55:12Z")

</div>

I have no system available to test your code.

For your Q.1 I'd use "struct numlist \*numbers" in main and change make\_list to return pointer to structure.

The Q.2 is not clear to me, what does not look right to you?

---

<div class="post-metadata">

**Author:** ![tornow](https://community.unix.com/letter_avatar/tornow/32/5_5575768a8748004e209b776fc1b2916d.png) [@tornow](https://community.unix.com/u/tornow)\
**Post date:** [May 20, 2013, 1:48pm UTC](https://community.unix.com/t/c-lenght-of-array/328751/5 "2013-05-20T13:48:18Z")

</div>

> [@migurus](#):
>
> I have no system available to test your code.
> 
> For your Q.1 I'd use "struct numlist \*numbers" in main and change make\_list to return pointer to structure.
> 
> The Q.2 is not clear to me, what does not look right to you?

Q.1: ok, thanks. y

Q.2: Inside of the function make\_list() i declare a struct num\_list tmp, but i don't allocate any memory. I allocate memory to the list, depending how much elements it will contain. Will the struct num\_list tmp have enough memory to store the list, no matter how much elements the list contains? (The question reveals that memory allocation is not always clear to me).

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [May 20, 2013, 3:23pm UTC](https://community.unix.com/t/c-lenght-of-array/328751/6 "2013-05-20T15:23:20Z")

</div>

> [@tornow](#):
>
> Q.2: Inside of the function make\_list() i declare a struct num\_list tmp, but i don't allocate any memory. I allocate memory to the list, depending how much elements it will contain. Will the struct num\_list tmp have enough memory to store the list, no matter how much elements the list contains? (The question reveals that memory allocation is not always clear to me).

numlist holds two and only two things:  
1) A pointer to memory  
2) An integer

Does a pointer get bigger or smaller? No... A pointer is just a number, always the same size.

---

<div class="post-metadata">

**Author:** ![migurus](https://community.unix.com/letter_avatar/migurus/32/5_5575768a8748004e209b776fc1b2916d.png) [@migurus](https://community.unix.com/u/migurus)\
**Post date:** [May 20, 2013, 3:26pm UTC](https://community.unix.com/t/c-lenght-of-array/328751/7 "2013-05-20T15:26:31Z")

</div>

Your Q.2 is now clear - yes, the tmp structure has a pointer \*v which will hold address of the beginning of the array of integers, which you malloc'ed. Corona clarified it already.

---

<div class="post-metadata">

**Author:** ![tornow](https://community.unix.com/letter_avatar/tornow/32/5_5575768a8748004e209b776fc1b2916d.png) [@tornow](https://community.unix.com/u/tornow)\
**Post date:** [May 20, 2013, 3:57pm UTC](https://community.unix.com/t/c-lenght-of-array/328751/8 "2013-05-20T15:57:54Z")

</div>

Ok, thanks to both of you. And thanks to Corona668 for explaining it detailed.
