0% found this document useful (0 votes)
3 views22 pages

Understanding Arrays in C Programming

The document provides an overview of arrays in programming, including their declaration, initialization, and memory allocation. It discusses accessing array elements, the lack of bounds checking, and how arrays can be passed to functions. Additionally, it covers character arrays, multidimensional arrays, and includes quizzes and a mini project related to creating a student academic database using arrays.

Uploaded by

regisanne
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views22 pages

Understanding Arrays in C Programming

The document provides an overview of arrays in programming, including their declaration, initialization, and memory allocation. It discusses accessing array elements, the lack of bounds checking, and how arrays can be passed to functions. Additionally, it covers character arrays, multidimensional arrays, and includes quizzes and a mini project related to creating a student academic database using arrays.

Uploaded by

regisanne
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Arrays

Arrays
Array is a collection of similar data types on
continous memory locations.
declaration:
int arr[10];//allocates 10 locations
int arr[]={10,20,30};//initialized
int arr[5]={10,20,30};
//last 2 locations contains 0
accessing array elements
int arr[5];
arr[0]=15;
arr[1]=25;
arr[5/2]=35;//valid
printf(“%d%d”,arr[0],arr[2]);
output:15 35
no bounds checking
accessing the array out side of its limit will
not result in any error but it will produce
unexpected value
int a[]={3,4,5};
printf(“%d”,a[6]);//gives garbage value
initializing more than limit
int a[3]={1,2,3,4,5};
//this is invalid
a[-2]=35;
//is valid since in stores the value from base
address -2
Arrays
it is possible to declare array of all data types but
void cannot be used
void arr[5];
//invalid
memory allocation
local arrays are allocated memory on stack
segment
static or global arrays are allocated memory in
data segment
dynamic arrays are allocated in heap segment
passing array
arrays are always passed as pointers to functions
int arr[]={1,2,3};
fun(arr);//function call
void fun(int s[]) //function definition
{
}
this is similar to
void fun(int *ptr)
{
}
character array
when character array is used last character
is ’\0’ null value
char a[]=“welcome”;
printf(“%d”,sizeof(a));
//output:8
last location is used to store ‘\0’
single quote and double quote
character array
char arr[]=“program”;
printf(“%d”,sizeof(arr));
//output:[Link] additional space created to
store ‘/0’
char arr[7]=“program”;
//no additional space created
char arr[]={‘p’,’r’,’o’,’g’,’r’,’a’,’m’};
//no additional space created
partial initialization of arrays
int a[5]={10,20};
printf(“%d”,a[3]);
output:0
since the other locations except first and
second are initialized to 0
rules for multidimesion array
the left most dimension is [Link] left most
every other dimension should be specified
int a[][2]={ {1,2},{3,4}}; //valid
int a[][][2]={{1,2},{3,4}
{4,5},{5,6}};
//invalid.
it is also invalid to skip the second dimension
int a[2][];//invalid
understanding arrays
int a[5]={10,20,30,40,50};
printf(“%d%d”,a[0],*(a+0));
output:10 10
a[0] and *(a+0) both are equivalent
understanding arrays
int a[5]={10,20,30,40,50};
int *p=a+2;
printf(“%d”,*p);
output:30
understanding arrays
int a[5]={10,20,30,40};
int *ptr=&a[2];
printf(“%d %d %d”,ptr[1],ptr[-1],ptr[-2]);
output:40 20 10
quiz
int main(void)
{
char a[]=“winter”;
printf(“%c”,a[6]);
return 0;
}
a)garbage value
b)r
c)-1
d)no output(\0)
quiz
int main(void)
{
int a[][2]={{1,2},{3,4}};
printf(“%d”,a[1][1]);
return 0;
}
a)3
b)4
c)2
d)1
quiz
which of the following are valid declaration
of array
a)int a[5];
b)int a[2][3];
c)int a[2][];
d)int a[][3];
quiz
int main(void)
{
int a[4]={10,20,30,40};
int *ptr=a;
printf(“%d”,ptr[1]);
return 0;
}
a)10
b)20
c)error
quiz
int main(void)
{
int a[3]={10,20};
printf(“%d”,a[2]);
return 0;
}
a)0
b)garbage value
c)error
quiz
int main(void)
{
int x[4]={10,20,30,40};
int *ptr=x;
ptr+=3;
ptr--;
printf(“%d”,*ptr);
return 0;
}
a)30
b)40
c)20
d)error
quiz
int main(void)
{
int a[3];
a[6/3]=5;
a[2/3]=4;
a[1]=3;
printf(“%d%d”,a[0],a[2]);
return 0;
}
a)3 4
b)4 5
c)3 5
d)error
mini project
create a student academic database such as
regno,mark1,mark2,mark3,total,percentage using array
input:
enter regno:101
output:
regno mark1 mark2 mark3 total percentage
101 50 60 70 180 60

You might also like