Passing null terminated string to function using pointer notation – Computer Sir Ki Class

Login


Lost your password?

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

Login
[lwa]



Code Learning #CPP#2706 siteicon   siteicon   siteicon  

Passing null terminated string to function using pointer notation

Program to pass by reference character array as a null terminated string to a function using the pointer notation using *.

Learning Objectives

  • To pass c-style string (null terminated character array) as a function argument using the pointer notation *.

Source Code

TC++ #2706

 

Source Code

Alternate (?) :

Run Output

Input a small sentence: Yoga can transform life.
Number of spaces in given input = 3

Code Understanding

int space_count(char *);
This is function prototype with char array passed using pointer * notation.

char str[80]; int spaces=0; //Initialisations as required.
cout<<“Input a small sentence: ” ; cin.getline(str,80); //Collecting user input sentence.

spaces=space_count(str);
function calling. When  we give the array name, we are actually passing the address of the first member of array.

cout<<“Number of spaces in given input = “<<spaces<<endl;
Final output printing.

int space_count(char *str)
Function definition where the array name with * notation is passed.
{
int sp_count=0; //Initial value 0 needed so that junk value is not stored.
int len=strlen(str); //strlen can be found as the passed array is actually a null terminated string.
for(int i=0;i<len;i++) //Loop till number of characters time.
if(str[i]==’ ‘) sp_count++; //increment count the moment space ‘ ‘ character is found.
return sp_count; //Return the given count to the calling function as an integer value.
}

Notes

  • array notation and pointer notation can be interchangeably used for passing arrays, but professional programmers prefer to use the pointer notation.
  • This program has an assumption that the character array passed is actually a null terminated string. If the null termination is not there, the functions like strlen() etc. will not work. For other types of array say integer array or character array without null, passing the array length as another parameter will be required.

Common Errors

  • sometime student give pointer notation as char str* which is incorrect. It can be –
    char* str or char *str


Suggested Filename(s): chararr-pass-an.cpp, cap-an.cpp



Share

sunmitra| Created: 17-Jan-2018 | Updated: 17-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