简体   繁体   English

不了解空指针/如何施放? (C 编程)

[英]Not Understanding Void Pointer / How to cast? (C Programming)

Suppose I want to write a function: int read_file (char *filename, void *abc) that reads the file, and puts the numbers in an array, which abc points to.假设我想编写一个函数: int read_file (char *filename, void *abc)读取文件,并将数字放入abc指向的数组中。 I must use the void pointer - I know how I would do it if it were int *abc instead of void, as I could treat abc syntactically like an array, and do stuff like abc[0]=1 , but here I can't do that, as it's a void pointer.我必须使用 void 指针 - 我知道如果它是int *abc而不是 void 我会怎么做,因为我可以在语法上像一个数组一样对待abc ,并且做像abc[0]=1这样的事情,但在这里我可以'不要这样做,因为它是一个空指针。

I'm not too familiar with void pointers, and how I should get this to work.我对空指针不太熟悉,我应该如何让它工作。 Please help!请帮忙! I prefer not to post code, as this is for a school assignment, and just want to know how I would put the information in the file into an array pointed to by abc , maybe with casting (not sure how to do that though).我不想发布代码,因为这是一个学校作业,我只想知道我如何将文件中的信息放入abc指向的数组中,也许使用强制转换(但不确定如何做)。

I am already familiar with putting file information into an array, if it's given by int abc .我已经熟悉将文件信息放入数组中,如果它是由int abc给出的。

If read_file is always called with an int array for the abc parameter, you can just copy it to an int pointer and work with that.如果始终使用abc参数的int数组调用read_file则可以将其复制到int指针并使用它。

int *p = abc;

In most cases, you need to cast when changing from one type to another, however a void * may be freely cast to or from any non-function pointer without a cast safely.在大多数情况下,从一种类型更改为另一种类型时需要强制转换,但是void *可以在没有安全转换的情况下自由转换为或从任何非函数指针转换。

Underneath the hood all pointers are the same - they're integers which represent memory addresses.在幕后,所有指针都是相同的——它们是代表内存地址的整数。 So you can cast them every which way.所以你可以以任何方式投射它们。 Just go:去吧:

int *p = (int *)abc;

And voila - you have your int * which you already know how to deal with.瞧 - 你有你的int * ,你已经知道如何处理了。 This is the "casting" thing, by the way - write the desired type in parentheses in front of your expression to "cast" it.顺便说一句,这是“强制转换”的事情 - 在表达式前面的括号中写入所需的类型以“强制转换”它。 It'll take the same bits and reinterpret them in a different way.它将采用相同的位并以不同的方式重新解释它们。

In a few cases C is actually smart enough to convert the data rather than blindly use the same bits.在少数情况下,C 实际上足够聪明来转换数据,而不是盲目地使用相同的位。 For example:例如:

float f = 3.75;
int i = (int)f;

In this case i will contain 3 because it rounds down in this case.在这种情况下, i将包含3因为在这种情况下它会向下舍入。 And an int is stored in memory differently than a float , so there is actual conversion going on here.并且int在内存中的存储方式与float不同,因此这里进行了实际的转换。

And in other cases it will forbid you to cast at all, because of how little sense it makes:在其他情况下,它会完全禁止你施放,因为它没有多大意义:

char *c = "Hello, world!";
float f = (float)c;

But quite often you can get away with it.但很多时候你可以逃脱它。 Especially with pointers, everything is fair game.尤其是三分球,一切都是公平的。 Now, mind you, with great power comes great responsibility.现在,请注意,能力越大,责任越大。 Although you can do it, doesn't mean that the result will be sensible and that using it won't crash your program.尽管您可以做到,但这并不意味着结果是合理的,并且使用它不会使您的程序崩溃。 Be careful.当心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM