Posts

Showing posts with the label array

Basics of Pointers In C

Image
Pointers in C A pointer is a variable which contains the address of the variable specified. for example:  int x = 100; int *data; data = &x; this variable data contains the address of the block in the memory which contains the value 100. * specifies that this is the pointer variable. & gives the address. that is the what I am doing here assigning the address of the integer variable x to the pointer variable data. to get the data from the pointers we have to use the * sign. for the previous example to get the data of the memory block of the printf("%d\n",*data );  this should print the output like the image. Further digging into deep for the pointers. we can use them for the array. if we change content in the memory block pointed by the data, then this change should reflect the in the content of the x. Because variable x has the content of that memory block. (this is the great advantage of the using the pointers) we can read, change anything without copying the data fr

How to Get the length of an Array in C

Image
Array in C an array is a collection of a variable of a fixed number of the same data type in a sequential memory. an array can be declared in various ways. one of them is given below int x[] = {200,300,522,655}; char y[] = {'x','s','r'}; int n[10],j; // first declaring the array // then assigning the value. for ( j = 0; j < 10; j++ ) { n[ j ] = j + 100; } indexing in C array starts from the 0. so if you want to access the 4th element then you have to use x[3]. that's enough intro about the array. Getting Length of the Array in C to get the length of the array first we divide the size of the full array by the size of the one element of the array. Please note there is another way of getting the length of the char array (you can use strlen). you can see this in the given program. #include <stdio.h> int main(){ int x[] = {200,300,522,655}; char y[] = {'x','s','r'}; // determining the size of the array // getting the