Question: I am trying to display the correct button and link depending on what device the user browses to a page on. If android display android app store button if ios display apple store button.

I cannot seem to get it to swap at all even though im making the function default to swap it from android to apple button.

Here is the code:

```
<html>
<head>
    <script type="text/javascript">

        var uagent = navigator.userAgent.toLowerCase();
        var apple = "/images/ios.jpg";
        var android = "/images/android.jpg"

        function DetectDevice() {
            var but = document.getElementById("AppButton");
            if(useragent.search("iphone") || useragent.search("ipod") || useragent.search("ipad")) {
                toswap.src = apple;
                alert("apple");
            } else if(useragent.search("android")) {
                toswap.src = android;
                alert("android");
            }
            but.src = apple;
        }
        DetectDevice();
    </script>

    <title>Device Detection</title>
</head>

<div id="butttons">
    <img src="images/android.jpg" name = "AppButton" id="AppButton"/>
</div>

```

Answer: 
Keep in mind, I do NOT program gmp, but a tail-dynamic buffer in a struct is usually implemented something like this (adapted to how I think you want to use it):

```
typedef struct
{
  size_t length;  //length of the array
  size_t numbits; //number of bits allocated per val in vals
  mpz_t vals[1];  //flexible array to hold some number of mpz_t array
} CoolArray;

```

Allocation strategy, knowing the the number of values and bit-depth, would be:

```
CoolArray* allocArray(size_t length, size_t numbits)
{
   CoolArray *p = malloc(sizeof(*p) + sizeof(mpz_t)*length);
   p->length = length;
   p->numbits = numbits;
   mpz_array_init(p->vals, length, numbits);
   return p;
}

```

Freeing it (just a wrapper for free(), but you may need to do some gmp-cleanup I'm unfamiliar with):

```
void freeArray(CoolArray **pp)
{
    if (*pp)
    {
        free(*pp);
        *pp = NULL;
    }
}

```

Using it:

```
CoolArray *pca = allocArray(length, numbits);

```

Freeing it when done:

```
freeArray(&pca);

```

These are just ideas, but maybe you can get something from them.
If you're using a flexible array member, you need to allocate the struct in one go:

```
CoolArray *array = malloc(sizeof(CoolArray) + length * sizeof(mpz_t));
mpz_array_init(array->vals, length, numbits);

```