Basics of Integer Single Dimension Array
In this section, you are going to learn
What are the basic properties of a integer single dimension array ?
What are the different expressions of single dimension array ?
What are synonymous expresions of single dimension array ?
How to find sizeof() of expressions of single dimenstion array ?
int array_name[Column];
Consider a Integer Single Dimension Array
int a[10];
Let us answer few basic questions in this array
How many integers can be stored in this array ?
See Answer
Number of Integers = 10
How many bytes are there in this array ?
See Answer
Number of Bytes = 10 * sizeof(int) = 10 * 4 = 40 Bytes
What is the sizeof the array ?
See Answer
sizeof(a) = Number of Bytes = 10 * sizeof(int) = 10 * 4 = 40 Bytes
How many bits can be stored in this array ?
See Answer
Number of Bits = sizeof(a) * 8 = 40 * 8 = 320 bits
Let us now explore basic examples of single dimension array !
Step 1 : Define a Single Dimension Array
int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Step 2 : Access and Print individual integers
for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
{
printf("%d ", a[i]);
}
Step 4 : See the full program below
#include <stdio.h>
#include <string.h>
int main(void)
{
int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// Print individual integers
printf("---------- Access and Print Individual Integers --------------\n");
for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
{
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
Step 5 : Output is as below
---------- Access and Print Individual Integers --------------
0 1 2 3 4 5 6 7 8 9
Let us now explore Double Dimension Array in depth !
Consider a integer single dimension array
int a[10];
Then below are the properties
Expressions
Synonyms
typeof(expression)
sizeof(expression)
fun(expression)
Know what are the possible expressions and how to properly call them !
Expression |
Description |
---|---|
a[0] |
|
a[1] |
|
a[9] |
|
&a[0] |
|
&a[1] |
|
&a[9] |
|
*a |
|
*(a + 1) |
|
*(a + 9) |
|
a |
|
a + 1 |
|
a + 9 |
|
&a |
|
Synonyms : Which can be used interchangeably !
Programmatically few expressions are one and the same ! Let us learn them
Expression |
Synonyms |
---|---|
a[0] |
*a |
a[1] |
*(a + 1) |
a[9] |
*(a + 9) |
&a[0] |
a + 0 a |
&a[1] |
a + 1 |
&a[9] |
a + 9 |
*a |
a[0] |
*(a + 1) |
a[1] |
*(a + 9) |
a[9] |
a |
&a[0] |
a + 1 |
&a[1] |
a + 9 |
&a[9] |
&a |
&a |
Finding the type of an expression is easy. See below
Expression |
Type |
Description |
---|---|---|
a[0] |
int |
|
a[1] |
int |
|
a[9] |
int |
|
&a[0] |
int * |
|
&a[1] |
int * |
|
&a[9] |
int * |
|
*a |
int |
|
*(a + 1) |
int |
|
*(a + 9) |
int |
|
a |
int * |
|
a + 1 |
int * |
|
a + 9 |
int * |
|
&a |
int (* )[10] |
|
sizeof(expression) |
size |
Description |
---|---|---|
sizeof(a[0]) |
4 Bytes |
a[0] is a integer |
sizeof(a[1]) |
4 Bytes |
a[1] is a integer |
sizeof(a[9]) |
4 Bytes |
a[9] is a integer |
sizeof(&a[0]) |
8 Bytes |
&a[0] is address / pointer |
sizeof(&a[1]) |
8 Bytes |
&a[1] is address / pointer |
sizeof(&a[9]) |
8 Bytes |
&a[9] is address / pointer |
sizeof(*a) |
4 Bytes |
*a is a integer |
sizeof(*(a + 1) ) |
4 Bytes |
*(a + 1) is a integer |
sizeof(*(a + 9) ) |
4 Bytes |
*(a + 9) is a integer |
sizeof(a) |
40 Bytes |
a is an array of 10 integers |
sizeof(a + 1) |
8 Bytes |
a + 1 is address / pointer |
sizeof(a + 9) |
8 Bytes |
a + 9 is address / pointer |
sizeof(&a) |
8 Bytes |
&a is address / pointer |
See the full program below
#include <stdio.h>
int main(void)
{
int a[10];
printf("sizeof(a[0]) = %d\t\n", (int) sizeof(a[0]) );
printf("sizeof(a[1]) = %d\t\n", (int) sizeof(a[1]) );
printf("sizeof(a[9]) = %d\t\n", (int) sizeof(a[9]) );
printf("sizeof(&a[0]) = %d\t\n", (int) sizeof(&a[0]) );
printf("sizeof(&a[1]) = %d\t\n", (int) sizeof(&a[1]) );
printf("sizeof(&a[9]) = %d\t\n", (int) sizeof(&a[9]) );
printf("sizeof(*a) = %d\t\n", (int) sizeof(*a) );
printf("sizeof( *(a + 1) ) = %d\t\n", (int) sizeof( *(a + 1) ) );
printf("sizeof( *(a + 9) ) = %d\t\n", (int) sizeof( *(a + 9) ) );
printf("sizeof(a) = %d\t\n", (int) sizeof(a) );
printf("sizeof(a + 1) = %d\t\n", (int) sizeof(a + 1) );
printf("sizeof(a + 9) = %d\t\n", (int) sizeof(a + 9) );
printf("sizeof(&a) = %d\t\n", (int) sizeof(&a) );
return 0;
}
Output is as below on 64 bit OS
sizeof(a[0]) = 4
sizeof(a[1]) = 4
sizeof(a[9]) = 4
sizeof(&a[0]) = 8
sizeof(&a[1]) = 8
sizeof(&a[9]) = 8
sizeof(*a) = 4
sizeof( *(a + 1) ) = 4
sizeof( *(a + 9) ) = 4
sizeof(a) = 10
sizeof(a + 1) = 8
sizeof(a + 9) = 8
sizeof(&a) = 8
If fun(x)
is the function call, then fun(typeof(x))
is the prototype / definition
Function Call |
Function Definition |
Observations |
---|---|---|
fun(a[0]) |
void fun(int x) { } |
|
fun(a[1]) |
void fun(int x) { } |
|
fun(a[9]) |
void fun(int x) { } |
|
fun(&a[0]) |
void fun(int *p) { } |
|
fun(&a[1]) |
void fun(int *p) { } |
|
fun(&a[9]) |
void fun(int *p) { } |
|
fun(*a) |
void fun(int x) { } |
|
fun(*(a + 1)) |
void fun(int x) { } |
|
fun(*(a + 9)) |
void fun(int x) { } |
|
fun(a) |
void fun(int *p) { } |
|
fun(a + 1) |
void fun(int *p) { } |
|
fun(a + 9) |
void fun(int *p) { } |
|
fun(&a) |
void fun(int (*p) [10] ) { } |
|
Read more about function calls and conventions of Functions and Integer Single Dimension Array
Other topics of character and functions
Current Module
Previous Module
Next Module
Other Modules