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
- All string in different language will be stored in in array phello.
- 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
- char *p is pointer to character.
- "hello world, welcome to wonderful world of C" is called as string constant.
- string constant are array of characters, array would contain character 'h' 'e' 'l' 'l' 'o' ' ' 'w' and so on.
- It is constant because string wont change during the execution of the program.
- string "hello world, welcome to wonderful world of C" would be stored in text section. (read to understand what is text section).
- Data stored any where will have address associated with it.
- char *str = "hello", here str is pointer to character, which is pointing to constant string hello.
- 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
- Function hello in difference language did not change.
- Since hello_english, hello_spanish, hello_german, hello_french are all similar function and address of can be placed in array.
- Note how the declaration of array of pointer is done.
- void (*hello[]) (void)
- 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 - brothersPrevious Article - C Programming #46: Pointer to function
All Article - C Programming
No comments :
Post a Comment