Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

void* pointer.

Status
Not open for further replies.

alphacat

New Member
can someone please refer me to a good breif about how to define a void* in a function parameters and how to use it inside the function?

Thanks.
 
Just don't use it. It's not really needed and may be considered by some to be bad practice.
 
Now that I read what you said, why would you that its not really needed?

I want to write a function that reads Len bytes from flash memory, and will store it in an local RAM variable.
The thing is that the function dont know what Len it will receive, If Len = 1, then the type of the Local Ram Variable will be char, but if Len = 2, then it would be int.

So i wanted to write a function like that:
void ReadFromFlash( int FlashAddress, int Len, void* Local_Ram_Variable);

The problem is that when I read the value from Flash, I cant perform:
*Local_Ram_Variable = value.

I cant also do casting to value because I dont know if its type is char or int or something else.

So how can i execute this function?
 
If the function does not know how many bytes should be read starting at the pointer, then how does it know when to stop reading? It sounds more like the problem is that you are underspecifying your function parameters.

That said, you can use function templates, then you won't need an extra parameter that tells the function how many bytes should be read.
 
Last edited:
I don't understand your problem.

Why can't you just do,
Code:
void ReadFromFlash( int FlashAddress, int Len, void* Local_Ram_Variable){
   *Local_Ram_Variable=ReadFlash(FlashAddress);
    if(Len==2)
        *(Local_Ram_Variable+1)=ReadFlash(FlashAddress+1);
}

Mike.
 
Now that I read what you said, why would you that its not really needed?

I want to write a function that reads Len bytes from flash memory, and will store it in an local RAM variable.
The thing is that the function dont know what Len it will receive, If Len = 1, then the type of the Local Ram Variable will be char, but if Len = 2, then it would be int.

So i wanted to write a function like that:
void ReadFromFlash( int FlashAddress, int Len, void* Local_Ram_Variable);

The problem is that when I read the value from Flash, I cant perform:
*Local_Ram_Variable = value.

I cant also do casting to value because I dont know if its type is char or int or something else.

So how can i execute this function?

To do this in c-style you could do something like:
Code:
void ReadFromFlash( int FlashAddress, int Len, void* Local_Ram_Variable)
{
   switch (Len)
     {
     case 1:
        *(char*)Local_Ram_Variable = someChar;
        break;
     case 2:
        *(int*)Local_Ram_Variable = someInt;
        break;
     default:
        error...
     }
}
it's not very typesafe or portable, but if you just want something quick and dirty...
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top