-4

How to store values from void function in main ?

Example

void UsartCallback (uchar* buf, uint len)
{
    return void(*buf);
}

int main()
{
    UsartCallback(&buf,1); // I can get values from void function but how to store this "*buf" value
    // For Example 
    uchar* data=UsartCallback(&buf); //Error: but i want get only "buf" value from void function.
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
user3764118
  • 65
  • 10

3 Answers3

2

You probably want this:

void *UsartCallback (uchar* buf, uint len)
{
  return (void*)buf;
}

int main()
{
  uchar *mybuf = UsartCallback(&buf,1);
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
0

This is invalid code, as it violates the constraint.

Quoting C11, chapter §6.8.6.4

A return statement with an expression shall not appear in a function whose return type is void. [...]

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

You can't. There is no such thing as a "void value". void is the absence of a value. It is true that it looks like you're returning a "thing" here, but you're not.

  • In C, this is illegal and your program doesn't even compile.

  • In C++, the code is accepted as an "oddity" of syntax (this is for making templates easier to implement). Your line is effectively the same as return;.


ok is there any way to get "buf" value from void and i want to store that in "main" .. is it possible ?

Okay, completely different question now.

buf is a pointer. So, yes, simply return it. Presumably your callback is required to return void* by some external force?

void* UsartCallback (uchar* buf, uint len)
{
    return buf;
}

I suggest you brush up on pointer syntax; your question (if I now understand it correctly; really hard to tell) betrays a lack of understanding of how to work with pointer types.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055