Basics of Character Single Dimension Array

In this section, you are going to learn

What are the basic properties of a character 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 ?

char array_name[Column];

Consider a Character Single Dimension Array

char a[10];

Let us answer few basic questions in this array

How many characters can be stored in this array ?

How many bytes are there in this array ?

What is the sizeof the array ?

How many bits can be stored in this array ?

Let us now explore basic examples of single dimension array !

  • Step 1 : Define a Single Dimension Array

char a[10] = "Laptop";
  • Step 2 : Access and Print individual characters

for (int i = 0; i < strlen(a); i++)
{
        printf("%c", a[i]);
}
  • Step 3 : Access and Print individual strings

printf("%s", a);
  • Step 4 : See the full program below

#include <stdio.h>
#include <string.h>

int main(void)
{
        char a[10] = "Laptop";

        // Print individual characters
        printf("---------- Access and Print Individual Characters --------------\n");
        for (int i = 0; i < strlen(a); i++)
        {
                printf("%c", a[i]);
        }
        printf("\n");

        // Print string
        printf("---------- Access and Print Individual String --------------\n");
        printf("%s", a);
        printf("\n");

        return 0;
}
  • Step 5 : Output is as below

---------- Access and Print Individual Characters --------------
Laptop

---------- Access and Print Individual String --------------
Laptop
  • Step 1 : Define a Single Dimension Array

char a[10] = { 'L', 'a', 'p', 't', 'o', 'p', '\0' };
  • Step 2 : Access and Print individual characters

for (int i = 0; i < strlen(a); i++)
{
        printf("%c", a[i]);
}
  • Step 3 : Access and Print individual strings

printf("%s", a);
  • Step 4 : See the full program below

#include <stdio.h>
#include <string.h>

int main(void)
{
        char a[10] = { 'L', 'a', 'p', 't', 'o', 'p', '\0' };

        // Print individual characters
        printf("---------- Access and Print Individual Characters --------------\n");
        for (int i = 0; i < strlen(a); i++)
        {
                printf("%c", a[i]);
        }
        printf("\n");

        // Print string
        printf("---------- Access and Print Individual String --------------\n");
        printf("%s", a);
        printf("\n");

        return 0;
}
  • Step 5 : Output is as below

---------- Access and Print Individual Characters --------------
Laptop

---------- Access and Print Individual String --------------
Laptop

Let us now explore Double Dimension Array in depth !

Consider a character single dimension array

char 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[0] is first character in the array

a[1]

  • a[1] is second character in the array

a[9]

  • a[9] is tenth character in the array

&a[0]

  • &a[0] is address of first character

&a[1]

  • &a[1] is address of second character

&a[9]

  • &a[9] is address of tenth character

*a

  • *a is first character in the array

*(a + 1)

  • *(a + 1) is second character in the array

*(a + 9)

  • *(a + 9) is tenth character in the array

a

  • a is address of first character

  • a is an array of ten characters

a + 1

  • a + 1 is address of second character

  • a is an array of nine characters

a + 9

  • a + 9 is address of tenth character

  • a + 9 is an array of one character

&a

  • &a is the address of array of characters

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]

char

  • a[0] is a character

  • Hence type is char

a[1]

char

  • a[1] is a character

  • Hence type is char

a[9]

char

  • a[9] is a character

  • Hence type is char

&a[0]

char *

  • a[0] is a character

  • typeof(a[0]) is char

  • Hence typeof(&a[0]) is char *

&a[1]

char *

  • a[1] is a character

  • typeof(a[1]) is char

  • Hence typeof(&a[1]) is char *

&a[9]

char *

  • a[9] is a character

  • typeof(a[9]) is char

  • Hence typeof(&a[9]) is char *

*a

char

  • *a is a character

  • Hence type is char

*(a + 1)

char

  • *(a + 1) is a character

  • Hence type is char

*(a + 9)

char

  • *(a + 9) is a character

  • Hence type is char

a

char *

  • a is equal to &a[0]

  • Hence typeof(a) is char *

a + 1

char *

  • a + 1 is equal to &a[1]

  • Hence typeof(a + 1) is char *

a + 9

char *

  • a + 9 is equal to &a[9]

  • Hence typeof(a + 9) is char *

&a

char (* )[10]

  • &a is address of complete array

sizeof(expression)

size

Description

sizeof(a[0])

1 Byte

a[0] is a character

sizeof(a[1])

1 Byte

a[1] is a character

sizeof(a[9])

1 Byte

a[9] is a charcter

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)

1 Byte

*a is a character

sizeof(*(a + 1) )

1 Byte

*(a + 1) is a character

sizeof(*(a + 9) )

1 Byte

*(a + 9) is a character

sizeof(a)

10 Bytes

a is an array of 10 characters

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)
{
        char 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])            = 1
sizeof(a[1])            = 1
sizeof(a[9])            = 1
sizeof(&a[0])           = 8
sizeof(&a[1])           = 8
sizeof(&a[9])           = 8
sizeof(*a)              = 1
sizeof( *(a + 1) )      = 1
sizeof( *(a + 9) )      = 1
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(char x) { }

  • Call by Value

fun(a[1])

void fun(char x) { }

  • Call by Value

fun(a[9])

void fun(char x) { }

  • Call by Value

fun(&a[0])

void fun(char *p) { }

  • Call by Reference

fun(&a[1])

void fun(char *p) { }

  • Call by Reference

fun(&a[9])

void fun(char *p) { }

  • Call by Reference

fun(*a)

void fun(char x) { }

  • Call by Value

fun(*(a + 1))

void fun(char x) { }

  • Call by Value

fun(*(a + 9))

void fun(char x) { }

  • Call by Value

fun(a)

void fun(char *p) { }

  • Call by Reference

fun(a + 1)

void fun(char *p) { }

  • Call by Reference

fun(a + 9)

void fun(char *p) { }

  • Call by Reference

fun(&a)

void fun(char (*p) [10] ) { }

  • Call by Reference

Read more about function calls and conventions of Functions and Character Single Dimension Array