Bu funksiyalardan foydalanish uchun <MEM.H> sarlavhali fayllarni ulash lozim. Xotirani to’ldirish uchun memset funksiyasidan foydalaniladi.
Prototipi:
void *memset (void *s, int c, size_t n);
Funksiya memset s massiv birinchi n ta elementini c ta simvol bilan to’ldiradi va s qaytaradi.
Misol:
#include<stdio.h>
#include <mem.h>
#include <string.h>
int main()
{
char buffer[] = "UZbekdevs\n";
printf("%s\n", buffer);
memset(buffer, '*', strlen(buffer) - 1);
printf("%s\n", buffer);
return 0;
}
Simvol izlash uchun memchr funksiyasidan foydalaniladi. Funksiya c simvoldan iborat n baytni qidiradi.
Prototipi:
void *memchr (const void *s, int c, size_t n);
Funksiya *s blokda c simvoldan iborat n uzunlikdagi blokni izlaydi. Agar blok mavjud bo’lsa ko’rsatkich qaytaradi aks holda null qaytaradi.
Misol:
#include<stdio.h>
#include <mem.h>
#include <string.h>
int main()
{
char str[17] = "Bu satr";
char *ptr;
ptr = (char *) memchr(str, 'r', strlen(str));
if (ptr)
printf("pozitsiya = %d\n", ptr - str);
else
printf("emas\n");
return 0;
}
Nusxa olish uchun memcpy funksiyasidan foydalaniladi. Siljitish uchun memmove funksiyasidan foydalaniladi
Prototipi:
void *memcpy (void *dest, const void *src, size_t n);
void *memmove(void *dest, const void *src, size_t n);
hajmi n baytga teng src blokni dest ga nusxalaydi. Ikkala funksiya dest qaytaradi. Xatolik yuz bersa null qaytaradi.
Misol:
#include<stdio.h>
#include <mem.h>
#include <string.h>
int main()
{
char src[] = "******************************";
char dest[] = "abcdefghijlkmnopqrstuvwxyz0123456709";
char *ptr;
printf("dest = %s\n", dest);
ptr = (char *) memcpy(dest, src, strlen(src));
if (ptr)
printf("dest = %s\n", dest);
else
printf("memcpy failed\n");
return 0;
}
Misol:
#include<stdio.h>
#include <mem.h>
int main()
{
char dest[256] = "abcdefghijklmnopqrstuvwxyz0123456789";
char src[256] = "******************************";
printf("dest = %s\n", dest);
memmove(dest, src, 30);
printf("dest = %s\n", dest);
return 0;
}
Xotira bloklarini solishtirish uchun memcmp funksiyasidan foydalaniladi.
Prototipi:
int memcmp (const void *s1, const void *s2, size_t n);
Funksiya memcmp s1 va s2 birinchi n baytini ishorasiz simvol sifatida solishtiradi.
Qaytaradigan qiymati:
- natija < 0 agar s1 < s2
- natija == 0 agar s1 == s2
- natija > 0 agar s1 > s2
Misol:
#include<stdio.h>
#include <mem.h>
#include <string.h>
int main()
{
char *buf1 = "aaa";
char *buf2 = "bbb";
int stat;
stat = memcmp(buf2, buf1, strlen(buf2));
if (stat > 0) printf("buffer 2 > buffer 1\n");
if (stat < 0) printf("buffer 2 < buffer 1\n");
if (stat = = 0) printf("buffer 2 = = buffer 1\n");
getch();
}
Universal funksiyalar
Xotira bilan ishlovchi funksiyalar asosida turli universalligi to’liq bo’lmagan va to’liq bo’lgan funksiyalar yaratish mumkin. Misol uchun maksimal elementni topuvchi funksiyani sonlar uchun ishlatilgan dasturi:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void* max_elem(void *base, size_t nelem, size_t width)
{
unsigned i;
unsigned n = nelem-1;
char*p1 = base;
char*p2 = base;
for(i = 0;i<n;i++)
{
p2 = p2+width;
if(memcmp((void*)p2,(void*)p1,width)>0) p1 = p2;
}
return (void*)p1;
}
unsigned list[5] = { 1,4,12,4,3};
int main(void)
{
unsigned* p = (unsigned*)max_elem((void *)list, 5, sizeof(list[0]));
printf("max = %d\n", *p);
return 0;
}
// max=12
Yuqorida yaratilgan funksiya to’liq universal emas. Sababi shundan iboratki, baytma-bayt solishtirish memcmp funksiyasini har qanday turlarga va struktura turidagi o’zgaruvchilarga qo’llab bo’lmaydi. Lekin nusxa olish memcpy funksiyasini ixtiyoriy turlarga qo’llash mumkin. Shuning uchun bu funksiya asosida to’liq universal funksiya yaratish mumkin. Masalan almashtirish replace funksiyasini haqiqiy sonlar uchun qo’llangan dasturi:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<conio.h>
void replace(void *base, void *s1,void *s2,size_t nelem, size_t width)
{
unsigned i;
char*p = base;
for(i = 0;i<nelem;i++)
{
if(memcmp((void*)p,s1,width) == 0) memcpy(p,s2,width);
p+ = width;
}
}
float list[5] = { -12.5,4,-12.5,12.5,-12.5};
int main(void)
{
float a = -12.5;float b = -1.06;
int i;
replace(list, &a,&b,5,sizeof(list[0]));
for(i = 0;i<5;i++) printf("%f\n", list[i]);
getch();
return 0;
}
Funksiyaga ko’rsatkich. Universal funksiyalar yaratish uchun solishtirish funksiyasiga ko’rsatkichni parametr sifatida uzatish lozim. Masalan, shu usuldan foydalangan holda maksimal elementni topish funksiyasini satrlar uchun qo’llangan dasturi:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int comp_function( const void *a, const void *b);
void* max_elem(void *base, size_t nelem, size_t width, int (*fcmp)(const void*, const void *))
{
unsigned i;
unsigned n = nelem-1;
char*p1 = base;
char*p2 = base;
for(i = 0;i<n;i++)
{
p2 = p2+width;
if(fcmp((void*)p2,(void*)p1)>0) p1 = p2;
}
return (void*)p1;
}
char list[5][4] = { "qizil", "qora", "oq", "binafsha", "ko'k" };
int main(void)
{
char* p = (char*)max_elem((void *)list, 5, sizeof(list[0]), comp_function);
printf("max = %s\n", p);
return 0;
}
int comp_function( const void *a, const void *b)
{
return( strcmp((char *)a,(char *)b) );
}
// max=qoraoq
Keyingi misolda maksimal elementni topish funksiyasini struktura uchun qo’llangan dasturi keltirilgan:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
int i;
char st[4];
}
FS;
void print(FS a)
{
printf("%d %s",a.i,a.st);
}
int comp_function( const void *a, const void *b);
void* max_elem(void *base, size_t nelem,size_t width, int (*fcmp)(const void *, const void *))
{
unsigned i;
unsigned n = nelem-1;
char*p1 = base;
char*p2 = base;
for(i = 0;i<n;i++)
{
p2 = p2+width;
if(fcmp((void*)p2,(void*)p1)>0) p1 = p2;
}
return (void*)p1;
}
FS list[5] = { {0,"bat"}, {6,"arr"}, {2,"tab"}, {3,"fap"}, {4,"ran"} };
int main(void)
{
FS* p = (FS*)max_elem((void *)list, 5, sizeof(list[0]), comp_function);
print(*p);
return 0;
}
int comp_function( const void *a, const void *b)
{
FS* a1 = (FS*)a;
FS*b1 = (FS*)b;
if ((*a1).i>(*b1).i)
return 1;
else return 0;
}
//Natija: 6 arr
Universal funksiyalar yaratilganda parametr sifatida solishtirish funksiyasidan tashqari turli amallar bajaradigan funksiyalarga ko’rsatkich uzatish mumkin. Quyidagi dasturda shu usuldan foydalanib yaratilgan foreach funksiyasidan foydalanish ko’rsatilgan:
#include <stdio.h>
typedef struct
{
int i;
char st[4];
}
FS;
void print(void* a)
{
FS* pa = (FS*)a;
printf("%d %s\n",pa->i,pa->st);
}
int op_function( void *a);
void for_each(void *base, size_t nelem, size_t width, void (*op)(void *))
{
unsigned i;
char*p = base;
for(i = 0;i<nelem;i++)
{
op(p);
p = p+width;
}
}
FS list[5] = { {0,"bat"}, {6,"arr"}, {2,"tab"}, {3,"fap"}, {4,"ran"} };
int main(void)
{
for_each((void *)list, 5, sizeof(list[0]), print);
return 0;
}
//Natija:
//0 bat
//6 arr
//2 tab
//3 fap
//4 ran