Software & Finance





C Programming (Turbo C++ Compiler) - Retriving string array from a function





How to retrieve a string array from a function. Look at the example, we need to tripe pointer for this one.


Source Code


#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <bios.h>

#include <ctype.h>

 

 

void GetCountryNames(char ***p, int *n)

{

      static char *arrayCountryNames[] = {

            "USA", "UK", "India", "Dubai", "Singapore", "Australia",

            "Malaysia", "Phillipines", "Manila", "Ethopia", "Canada"

      };

 

      *p =  arrayCountryNames;

      *n = sizeof(arrayCountryNames) / sizeof(arrayCountryNames[0]);

 

};

 

void main()

{

 

      int numElements = 0;

      char **p = 0;

 

      GetCountryNames(&p, &numElements);

 

      for(int i = 0; i < numElements; i++)

            printf("%s\n", p[i]); 

}

Output


USA
UK
India
Dubai
Singapore
Australia 
Malaysia
Phillipines
Manila
Ethopia
Canada