Sep 28, 2014

C Programming #47: Array of pointers

We have already discussed array [C Programming #39: Array - single dimension] and pointers [C Programming #41: Pointer - introduction] separately. Here were going are going to see how they can be used together.


Just a small re-visit.

array - An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by same name with index notation.
pointer - Pointer is type of variable that stores the address of other variable/function/constant.

If we wanted array of 10 integer pointers, declaration would be as follows


int *parr[10];

Lets dive straight into example

lets re-write the same problem mentioned in previous article [C Programming #46: Pointer to function] using array of pointers to character.


Write a C program to print "hello world, welcome to wonderfull world of C" in different languages (english, spanish, german, french)


Strategy

  1. All string in different language will be stored in in array phello.
  2. We will define new enum (lang_t) that will define constants that will represent different languages, which will correspond to array index of phello.

#include <stdio.h>
typedef enum lang_tag{ENGLISH, SPANISH, GERMAN, FRENCH}lang_t;

char *phello[] = { "hello world, welcome to wonderfull world of C\n",
                   "hola mundo , bienvenidos a wonderfull mundo de C\n",
                   "Hallo Welt , willkommen in wunderbaren Welt der C\n",
                   "Bonjour tout le monde , bienvenue à merveilleux monde de C\n"};

int main()
{
   printf(phello[ENGLISH]);
   printf(phello[SPANISH]);
   printf(phello[GERMAN]);
   printf(phello[FRENCH]);

   return 0;
}

Output of above program is

hello world, welcome to wonderfull world of C
hola mundo , bienvenidos a wonderfull mundo de C
Hallo Welt , willkommen in wunderbaren Welt der C
Bonjour tout le monde , bienvenue à merveilleux monde de C
Explanation
  1.  char *p is pointer to character.
  2.  "hello world, welcome to wonderful world of C" is called as string constant.
  3.  string constant are array of characters, array would contain character 'h' 'e' 'l' 'l' 'o' ' ' 'w' and so on.
  4.  It is constant because string wont change during the execution of the program.
  5.  string "hello world, welcome to wonderful world of C" would be stored in text section. (read to understand what is text section).
  6.  Data stored any where will have address associated with it.
  7.  char *str = "hello", here str is pointer to character, which is pointing to constant string hello.
  8. Similarly is variable phello is array of character pointer that points to 4 string which are in different languages.


lets re-write the same problem mentioned in previous article using array of function pointer.

Write a C program to print "hello world, welcome to wonderful world of C" in different languages (English, Spanish, German, French)



#include <stdio.h>

void hello_english(void);
void hello_spanish(void);
void hello_german(void);
void hello_french(void);

typedef enum lang_tag{ENGLISH, SPANISH, GERMAN, FRENCH}lang_t;

int main()
{
   void (*hello[]) (void) = {hello_english, hello_spanish,
                            hello_german, hello_french};
    hello[ENGLISH]();
    hello[SPANISH]();
    hello[GERMAN]();
    hello[FRENCH]();

}
void hello_english(void)
{
   printf("hello world, welcome to wonderfull world of C\n");
 return;
}
void hello_spanish(void)
{
   printf("hola mundo , bienvenidos a wonderfull mundo de C\n");
 return;
}
void hello_german(void)
{
   printf("Hallo Welt , willkommen in wunderbaren Welt der C\n");
 return;
}
void hello_french(void)
{
   printf("Bonjour tout le monde , bienvenue à merveilleux monde de C\n");
 return;
}


hello world, welcome to wonderfull world of C
hola mundo , bienvenidos a wonderfull mundo de C
Hallo Welt , willkommen in wunderbaren Welt der C
Bonjour tout le monde , bienvenue à merveilleux monde de C
Explanation
  1. Function hello in difference language did not change.
  2. Since hello_english, hello_spanish, hello_german, hello_french are all similar function and address of can be placed in array.
  3. Note how the declaration of array of pointer is done.
    1. void (*hello[]) (void)
  4. Also note how the function call is made when it is array of function pointer.

Later on, we will discuss all complicated declaration. Don't get intimidated with it.

Links

Next Article - C Programming #48: Pointers and Array - brothers
Previous Article - C Programming #46: Pointer to function

All Article - C Programming


No comments :

Post a Comment