Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 从空指针传输值_C_Pointers_Char_Void_Memcpy - Fatal编程技术网

C 从空指针传输值

C 从空指针传输值,c,pointers,char,void,memcpy,C,Pointers,Char,Void,Memcpy,有人能告诉我这个代码有什么问题吗 base是指向一组浮动的空指针 i是一个值>1 size是类型的大小(在本例中为float-4) 这是输出: 2:136724=136728 3:136724=136732 4:136728=136736 6:136732=136744 7:136732=136748 8:136736=136752如果a是void*,编译器将不允许您编写a+size*i(您不能对不完整的类型执行指针算术)。可能不是你想象的那种类型 但你为什么认为有问题?左侧列的前进速度是右侧列

有人能告诉我这个代码有什么问题吗

base
是指向一组
浮动的空指针

i
是一个值
>1

size
是类型的大小(在本例中为
float
-
4

这是输出:

2:136724=136728
3:136724=136732
4:136728=136736
6:136732=136744
7:136732=136748

8:136736=136752

如果
a
void*
,编译器将不允许您编写
a+size*i
(您不能对不完整的类型执行指针算术)。可能不是你想象的那种类型

但你为什么认为有问题?左侧列的前进速度是右侧列的一半,这是预期的,因为您要除以2

您确实意识到您正在打印地址,而不是正在复制的值,对吗

char *a = (char *)base;
char *temp = (char *)a + size * (i/2);
printf("%d: %f = ", i, *(float *)temp);
memcpy(a + size * i , temp , size);
printf("%f\n", *(float *)(a + size * i));
我所做的更改是正确地取消引用指针(并将它们转换为正确的类型),并将%d更改为%f,因为您指定base是一个浮点数组。%d代表整数,%f代表浮点数


您的代码不起作用的原因是您打印的是地址,而不是值。

请告诉我们您想要完成什么?看起来像一个 家庭作业问题,对吗

C语言允许您将任何指针强制转换为void*,然后再强制转换它 返回到原始指针类型,而不丢失任何信息。任何东西 否则使用空指针是个坏主意,尽管有些库函数 (如memcpy)由于历史原因仍然具有void*。这也是为什么 从任何指针类型到void*都不需要显式转换

你不能看到虚空*指向的是什么,除非你把它扔回原处 正确的指针类型。当你这样做的时候要小心

#include <stdio.h>
#include <memory.h>

/* It's a bad idea to pass Base as a void pointer,
   but that's what you said you have. */
void silly_function(void*base, int i, int size) {
    /* Using a char* that points to float, is an even worse idea!
        char *a = (char *)base;
        char *temp = (char *)a + size * (i/2);
        printf("%d: %d = ", i, temp);
        memcpy(a + size * i , temp , size);
        printf("%d\n", a + size * i); 
    **/

    /** Probably ought to have a big SWITCH statement here, based
        on the data type. sizeof() isn't a good way to do this...
        On many computers, sizeof(float)==sizeof(long), but that
        doesn't mean that a float* is the same as a long* !!!
        For now, I'm going to assume (as you did) that base points
        to an array of float. */

    /* I think you're trying to copy the first half of the array
       into the second half of the array! But that's easy. */
    float*firsthalf = (float*)base; 
    float*secondhalf = firsthalf + (i/2);

    /* Show some starting values. */
    printf("Before: %x --> %f, %x --> %f\n",
        firsthalf, *firsthalf, secondhalf, *secondhalf);

    /* Now do the copy */
    memcpy(secondhalf, firsthalf, (i/2)*(sizeof(float)));

    /* Now prove that it's been copied? */
    printf("After:  %x --> %f, %x --> %f\n",
        firsthalf, *firsthalf, secondhalf, *secondhalf);
}

int main() {
    /* This drives the test */
    float ary[10] = {
        1.1f, 2.2f, 3.3f, 4.4f, 5.5f,
        0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
    silly_function(ary, 10, sizeof(ary[0]));
    return 0;
}

我希望这能有所帮助。

我不相信这段代码有这样的输出。如果
a
确实是一个
void*
那么
a+size*i
是无效的,因为你不能对一个void指针执行算术运算。你怎么会认为它有问题?您期望的结果是什么?也许你想去引用一些指针来打印它们所指向的值?你只是打印他们存储的地址,这不是很有趣。Base是一个空指针,a是一个char指针。代码已更新。我如何打印这些值?@Kamran224:你在说什么“值”?
a[I]=a[I/2]如何使用指针实现这一点?我来自C++和java后台,没有C标准中的内存。
#include <stdio.h>
#include <memory.h>

/* It's a bad idea to pass Base as a void pointer,
   but that's what you said you have. */
void silly_function(void*base, int i, int size) {
    /* Using a char* that points to float, is an even worse idea!
        char *a = (char *)base;
        char *temp = (char *)a + size * (i/2);
        printf("%d: %d = ", i, temp);
        memcpy(a + size * i , temp , size);
        printf("%d\n", a + size * i); 
    **/

    /** Probably ought to have a big SWITCH statement here, based
        on the data type. sizeof() isn't a good way to do this...
        On many computers, sizeof(float)==sizeof(long), but that
        doesn't mean that a float* is the same as a long* !!!
        For now, I'm going to assume (as you did) that base points
        to an array of float. */

    /* I think you're trying to copy the first half of the array
       into the second half of the array! But that's easy. */
    float*firsthalf = (float*)base; 
    float*secondhalf = firsthalf + (i/2);

    /* Show some starting values. */
    printf("Before: %x --> %f, %x --> %f\n",
        firsthalf, *firsthalf, secondhalf, *secondhalf);

    /* Now do the copy */
    memcpy(secondhalf, firsthalf, (i/2)*(sizeof(float)));

    /* Now prove that it's been copied? */
    printf("After:  %x --> %f, %x --> %f\n",
        firsthalf, *firsthalf, secondhalf, *secondhalf);
}

int main() {
    /* This drives the test */
    float ary[10] = {
        1.1f, 2.2f, 3.3f, 4.4f, 5.5f,
        0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
    silly_function(ary, 10, sizeof(ary[0]));
    return 0;
}
Before: 12ff38 --> 1.100000, 12ff4c --> 0.000000
After:  12ff38 --> 1.100000, 12ff4c --> 1.100000