Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring arrays in c language without initial size

Tags:

arrays

c

function

Write a program to manipulate the temperature details as given below.
- Input the number of days to be calculated. – Main function
- Input temperature in Celsius – input function
- Convert the temperature from Celsius to Fahrenheit.- Separate function
- find the average temperature in Fahrenheit.

how can I make this program without initial size of array ??

#include<stdio.h>
#include<conio.h>
void input(int);
int temp[10];
int d;
void main()
{
    int x=0;
    float avg=0,t=0;
    printf("\nHow many days : ");
    scanf("%d",&d);
    input(d);
    conv();
    for(x=0;x<d;x++)
    {
        t=t+temp[x];
    }
    avg=t/d;
    printf("Avarage is %f",avg);
    getch();
}
void input(int d)
{
    int x=0;
    for(x=0;x<d;x++)
    {
        printf("Input temperature in Celsius for #%d day",x+1);
        scanf("%d",&temp[x]);
    }
}
void conv()
{
    int x=0;
    for(x=0;x<d;x++)
    {
        temp[x]=1.8*temp[x]+32;
    }
}
like image 266
ViSTA Avatar asked Jun 29 '13 15:06

ViSTA


People also ask

Can you declare an array without size?

Answer: No. It is not possible to declare an array without specifying the size. If at all you want to do that, then you can use ArrayList which is dynamic in nature.

How do you declare an array of unknown size?

int[] integers; int j = 0; do { integers = new int[j + 1]; integers[j] = in. nextInt(); j++; } while((integers[j-1] >= 1) ||(integers[j-1]) <= 100); java.

How can I get array elements without knowing the size in C?

int reqArraySize; printf("Enter the array size: "); scanf("%d", &reqArraySize); After this you may proceed with this interger input array size : for(i=0;i<reqArraySize;i++) scanf("%d",&arr[i]); Hope this helps you.


2 Answers

In C arrays and pointers are closely related. In fact, by design an array is just a syntax convention for accessing a pointer to an allocated memory. *(see note for more details below)

So in C the statement

 anyarray[n] 

is the same as

 *(anyarray+n)

Using pointer arithmetic.

You don't really have to worry about the details to make it "work" as it is designed to be somewhat intuitive.

Just create a pointer, and allocate the memory and then access it like as an array.

Here is some examples --

int *temp = null; // this will be our array


// allocate space for 10 items
temp = malloc(sizeof(int)*10);


// reference the first element of temp
temp[0] = 70;


// free the memory when done
free(temp);

Remember -- if you access outside of the allocated area you will have unknown effects.

  • To be clear it is the indexing operator ([ ]) that is translated to pointer arithmetic. This is not an array in the modern sense of the type. Whether (or not) the pointer involved points to (dynamically) allocated memory is inconsequential to how this operator works. In a more modern language you would be able to operate on the array as an abstract type (to see how big it is, for example), you can't do this in C.
like image 86
Hogan Avatar answered Sep 28 '22 19:09

Hogan


An array without an initial size is basically just a pointer. In order to dynamically set the size of the array, you need to use the malloc() or calloc() functions. These will allocate a specified amount of bytes of memory.

In your code above, declare temp as an int pointer

int *temp;

Then allocate space for it using malloc() or calloc(). The argument that these functions take is is the number of bytes of memory to allocate. In this case, you want enough space for d ints. So...

temp = malloc(d * sizeof(int));

malloc returns a pointer to the first byte in the block of memory that was just allocated. Regular arrays are simply pointers to the first byte in a sectioned off block of memory, which is exactly what temp is now. Thus, you can treat the temp pointer as an array! Like so:

temp[1] = 10;
int foo = temp[1];
printf("%d", foo);

Outputs

10
like image 43
btidwell Avatar answered Sep 28 '22 19:09

btidwell