Does void have a value ?

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm discussing something elsewhere and looking for a reference to how void is resolved to a true/false value. e.g.

 

void func1(void) {
  ...
}

void main(void) {
  while (func1) {
    ...
  }
}

avr-g++ compiles it but warns:

/Users/xxx/Documents/Arduino/temp1/temp1.ino:24:10: warning: the address of 'void display()' will never be NULL [-Waddress]
   while (display) {
          ^~~~~~~

Running the program, the while loop never exits, so I assume void resolves to a non-zero value i.e. it's 'true' because it's 'not false'. But I can't find a reference to this, and my google skills are failing me.

 

This topic has a solution.
Last Edited: Wed. May 6, 2020 - 09:31 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That is not void, that's the address of the function display...

 

void would be the return of the function, i.e display()

 

Changing your code, I get this error:

error: void value not ignored as it ought to be

:: Morten

 

(yes, I work for Microchip, yes, I do this in my spare time, now stop sending PMs)

 

The postings on this site are my own and do not represent Microchip’s positions, strategies, or opinions.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

And to answer your question, void does not have a value, so it does not have a boolean value...

:: Morten

 

(yes, I work for Microchip, yes, I do this in my spare time, now stop sending PMs)

 

The postings on this site are my own and do not represent Microchip’s positions, strategies, or opinions.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks. My typo frown Or rather, blithely typing in someone else's code.

 

g++ now says:

/Users/xxx/Documents/Arduino/temp1/temp1.ino: In function 'void setup()':
temp1:24:17: error: could not convert 'display()' from 'void' to 'bool'
   while (display()) {
          ~~~~~~~^~
exit status 1
could not convert 'display()' from 'void' to 'bool'
 

Last Edited: Wed. May 6, 2020 - 09:37 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As Morten says "func1" (or "display") is the address of the function so is not 0 (and not 'void'). I guess you mean while(display())? But now you are testing the return from a function that is defined as not returning a value which does not make much sense.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, I'm guilty of blithely pasting in someone else's code, rather than reading it properly.

 

Thanks.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Next you're gonna ask what is sizeof(void) cheeky

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well according to King James, it's large enough to contain waters...

 

<g,d,r>

 

Neil

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

obdevel wrote:

Yes, I'm guilty of blithely pasting in someone else's code, rather than reading it properly.

I wouldn't listen to anything this "someone else" has to say about programming if I were you. Two pretty serious errors in as many lines, that's quite an achievement.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

BTW when I try:

void foo() {}

int main(void) {
	while(foo());
}

then you cannot even build this:

		.././main.c: In function 'main':
D:\test\test\main.c(6,8): error: void value not ignored as it ought to be
		  while(foo());
		        ^
		make: *** [main.o] Error 1

So how a void return is interpreted is kind of a moot question as you just cannot do it anyway.