C Arrays nd pointer[1]
C Arrays nd pointer[1]
Arrays in C
An array is a variable that can store multiple values. For example, if you want
to store 100 integers, you can create an array for it.
int data[100];
dataType arrayName[arraySize];
For example,
float mark[5];
Declare an Array
Few keynotes:
• Arrays have 0 as the first index, not 1. In this example, mark[0] is the first
element.
• If the size of an array is n , to access the last element, the n-1 index is used. In
this example, mark[4]
Here, we haven't specified the size. However, the compiler knows its size is 5
as we are initializing it with 5 elements.
Initialize an Array
Here,
mark[0] is equal to 19
mark[1] is equal to 10
mark[2] is equal to 8
mark[3] is equal to 17
mark[4] is equal to 9
// Program to take 5 values from the user and store them in an array
// Print the elements stored in the array
#include <stdio.h>
int main() {
int values[5];
Output
Enter 5 integers: 1
-3
34
0
3
Displaying integers: 1
-3
34
0
3
Here, we have used a for loop to take 5 inputs from the user and store them
in an array. Then, using another for loop, these elements are displayed on the
screen.
Example 2: Calculate Average
#include <stdio.h>
int main() {
average = sum / n;
printf("Average = %d", average);
return 0;
}
Output
Enter n: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39
int testArray[10];
Now let's say if you try to access testArray[12] . The element is not available.
This may cause unexpected output (undefined behavior). Sometimes you
might get an error and some other time your program may run correctly.
Hence, you should never access elements of an array outside of its bound.
Multidimensional arrays
In C programming, you can create an array of arrays. These arrays are known
as multidimensional arrays. For example,
float x[3][4];
Here, x is a two-dimensional (2d) array. The array can hold 12 elements. You
can think the array as a table with 3 rows and each row has 4 columns.
Two dimensional Array
float y[2][4][3];
Initialization of a 2d array
Initialization of a 3d array
Output
City 1, Day 1: 33
City 1, Day 2: 34
City 1, Day 3: 35
City 1, Day 4: 33
City 1, Day 5: 32
City 1, Day 6: 31
City 1, Day 7: 30
City 2, Day 1: 23
City 2, Day 2: 22
City 2, Day 3: 21
City 2, Day 4: 24
City 2, Day 5: 22
City 2, Day 6: 25
City 2, Day 7: 26
Displaying values:
City 1, Day 1 = 33
City 1, Day 2 = 34
City 1, Day 3 = 35
City 1, Day 4 = 33
City 1, Day 5 = 32
City 1, Day 6 = 31
City 1, Day 7 = 30
City 2, Day 1 = 23
City 2, Day 2 = 22
City 2, Day 3 = 21
City 2, Day 4 = 24
City 2, Day 5 = 22
City 2, Day 6 = 25
City 2, Day 7 = 26
#include <stdio.h>
int main()
{
float a[2][2], b[2][2], result[2][2];
if (j == 1)
printf("\n");
}
return 0;
}
Output
Sum Of Matrix:
2.2 0.5
-0.9 25.0
#include <stdio.h>
int main()
{
int test[2][3][2];
printf("\nDisplaying values:\n");
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 2; ++k)
{
printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]);
}
}
}
return 0;
}
Output
Enter 12 values:
1
2
3
4
5
6
7
8
9
10
11
12
Displaying Values:
test[0][0][0] = 1
test[0][0][1] = 2
test[0][1][0] = 3
test[0][1][1] = 4
test[0][2][0] = 5
test[0][2][1] = 6
test[1][0][0] = 7
test[1][0][1] = 8
test[1][1][0] = 9
test[1][1][1] = 10
test[1][2][0] = 11
test[1][2][1] = 12
#include <stdio.h>
void display(int age1, int age2) {
printf("%d\n", age1);
printf("%d\n", age2);
}
int main() {
int ageArray[] = {2, 8, 4, 12};
Output
Here, we have passed array parameters to the display() function in the same
way we pass variables to a function.
We can see this in the function definition, where the function parameters are
individual variables:
#include <stdio.h>
float calculateSum(float num[]);
int main() {
float result, num[] = {23.4, 55, 22.6, 3, 40.5, 18};
return sum;
}
Output
Result = 162.50
To pass an entire array to a function, only the name of the array is passed as
an argument.
result = calculateSum(num);
This informs the compiler that you are passing a one-dimensional array to the
function.
#include <stdio.h>
void displayNumbers(int num[2][2]);
int main() {
int num[2][2];
printf("Enter 4 numbers:\n");
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
scanf("%d", &num[i][j]);
}
}
return 0;
}
Enter 4 numbers:
2
3
4
5
Displaying:
2
3
4
5
Notice the parameter int num[2][2] in the function prototype and function
definition:
// function prototype
void displayNumbers(int num[2][2]);
For example,
Address in C
If you have a variable var in your program, &var will give you its address in the
memory.
We have used address numerous times while using the scanf() function.
scanf("%d", &var);
Here, the value entered by the user is stored in the address of var variable.
Let's take a working example.
#include <stdio.h>
int main()
{
int var = 5;
printf("var: %d\n", var);
Output
var: 5
address of var: 2686778
Note: You will probably get a different address when you run the above code.
C Pointers
Pointers (pointer variables) are special variables that are used to store
addresses rather than values.
Pointer Syntax
int* p;
int *p1;
int * p2;
int* pc, c;
c = 5;
pc = &c;
To get the value of the thing pointed by the pointers, we use the * operator.
For example:
int* pc, c;
c = 5;
pc = &c;
printf("%d", *pc); // Output: 5
Here, the address of c is assigned to the pc pointer. To get the value stored in
that address, we used *pc .
Note: In the above example, pc is a pointer, not *pc . You cannot and should
not do something like *pc = &c ;
By the way, * is called the dereference operator (when working with pointers).
It operates on a pointer and gives the value stored in that pointer.
int* pc, c;
c = 5;
pc = &c;
c = 1;
printf("%d", c); // Output: 1
printf("%d", *pc); // Ouptut: 1
int* pc, c;
c = 5;
pc = &c;
*pc = 1;
printf("%d", *pc); // Ouptut: 1
printf("%d", c); // Output: 1
We have assigned the address of c to the pc pointer.
Then, we changed *pc to 1 using *pc = 1; . Since pc and the address of c is
the same, c will be equal to 1.
Let's take one more example.
int* pc, c, d;
c = 5;
d = -15;
#include <stdio.h>
int main()
{
int* pc, c;
c = 22;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 22
pc = &c;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 22
c = 11;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 11
*pc = 2;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 2
return 0;
}
Output
Address of c: 2686784
Value of c: 22
Address of c: 2686784
Value of c: 2
2. c = 22;
This assigns 22 to the variable c . That is, 22 is stored in the memory location
of variable c .
3. pc = &c;
4. c = 11;
5. *pc = 2;
This change the value at the memory location pointed by the pointer pc to 2.
Common mistakes when working with pointers
int c, *pc;
#include <stdio.h>
int main() {
int c = 5;
int *p = &c;
printf("%d", *p); // 5
return 0;
}
It's because
int *p = &c;
is equivalent to
int *p:
p = &c;
In both cases, we are creating a pointer p (not *p ) and assigning &c to it.
To avoid this confusion, we can use the statement like this:
int* p = &c;
Relationship Between Arrays and
Pointers
Relationship Between Arrays and Pointers
An array is a block of sequential data. Let's write a program to print addresses
of array elements.
#include <stdio.h>
int main() {
int x[4];
int i;
return 0;
}
Output
&x[0] = 1450734448
&x[1] = 1450734452
&x[2] = 1450734456
&x[3] = 1450734460
Address of array x: 1450734448
Similarly,
• ...
#include <stdio.h>
int main() {
return 0;
}
Enter 6 numbers: 2
3
4
4
12
4
Sum = 29
#include <stdio.h>
int main() {
return 0;
}
*ptr = 3
*(ptr+1) = 4
*(ptr-1) = 2
And, printing *(ptr+1) gives us the fourth element. Similarly, printing *(ptr-