Char array variations – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Code Learning #CPP#1623 siteicon   siteicon   siteicon  

Char array variations

Learning character array variations and need for null terminator

Learning Objectives

  • Concept of character arrays.
  • Character array initialisation.
  • Anomalies of character array initialisation.
  • Concept of null terminated strings, also called the c-style strings.
  • Behaviour of cout in printing null terminated and non-null terminated character arrays.
  • Cases where compiler automatically puts null character at the end of character arrays.

Source Code

TC++ #1623

Source Code

Run Output

array 1 contains FACE
array 2 contains NOSE
array 3 contains NOSE
array 4 contains CHIN
array 1 by individual characters = FACE

Note: output of line 1 and 2 may differ in your computer based on memory conditions.

Code Understanding

char ca1[]={‘F’,’A’,’C’,’E’};
Initialisation character array with individual characters. Here 4 initialising character F, A, C and E are given and array size will automatically become 4 characters.
char ca2[4]={‘N’,’O’,’S’,’E’};
Here array size is predefined and only 4 characters can be given for initialisation. If you give more than that program will not compile.
char ca3[5]={‘N’,’O’,’S’,’E’};
Here array size has been defined as 5 and only 4 initialisation characters have been given. In this 1 extra space of putting null () character is there. This will automatically be done by the compiler and ca3 will be a neatly printable null character terminated string of C language style of strings.
char ca4[]=”CHIN”;
Here string initialisation has been done with a 4 character string literal. One extra space for null character will automatically be assigned by the compiler.
cout<<“array 1 contains “<<ca1<<endl;
This will print FACE followed by few junk characters as null termination is not proper.
cout<<“array 2 contains “<<ca2<<endl;
This will print NOSE followed by few junk characters as null termination is not proper in this case also as the space for 5th character is not there..
cout<<“array 3 contains “<<ca3<<endl;
This will print NOSE properly as 5th character storage space we have provided and compiler has an opportunity to terminate it properly with the null character.
cout<<“array 4 contains “<<ca4<<endl;
This will print CHIN properly as string literal has been used for initialisation and compiler has its own choice of creating extra space for null character.
cout<<“array 1 by individual characters = “
<<ca1[0]<<ca1[1]<<ca1[2]<<ca1[3]<<endl;
This will print FACE as individual characters are now being picked from the array and are being displayed one after the other.

Notes

  • In C++ use of string type is recommended in place of null terminated character array.
  • In C++ Char array is used mainly when it is convenient to store a series of individual characters for the purpose of some character oriented data.


Suggested Filename(s): chararr.cpp, chararraytypes.cpp



Share

sunmitra| Created: 4-Dec-2017 | Updated: 11-Jan-2018|






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back