Created
February 26, 2012 15:28
-
-
Save sinannar/1917398 to your computer and use it in GitHub Desktop.
Good example about pointers also Function Pointers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Created By: EKREM ÇOBAN | |
* Editted By: Sinan NAR | |
* | |
* Dinamic allocation and Function Pointer Usage | |
* | |
*/ | |
/* Dinamik olarak elde edilen alanın başlangıç adresine dönen fonksiyon */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define SIZE 2 | |
//#define WINDOWS_ENVIRONMENT | |
#ifdef WINDOWS_ENVIRONMENT | |
#include <conio.h> | |
#endif | |
char *getname(); | |
int main(){ | |
char *p[2]; | |
int k, sum = 0; | |
char* (*fonkPtr)(); //function pointer declaration | |
fonkPtr=getname; //we can use fonkPtr as getname | |
for(k = 0; k < SIZE; ++k) | |
p[k] = fonkPtr(); //we use fonkPtr that point getname | |
printf("isimler: \n"); | |
for(k = 0; k < SIZE; ++k){ | |
printf("(%s)", p[k]); | |
sum += strlen(p[k]); | |
} | |
printf("\n"); | |
printf("Girilen yazinin toplam uzunlugu: %lf\n", (double)sum / SIZE); //average long of words | |
printf("first:%d and second:%d\n",strlen(p[0]),strlen(p[1])); | |
for(k = 0; k < SIZE; ++k) | |
free(p[k]); | |
#ifdef WINDOWS_ENVIRONMENT | |
getch(); | |
#endif | |
return 0; | |
} | |
char* getname(){ | |
char array[SIZE]; | |
char *ptr; | |
printf("Yazi girin:"); //wait for the entering string | |
gets(array); | |
ptr = (char*)malloc(strlen(array) + 1); | |
if(ptr == NULL){ | |
printf("Allocate bellek ayrilamadi!\n"); //there is no space to allocate | |
exit(EXIT_FAILURE); | |
} | |
strcpy(ptr,array); | |
return ptr; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
BU PROGRAMDA HICBIR EDITLEME YOKTUR OLDUGU GIBI EKREM COBAN IN GONDERDIGI IKINCI YANI FIX EDILMIS PROGRAMIN DOSYASIDIR | |
BU SAYFADAKI DIGER PROGRAMDA BENIM BIRKAC KOD'UM BULUNMAKTADIR | |
SiNAN NAR | |
*/ | |
/* Dinamik olarak elde edilen alanın başlangıç adresini döndüren fonksiyon. | |
Poniter dizinin her elemanına girdiğiniz yazıların, toplam uzunluğunu verir. */ | |
#include<stdio.h> | |
#include<stdlib.h> | |
#include<string.h> | |
#define SIZE 2 | |
char *getname(); | |
int main(){ | |
char *p[SIZE]; | |
int k, sum = 0; | |
for(k = 0; k < SIZE; ++k) | |
p[k] = getname(); | |
printf("isimler: \n"); | |
for(k = 0; k < SIZE; ++k){ | |
printf("(%s)", p[k]); | |
sum += strlen(p[k]); | |
} | |
printf("\n"); | |
printf("Girilen yazinin toplam uzunlugu: %d\n", sum); | |
for(k = 0; k < SIZE; ++k) | |
free(p[k]); | |
return 0; | |
} | |
char *getname(){ | |
char array[SIZE]; | |
char *ptr; | |
printf("Yazi girin:"); | |
gets(array); | |
ptr = (char*)malloc(strlen(array) + 1); | |
if(ptr == NULL){ | |
printf("Allocate bellek ayrilamadi!\n"); | |
exit(EXIT_FAILURE); | |
} | |
strcpy(ptr,array); | |
return ptr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment