-4

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. 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.

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).

I am already familiar with putting file information into an array, if it's given by int abc.

Harambe17
  • 175
  • 7
  • 1
    Not clear what you ask. See [ask] and provide a [mcve]. – too honest for this site Jan 17 '17 at 19:10
  • @Olaf - What's not clear? The guy has a function which gets passed a `void *` parameter (which he knows contains a pointer to an `int` array), and he wonders how he can get to the `int` array to work with it. It's homework, he's obviously learning the fundamentals of C. – Vilx- Jan 17 '17 at 19:12
  • Why must you use ` void *` in the function argument list? Do you know that it is always an array of `int` that is being pointed to? If you do have to use a `void *` in the function argument list, but you know it is always an `int *` behind the scenes, you can use `int read_file(char *filename, void *vp_abc) { int *abc = vp_abc; …rest of code as before… }` — simply coerce the pointless void pointer into the type you really want. – Jonathan Leffler Jan 17 '17 at 19:15
  • I didn't know you could do that. I tried some type-casting which I saw, and it didn't work. I will try to fix my program with that. – Harambe17 Jan 17 '17 at 19:17
  • OK, so suppose I do what you wrote ^ (and it compiles), but now I want to do `int main () { int stuff[1000]; read_file("file.txt", stuff); return 0;}` in order to get the file data into `stuff`, it gives me an error. What am I missing? – Harambe17 Jan 17 '17 at 19:25
  • Never mind - my array i used was too small! When I used a larger array, the program ran smoothly, and I got the desired printf results... thank you... – Harambe17 Jan 17 '17 at 19:29
  • @Vilx-: How do you know it is an `int *`? There is only some "if it was ..." statement. If that is, he should use `int *`, not `void *` - never use `void *` without true need. – too honest for this site Jan 17 '17 at 19:30
  • The school assignment required the void pointer in this case, just to clarify. And yes, that's a good point, thanks. – Harambe17 Jan 17 '17 at 19:37

2 Answers2

1

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.

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.

dbush
  • 205,898
  • 23
  • 218
  • 273
0

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. 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. For example:

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

In this case i will contain 3 because it rounds down in this case. And an int is stored in memory differently than a float, so there is actual conversion going on here.

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.

Vilx-
  • 104,512
  • 87
  • 279
  • 422