# Initialising int array of unknown size

Page 1 of 1

## 11 Replies - 1831 Views - Last Post: 24 May 2013 - 10:50 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=321385&amp;s=4710ce0cb5857b703e305930a5db9201&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lewm

Reputation: 7
• Posts: 160
• Joined: 29-March 13

# Initialising int array of unknown size

Posted 19 May 2013 - 10:22 AM

```int main()
{
int a[]={0};

a[0]=1;
a[1]=1;
a[2]=1;

printf("%d %d %d %d", a[0], a[1], a[2], a[3]); //Here is an extra array element 'a[3]' i have not given a value to//
//I thought i had set the array element values to NULL 'a[]={0}'//
getch();
return 0;
}
```

I get the output 1 1 1 "rubbish".
Why? I set a[]={0}.

Is This A Good Question/Topic? 0

## Replies To: Initialising int array of unknown size

### #2 ValenL

Reputation: 3
• Posts: 26
• Joined: 26-March 13

## Re: Initialising int array of unknown size

Posted 19 May 2013 - 12:30 PM

Hi, I am learning C as well and I'm here to give an answer to your question hopefully I am correct.

a[]={0}
You are declaring the array with nothing in it.

you can easily just do a[] = {1, 2, 3}

```int main()
{
int a[] = {1, 2, 3};
printf("%i %i %i", a[0], a[1], a[2]);

}
```

### #3 CTphpnwb

• D.I.C Lover

Reputation: 3466
• Posts: 12,349
• Joined: 08-August 08

## Re: Initialising int array of unknown size

Posted 19 May 2013 - 12:30 PM

If you want to use variable length arrays you need to either learn to use pointers and dynamic allocation or if you're using C++ you can use vectors.

### #4 ValenL

Reputation: 3
• Posts: 26
• Joined: 26-March 13

## Re: Initialising int array of unknown size

Posted 19 May 2013 - 12:32 PM

sorry for the double post..... but theres a 0 in the array

### #5 snoopy11

• Engineering ● Software

Reputation: 1127
• Posts: 3,371
• Joined: 20-March 10

## Re: Initialising int array of unknown size

Posted 19 May 2013 - 01:16 PM

I take it this is c,

C manages memory in 3 different ways statically, automatically or dynamically.

static memory the size is known at design time and you would just use a[25];
or something similar static memory is held in main memory.

automatic memory is held on the stack and you have declared your array a[] on the stack. This is ok for small arrays but you will quickly blow your stack for large arrays.

This is why dynamic memory is needed

dynamic memory in C is declared in two ways with malloc or calloc

```int *a;

a= malloc(arraysize*sizeof(int*));
```

```a= calloc(arraysize,sizeof(int*));
```

calloc basically zeroes the memory block for you while malloc does not.

Why you are getting rubbish is because you never set the value of a[3] and you are just getting a garbage value that is held at that address.

consider this small program.

```#include <stdio.h>
#include <stdlib.h>
#define arraysize 10
int main()
{
int i = 0;
int *a;

a= malloc(arraysize*sizeof(int));
a[0] =10;
a[1] =11;
for(i=0;i<10;i++)
printf("%d\n",a[i]);

free(a);

printf("\nUse of calloc to zero block of memory\n");
a= calloc(arraysize,sizeof(int));
a[0] =10;
a[1] =11;
for(i=0;i<10;i++)
printf("%d\n",a[i]);

free(a);
return 0;
}

```

The first ten values contain garbage values with malloc but are zeroed with calloc.

Note the use of free which deallocates the memory block which is allocated on the heap.

Regards

Snoopy.

This post has been edited by JackOfAllTrades: 20 May 2013 - 04:59 AM
Reason for edit:: Changed int * to int

### #6 jjl

• Engineer

Reputation: 1169
• Posts: 4,785
• Joined: 09-June 09

## Re: Initialising int array of unknown size

Posted 19 May 2013 - 01:28 PM

Quote

``` a= malloc(arraysize*sizeof(int*));

```

Snoopy, I think you meant sizeof(int). sizeof(int) does not always equal sizeof(int*)

This post has been edited by jjl: 19 May 2013 - 01:29 PM

### #7 snoopy11

• Engineering ● Software

Reputation: 1127
• Posts: 3,371
• Joined: 20-March 10

## Re: Initialising int array of unknown size

Posted 19 May 2013 - 01:51 PM

jjl, on 19 May 2013 - 08:28 PM, said:

Snoopy, I think you meant sizeof(int). sizeof(int) does not always equal sizeof(int*)

Yeah sorry

should be sizeof(int)

Agreed.
Getting tired.
Snoopy.

MOD NOTE: Fixed it for you.

This post has been edited by JackOfAllTrades: 20 May 2013 - 04:59 AM

### #8 lewm

Reputation: 7
• Posts: 160
• Joined: 29-March 13

## Re: Initialising int array of unknown size

Posted 19 May 2013 - 02:08 PM

CTphpnwb, on 19 May 2013 - 12:30 PM, said:

If you want to use variable length arrays you need to either learn to use pointers and dynamic allocation or if you're using C++ you can use vectors.

Like this?
```int main()
{
int *a;
int howm=0, i=0;

puts("How many?");
scanf("%d", &howm);

a=malloc(howm*sizeof(int));
for(i=0;i<howm;i++)
{
a[i]=i;
printf("%d\n", a[i]);
}
getch();
free(a);

return 0;
}
```

This seems to work ok.

This post has been edited by lewm: 19 May 2013 - 02:55 PM

### #9 lewm

Reputation: 7
• Posts: 160
• Joined: 29-March 13

## Re: Initialising int array of unknown size

Posted 19 May 2013 - 02:21 PM

How would you print out the entire contents of the array at once. This doesnt work:
```printf("%d", a);
```

### #10 jjl

• Engineer

Reputation: 1169
• Posts: 4,785
• Joined: 09-June 09

## Re: Initialising int array of unknown size

Posted 19 May 2013 - 03:29 PM

Loop over each array index and call print for each element

### #11 baavgai

• Dreaming Coder

Reputation: 6601
• Posts: 13,941
• Joined: 16-October 07

## Re: Initialising int array of unknown size

Posted 20 May 2013 - 06:35 AM

lewm, on 19 May 2013 - 05:21 PM, said:

How would you print out the entire contents of the array at once.

```for(i=0;i<howm;i++)
{
a[i]=i;
printf("%d\n", a[i]);
}

```

```for(i=0;i<howm;i++)
{
printf("%d\n", a[i]);
}

```

Even better, put this in a function so you can always print an array the way you like. e.g.
```void printArray(int *a, int size);

```

### #12 lewm

Reputation: 7
• Posts: 160
• Joined: 29-March 13

## Re: Initialising int array of unknown size

Posted 24 May 2013 - 10:50 AM

Thanks guys