Saturday, July 2, 2016

How does structure differ from array ? Q.no 9 2068 shrawan IOE

Q. How does structure differ from array ?

Array:
- Array is collection of similar data types.
- Each array element is accessed by array name followed by index with square brackets [] as:
    array_name[index];
- Array is declared with fixed size which it allocates some space in memory.
example:

#include<stdio.h>
int main() {
int a[10],i;
printf("\nEnter array Elements: ");
for (i=0;i<10;i++)
{
scanf("%d",&a[i];
}
printf("\nArray elements are : ");

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

Structure:
-Structure is collection of similar and dissimilar data types.
- Each structure element is accessed by using a dot operator(.) as :
  structure_name.structure_element
- Before declaring a structure we have to define a structure data type along with its elements to inform compiler about it.
example:

struct students {
char name [20];
int roll;
};
void main (){

struct students s;
printf("Enter name and roll number of student:");
scanf("%s%d",s.name,&s.roll);
printf("\n Name: %s \t Roll no : %d",s.name,s.roll);
}

No comments:

Post a Comment