Stanford 编程范式 lesson 4

本文介绍了如何在C语言中实现不关心变量类型的交换函数,通过指针和内存操作实现`swap`功能,以及利用`memcpy`进行任意类型变量的交换。此外,还探讨了线性搜索函数`_lsearch`的实现,利用`memcmp`进行内存比较,从而在数组中查找目标元素。
摘要由CSDN通过智能技术生成

【斯坦福大学公开课】编程范式_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV11441137jY?p=4&spm_id_from=pageDriver如何在不care变量类型的情况下实现swap函数

一、swap 交换变量的值

1.使用指针作为参数交换两个 int

// 将字符串复制到数组 dest 中
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


void swap(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    //写法1
    int x = 17, y = 37;
    swap(&x, &y);
    printf("%d", x);
    
    //写法2
    int* a = &x, * b = &y;
    swap(a, b);
    printf("%d", x);

    return(0);
}

为何两种写法都可以?

指针是一个unsigned类型的整数,此处需要传入待交换两数的地址。当你传入&x或者a时,它们表示的都是x变量的地址,所以都符合要求。

2.使用memcpy交换任意两个变量的值

2.1 memcpy 用来复制内存

void *memcpy(void *str1, const void *str2, size_t n)
  1. memcpy() 会复制 src 所指的内存内容的前 num 个字节到 dest 所指的内存地址上。
  2. memcpy() 并不关心被复制的数据类型,只是逐字节地进行复制,这给函数的使用带来了很大的灵活性,可以面向任何数据类型进行复制。

2.2 利用strdup进行字符串拷贝

strdup函数原型:

strdup()主要是拷贝字符串s的一个副本,由函数返回值返回,这个副本有自己的内存空间,和s不相干。strdup函数复制一个字符串,使用完后要记得删除在函数中动态申请的内存,strdup函数的参数不能为NULL,一旦为NULL,就会报段错误,因为该函数包括了strlen函数,而该函数参数不能是NULL。

// 将字符串复制到数组 dest 中
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>


void swap(void* vp1, void* vp2, int size) {

    //1. 动态分配数组空间
    //char* buffer;// char数组每个占一字节,一共size字节
    //buffer = (char*)calloc(size, sizeof(char));
    char* buffer = (char*)malloc(size * (sizeof(char)));

    //2. 复制内存内容
    memcpy(buffer, vp1, size);// 对vp1解引用,得到它所指向的值,将该值复制到buffer
    memcpy(vp1, vp2, size);
    memcpy(vp2, buffer, size);
}



int main() {
    int x = 17, y = 37;
    swap(&x, &y, sizeof(int));
    printf("%d\n", x);

    char x1 = 'a', x2 = 'b';
    swap(&x1, &x2, sizeof(char));
    printf("%c\n", x1);

    bool f1 = true, f2 = false;
    swap(&f1, &f2, sizeof(char));
    printf("%d\n", f1);

    char* husband = _strdup("Fred");
    char* wife = _strdup("Willa");
    // swap(husband, wife, sizeof(char*));// 交换char*里面的值 Will
    // swap(husband, &wife, sizeof(char*));// 交换char*里面的值 Crush
    swap(&husband, &wife, sizeof(char*));// 交换char*里面的值

    printf("%s\n", husband);

    return(0);
}

二、线性搜索的函数原型

1 利用memcmp进行内存中值的比较

基本原型

int memcmp(const void *buf1, const void *buf2, unsigned int count);


主要功能

比较内存区域buf1和buf2的前count个字节。

// 将字符串复制到数组 dest 中
#include <stdio.h>
#include <string.h>/#include <stdlib.h>
//#include <stdbool.h>

void *_lsearch(void* key, void* base, int n, int elemSize) {
	
	for (int i = 0; i < n; i++) {
		void* elemAddress = (char*)base + i * elemSize;
		if (memcmp(key, elemAddress, elemSize) == 0) return elemAddress;
	}
	return NULL;
}
int main() {
	int a[] = { 1,3,5,7 };
	int key1 = 13;
	int* find1 = NULL;
	find1 = _lsearch(&key1, a, sizeof(a) / sizeof(int), sizeof(int));
	printf("address of key1 is:%p\n", find1);

	char c[] = { 'a','b','c' };
	char key2 = 'c';
	char* find2 = NULL;
	find2 = _lsearch(&key2, c, sizeof(c) / sizeof(char), sizeof(char));
	printf("address of key2 is:%p", find2);
}

//可查看float中元素的地址如何排列
void main() {
	float a[] = { 1.2,4.5,7.99,8.0 };
	float key1 = 1.2;
	float* find1 = NULL;
	find1 = _lsearch(&key1, a, sizeof(a) / sizeof(float), sizeof(float));
	printf("%p\n", find1);

	float key2 = 4.5;
	float* find2 = _lsearch(&key2, a, sizeof(a) / sizeof(float), sizeof(float));
	printf("%p", find2);
	return 0;
}

方法元素c语言范式编程之lsearch - xinyuyuanm - 博客园 (cnblogs.com)icon-default.png?t=LA92https://www.cnblogs.com/xinyuyuanm/archive/2013/05/04/3060055.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值