Microsoft KB Archive/34921

= INFO: jmp_buf Pointer Type is &quot;void *&quot; =

Article ID: 34921

Article Last Modified on 7/5/2005

-

APPLIES TO

 The C Run-Time (CRT), when used with:  Microsoft C Professional Development System 6.0a

 Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q34921

<div class="notice_section">

<div class="summary_section">

SUMMARY
The setjmp and longjmp functions accept a parameter of type jmp_buf. (The jmp_buf type is declared in SETJMP.H.) If you use the address of (&) operator with a variable of type jmp_buf and assign it to a jmp_buf pointer, Microsoft C 5.1 generates the following messages:

warning C4046: '&' on function/array, ignored

warning C4047: '=' : different levels of indirection

If you omit the address of operator and assign the value to a pointer of type jmp_buf, the compilers listed in the banner above generate the following message:

warning C4047: '=' : different levels of indirection

To address this situation, declare the pointer to have type &quot;void *.&quot; Then assign the value to the pointer without using the ampersand, as the code example below demonstrates.

<div class="moreinformation_section">

MORE INFORMATION
Because the SETJMP.H header file declares the jmp_buf type as an array of integers, the compiler generates the messages above when you use the address of operator. SETJMP.H uses an array type instead of a structure to be compatible with common C coding practices; section 4.6 of the &quot;Rationale for the ANSI C Standard&quot; provides additional information.

Basically, the setjmp function changes the jmp_buf parameter to record the destination for the longjmp function. Elsewhere, the standard defines setjmp to accept a parameter of type jmp_buf, not of type &quot;pointer to jmp_buf.&quot; Therefore, jmp_buf cannot be a structure; structures are passed by value and setjmp could not make the necessary changes. Because an array is passed by reference (that is, by its address), setjmp can make the necessary changes to an array parameter.

The declaration for badptr in the code example below declares it to be of type &quot;pointer to array of integer,&quot; not &quot;pointer to integer.&quot; Performing an assignment between different pointer types generates the C4047 warning. (To succeed without warning, badptr must be a &quot;pointer to integer,&quot; because that type is compatible with &quot;array of integer.&quot;)

Instead, the code uses goodptr, declared as a &quot;pointer to void,&quot; which supports the assignment without generating a warning. The code that uses goodptr is portable. Note that void pointers to not support indirection or subscripting (another form of indirection).

However, because your code does not know the contents of the jump buffer, this should not be a problem. (The contents of the jump buffer are implementation dependent and can vary between versions of the same compiler. Any code that directly manipulates the contents of a jump buffer is also implementation dependent and not portable.)

Sample Code
/* * Compiler options needed: none */


 * 1) include <setjmp.h>

jmp_buf buf; jmp_buf *badptr; void *goodptr;

void main {  badptr = &buf; /* C 5.1 warning C4046: '&' on function/array, ignored      */ /* C 5.1 warning C4047: '=' different levels of indirection */

badptr = buf; /* warning C4047: '=' : different levels of indirection */

goodptr = buf; }

Keywords: kbinfo kbcrt KB34921

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.