Declaring arrays in c language without initial size -
write program manipulate temperature details given below.
- input number of days calculated. – main function
- input temperature in celsius – input function
- convert temperature celsius fahrenheit.- separate function
- find average temperature in fahrenheit.
how can make 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 %f",avg); getch(); } void input(int d) { int x=0; for(x=0;x<d;x++) { printf("input temperature in celsius #%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; } }
in c arrays , pointers closely related. in fact, design array syntax convention accessing pointer allocated memory.
so in c statement
anyarray[n]
is same as
*(anyarray+n)
using pointer arithmetic.
you don't have worry details make "work" designed intuitive.
just create pointer, , allocate memory , access array.
here examples --
int *temp = null; // our array // allocate space 10 items temp = malloc(sizeof(int)*10); // reference first element of temp temp[0] = 70; // free memory when done free(temp);
remember -- if access outside of allocated area have unknown effects.
Comments
Post a Comment