C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Functions and Strings

C Programming

In this tutorial we will learn how to pass and use strings in functions in C programming language.

We know that a string is a sequence of characters enclosed in double quotes.

For example, "Hello World" is a string and it consists of a sequence of English letters in both uppercase and lowercase and the two words are separated by a white space. So, there are total 11 characters.

We know that a string in C programming language ends with a NULL \0 character. So, in order to save the above string we will need an array of size 12. The first 11 places will be used to store the words and the space and the 12th place will be used to hold the NULL character to mark the end.

Function declaration to accept one dimensional string

We know that strings are saved in arrays so, to pass an one dimensional array to a function we will have the following declaration.

returnType functionName(char str[]);

Example:

void displayString(char str[]);

In the above example we have a function by the name displayString and it takes an argument of type char and the argument is an one dimensional array as we are using the [] square brackets.

Passing one dimensional string to a function

To pass a one dimensional string to a function as an argument we just write the name of the string array variable.

In the following example we have a string array variable message and it is passed to the displayString function.

#include <stdio.h>

void displayString(char []);

int main(void) {
  // variables
  char
    message[] = "Hello World";
    
  // print the string message
  displayString(message);
  
  return 0;
}

void displayString(char str[]) {
  printf("String: %s\n", str);
}

Output:

String: Hello World

Another way we can print the string is by using a loop like for or while and print characters till we hit the NULL character.

In the following example we have redefined the displayString function logic.

#include <stdio.h>

void displayString(char []);

int main(void) {
  // variables
  char
    message[] = "Hello World";
    
  // print the string message
  displayString(message);
  
  return 0;
}

void displayString(char str[]) {
  int i = 0;
  printf("String: ");
  while (str[i] != '\0') {
    printf("%c", str[i]);
    i++;
  }
  printf("\n");
}

Output:

String: Hello World

Function declaration to accept two dimensional string

In order to accept two dimensional string array the function declaration will look like the following.

returnType functionName(char [][C], type rows);

Example:

void displayCities(char str[][50], int rows);

In the above example we have a function by the name displayCities and it takes a two dimensional string array of type char.

The str is a two dimensional array as because we are using two [][] sqaure brackets.

It is important to specify the second dimension of the array and in this example the second dimension i.e., total number of columns is 50.

The second parameter rows tell us about the total number of rows in the give two dimensional string array str.

The return type for this function is set to void that means it will return no value.

Passing two dimensional string to a function

To pass a two dimensional string to a function we just write the name of the string array variable as the function argument.

In the following example we have the name of 5 cities saved in an array cities of type char. We will be using the displayCities function to print the names.

#include <stdio.h>

void displayCities(char [][50], int rows);

int main(void) {
  // variables    
  char
    cities[][50] = {
      "Bangalore",
      "Chennai",
      "Kolkata",
      "Mumbai",
      "New Delhi"
    };

  int rows = 5;
  
  // print the name of the cities
  displayCities(cities, rows);
  
  return 0;
}

void displayCities(char str[][50], int rows) {
  // variables
  int r, i;
  
  printf("Cities:\n");
  for (r = 0; r < rows; r++) {
    i = 0;
    while(str[r][i] != '\0') {
      printf("%c", str[r][i]);
      i++;
    }
    printf("\n");
  }
}

Output:

Cities:
Bangalore
Chennai
Kolkata
Mumbai
New Delhi

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT