11 Replies - 1106 Views - Last Post: 24 May 2013 - 10:50 AM Rate Topic: -----

#1 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • 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  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • 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]);

}

Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3034
  • Posts: 10,598
  • 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.
Was This Post Helpful? 1
  • +
  • -

#4 ValenL  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 810
  • View blog
  • Posts: 2,407
  • 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

Was This Post Helpful? 3
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,564
  • 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

Was This Post Helpful? 2
  • +
  • -

#7 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 810
  • View blog
  • Posts: 2,407
  • Joined: 20-March 10

Re: Initialising int array of unknown size

Posted 19 May 2013 - 01:51 PM

View Postjjl, 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

Was This Post Helpful? 0
  • +
  • -

#8 lewm  Icon User is offline

  • D.I.C Head

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

Re: Initialising int array of unknown size

Posted 19 May 2013 - 02:08 PM

View PostCTphpnwb, 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

Was This Post Helpful? 0
  • +
  • -

#9 lewm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • 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);

Was This Post Helpful? 0
  • +
  • -

#10 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,564
  • 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
Was This Post Helpful? 1
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,761
  • Joined: 16-October 07

Re: Initialising int array of unknown size

Posted 20 May 2013 - 06:35 AM

View Postlewm, on 19 May 2013 - 05:21 PM, said:

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


You kind of already showed that. Instead of:
for(i=0;i<howm;i++)
{
    a[i]=i;
    printf("%d\n", a[i]);
}



How about:
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);


Was This Post Helpful? 1
  • +
  • -

#12 lewm  Icon User is offline

  • D.I.C Head

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

Re: Initialising int array of unknown size

Posted 24 May 2013 - 10:50 AM

Thanks guys :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1