C Array - Part 1 | C Language Tutorial | Mr. Srinivas

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi everyone welcome to nourish technologies this is xenoverse today we are going to discuss so very important concept in a c language so that is arrays first one what is array what is array array is an aggregate data type or we can call it as a derived data type also what is the main use of arrays so why we are using arrays in a c language just consider this consider take one primitive data type variable in teaser a this is primitive variable declaration a gets memory allocation somewhere a gets memory allocation and next we are assigning one value to a for example 10 we are assigning to a 10 we are assigning to a the 10 will go on store into a into a for example we are trying to store another value 20 into a can we store the next value of course we can but that will replace the existing value 10 value is replaced with the new value 20 so that is the main problem using one primitive variable we can store only one value at a time multiple values we cannot store that is impossible case this is the main disadvantage of a primitive data type so then how can we rectify the problem so I want to store more than one element for example students marks I want to store or for example mobile numbers I want to store write employee numbers we want to store some student IDs I want to store then how can we store sir if hundred students are there to store ID number of each student 100 variables we have to declare is that possible impossible very complex declaration of 100 variables and working with 100 variables one by one individually is very very complex task in a programming language so that is why they introduced the concept of arrays using an array we can store n number of elements more than one element right using a single array variable using a single array variable we can store n number of elements but of same data-type only homogeneous data elements allowed into eros either all the elements are integer type either all the elements are flow type either all elements are double type only homogeneous elements allowed that that is called array how the declaration will be syntax a declaration and some examples we will see declaration of array here this is a syntax how to declare array in the application so generally how to declare a variable parallel you will see example also then you will understand more clearly I am declaring int int a a is a identity integer datatype a is a identity int is a data type this is just declaration of variable already we know now the new thing is added here you have to specify how many elements you want to store what is the size of array here along with the identity you should specify the size of the array this is a new thing added to the syntax of the variable okay now a is a array variable how the memory allocation will be simple a block of memory will be allocated block of memory five locations because the size is a five and array elements always we can process with the help of index number index number starts with a zero to size minus 1 0 1 2 3 4 because size is a file size minus 1 Phi minus 1 is a 4 and here it is a memory block starting address is the random address only 2 0 4 6 consider this array address a will hold a is an array variable right array variable always holds the base address of the memory block so that it will pointing sir here it is only pointers hold addresses but here it is a variable is hold the address where is the pointer in the declaration sir pointer means asterisk so nothing but a star where is the pointers are here array variable is called internal pointer variable what is this internal pointer variable it holds the base address of the array it holds the base address of the array so remaining blocks addresses integer occupies 2 bytes consider so each element occupies two bytes so first one two bytes 2 0 4 6 & 4 7 next one two zero four eight two zero five zero two zero five two two zero five four total 10 bytes memory will be allocated to this array because it is storing five integer elements it is storing five integer elements so 5 into 2 10 bytes memory a block will occupy array elements we can access using its index number only very very important thing and next one array variable always holds the base address of the memory block base address means what the block main address is called base address who will hold that base address variable a array variable holds the base address right so that is why it is called internal pointer variable because pointer variables only holds the base address of the blocks okay memory address of the block so this is a declaration example and how the memory will be allocated and next one local declarations and global declarations of array for example if you take a general variable primitive variable a primitive variable you can declare either globally or locally so locally means what just declaration inside a block or method a global means what outside to all the methods is called a global declaration right how we are declaring a general variables a globally and locally array variables also we can declare in the same way suppose first we will see local declarations local declarations inside the method here at the time of declaration if you want to provide some elements if you want to assign some values into the array directly you can assign using assignment operator nothing but erase we can initialize directly chances are there suppose here I am declaring array int a RR into ARR and the size is a 5 size is a 5 suppose if you are not initializing with any values here all the locations are initializes with Ag our base values all these locations initializes with a garbage values why reasons already we know local variables in C language initialized with agar base values if you don't initialize with any manual value is a garbage value for example sir I am declaring like this int a RR into arr this is providing initialization here values 10 20 30 40 50 then five locations initializes with the five values 10 20 30 40 50 this is direct initialization using assignment operator and by using braces all the elements of array we can initialize directly that is a possibility next for example I am declaring array array but I am initializing only two values only two values I am assigning sir what about the remaining values remaining values 10 20 remaining all the elements are initializes with the zeros difference is there just observe carefully first a declaration and the third declaration in the first declaration if you are not initializing it list with one element all the elements are initializes with the garbage values but in the third case if you are assigning only two elements or what about the remaining three elements remaining three locations initializes with zeros initializes with zeros that is important so global declaration global declarations we can declare an array globally also outside to all the methods global variable means a declaration of a variable outside to all the functions in the program so here I am declaring one array I am NOT initializing with anywhere you but no problem all the five locations initializes with the zeros because a global variable a default integer value is a zero that we already discussed in a variables concept okay so this is the global Declaration and local Declaration of arrays okay so now we will see how to process the elements of arrays this is also very very important how to process the elements of array the thing is using iterators sir what is an iterator it is a loop so why we are using array elements mean processing array elements using iterator sir simple reason suppose array size is 100 if you want to read one element one time you are writing a scanner function if you want to print one element so one time you will write a printf and scanf happy suppose in the array hundred elements you have to read and hundred elements you have to print them what you will do will you write a hundred times a printf function and 100 times a scanner function impossible case we cannot define write 100 times printf 100 times the scanf instead of that it is better to go for a loop based on index of the array how very simple this is array this is array suppose size is a 5 consider so index is a 1 2 3 4 starts with a 0 this is ARR of 5 so what type integer type consider integer addresses very very important to zero four six two zero four eight two zero five zero two zero five two two zero five four addresses how to read and how to print the element for example we are repeating the for loop this is for purpose we are reading elements reading purpose we are using for loop for index number starts with a zero index number starts with zero up to less than size because up to four I is less than five location by location one by one it is increasing scanf i want to read element by element only one integer i want to read so one time format specifier of integer percentage D first iteration I value zero so zero through occasion address is very very important ARR of zero address yay RR off zero means what I address this is reading this is reading whenever I value zero whenever I value zero I equals to zero then a error of 0 address so what is that the error of 0 address to 0 for 6 so first element will be scanned first element will be read in 2 to 0 for 6 location when I equals to 1 to 0 for 8 1 address to 0 for 8 when I equals to 2 to 0 5 0 so like that every location address we are providing very clearly to read the elements in two locations into array for example they are providing the values 10 20 30 40 50 then we are reading 10 20 30 40 50 reading over next suppose if you want to print how to print elements of array here now this is printing of array same for loop we are using index number starts with a 0 to 5 0 to 5 you write so zero to five we are reading then how simply printf printf sir for printf so what is required location address or location for scanf we need to provide a dress for printf we need to provide location a error of zero location error of one location nothing but just simply a error of I enough the only difference between scanf and the printf is in case of scanf we need to provide address of each location in case of printf directly we are providing the location either you want to read or you want to print or you you want to process the elements of array it is better to use iterators concept it is better to use any loop either while loop do-while loop or for loop so that easily we can process the elements okay in the next session so we will see the more technical examples and more interview point of view questions on arrays so this is all about this session for more videos so please subscribe to nourish id channel thank you [Music]
Info
Channel: Naresh i Technologies
Views: 1,186,463
Rating: 4.8815975 out of 5
Keywords: C Language, Arrays in C, Srinivas, Naresh IT, Hands on C Language Training, C Language Demo, Online C Language Training, C Language Tutorial Videos, C Language Overview, C Language Interview Questions, array in c language, array in c programming tutorial, c arrays, c array program, c srinivas, naresh it c
Id: SPuS9UJF1lo
Channel Id: undefined
Length: 17min 33sec (1053 seconds)
Published: Mon Sep 19 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.