English 中文(简体)
C类通用方案
原标题:generic programming in C with void pointer

Even though it is possible to write generic code in C using void pointer(generic pointer), I find that it is quite difficult to debug the code since void pointer can take any pointer type without warning from compiler. (e.g function foo() take void pointer which is supposed to be pointer to struct, but compiler won t complain if char array is passed.) What kind of approach/strategy do you all use when using void pointer in C?

最佳回答

解决办法不是使用<代码> 避免*,除非你真的确实需要。 实际需要空白点的地点非常小:校对功能的参数,还有一些地方需要通过通用功能通过具体执行的数据。 不管怎样,接受<代码> 避免*参数的代码只应当接受one<>>>>>>>>>通过空白点传送的数据类型,这种数据类型应当记录在评论中,并受所有电离器的奴役。

问题回答

这可能有助于:

rel=“nofollow noreferer”>comp.lang.c FAQ list Question 4.9

Q: 赞同我拟写一种以通用名词为理由的职能,我想通过提及加以模拟。 Can 我给正式参数类型** 留下了空白,也做了类似的事情?

void f(void **);
double *dp;
f((void **)&dp);

A: 不可运输。 类似守则可能奏效,有时建议采用,但取决于具有相同内部代表性的所有点类型(通常但并非普遍性;见问题5.17)。

C. 真空中不存在通用的点对点类型,* 只是因为其他点类型被指定为无效时(如有必要)自动适用转换;如果试图间接地以无效** 值作为,则不能进行这种转换。 如果你使用无效**点值(例如,当你利用*操作员获取无效* 数值时,汇编者无法知道该真空* 值是否曾经从其他点转换过。 它必须假定它只不过是一个真空;它不能进行任何暗中转换。

换言之,你发挥的任何无效** 价值必须是实际无效的地址* 某一地方的价值;如(无**)和p,虽然它们可能关闭汇编者,但却是不可驱逐的(甚至可能不做你想要的东西;另见问题13.9)。 如果无效** 指出无效,如果其大小或代表性与无效不同,则汇编者无法正确查阅。

为使上述准则支离破碎,您必须使用中间空白* 变量:

double *dp;
void *vp = dp;
f(&vp);
dp = vp;

这些派任和免职使汇编者有机会在必要时进行转换。

同样,迄今为止的讨论假设,不同的点类型可能具有不同的大小或代表性,而今天这种规模或代表性是罕见的,但并非没有受到重视。 更清楚地理解问题** ,将情况与类似情况相比较,后者涉及的类型和双重,其规模可能不同,而且肯定有不同的代表性。 如果我们能发挥作用,

void incme(double *p)
{
    *p += 1;
}

那么,我们可以做这样的事情。

int i = 1;
double d = i;
incme(&d);
i = d;

i) 增减1。 (这类似于涉及辅助性 vp的正确的无效代码。) 另一方面,如果我们想要尝试像这样的东西。

int i = 1;
incme((double *)&i);    /* WRONG */

(该法典类似于问题中的碎块),因此极不可能奏效。

能够改变阿里亚办法,以支持一个可变的规模:

#include <stdio.h>
#include <string.h>

void swap(void *vp1,void *vp2,int size)
{
  char buf[size];
  memcpy(buf,vp1,size);
  memcpy(vp1,vp2,size);
  memcpy(vp2,buf,size);  //memcpy ->inbuilt function in std-c
}

int main()
{
  int array1[] = {1, 2, 3};
  int array2[] = {10, 20, 30};
  swap(array1, array2, 3 * sizeof(int));

  int i;
  printf("array1: ");
  for (i = 0; i < 3; i++)
    printf(" %d", array1[i]);
  printf("
");

  printf("array2: ");
  for (i = 0; i < 3; i++)
    printf(" %d", array2[i]);
  printf("
");

  return 0;
}

做法/战略是尽量减少使用无效点* 。 在特定情况下需要这些援助。 如果你真的需要通过真空* ,你也应该通过点人的目标。

这种通用的互换功能将帮助您了解一般性的空白 *

#include<stdio.h>
void swap(void *vp1,void *vp2,int size)
{
        char buf[100];
        memcpy(buf,vp1,size);
        memcpy(vp1,vp2,size);
        memcpy(vp2,buf,size);  //memcpy ->inbuilt function in std-c
}

int main()
{
        int a=2,b=3;
        float d=5,e=7;
        swap(&a,&b,sizeof(int));
        swap(&d,&e,sizeof(float));
        printf("%d %d %.0f %.0f
",a,b,d,e);
return 0;
}

我们都知道,C类系统基本上是强奸,但试图不这样做。 你们仍然有一些处理一般类型的选择:工会和不透明点。

不管怎么说,如果一个通用的职能是作为参数的空白点,那么它就应当试图加以参照。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...