Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6622571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:25:25+00:00 2026-05-25T21:25:25+00:00

please see my codes below #include <stdio.h> #include <stddef.h> typedef struct _node { int

  • 0

please see my codes below

#include <stdio.h>
#include <stddef.h>

typedef struct _node
{
int a;
char *s;
}Node, *nodePtr;

int main(int argc, char *argv[])
{
char *str = "string"; /*str points to satic storage area*/
Node nd;
nodePtr pNode = NULL;
size_t offset_of_s = offsetof(Node,s);

nd.a = 1;
nd.s = str;

pNode = &nd;

    /*Get addr of s, cast it to a different data types pointer, then de-reference it*/

/*this works, print "string"*/
printf("%s\n", *(int*)((char*)pNode + offset_of_s));

/*this sucks, print (null)*/
printf("%s\n", *(float*)((char*)pNode + offset_of_s));

return 0;
} 

i attempt to get the address of the s member of the Node structure, cast it to a data types not less than 4 bytes(4 byte is the width of a pointer on my machine), then de-reference the pointer as a argument to printf.

i do think the outcome of two printfs should be the same, but the second one displays “(null)” .

float and int have the same byte width on my machine, is the internal different representation of the two types that cause this ?

thanks in advance !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:25:26+00:00Added an answer on May 25, 2026 at 9:25 pm

    Your program invokes undefined behavior because the types of the arguments to printf() are not what printf expects. There is no way to predict the outcome by looking at the source code.

    C99-TC3, §7.19.6.1/9

    If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.

    However, if you are interested in the reason the behavior you’ve observed is as it is, it’s likely that your compiler is one of those that pass floating-point values to printf() in the floating-point CPU registers. (GNU and CLang do that, for example). The second call to printf placed the dereferenced value in a floating-point register, but printf, seeing the %s conversion specifier, looked at the register where a char* would have been passed, likely a general-purpose register, which happened to be zero in your case.

    PS: Here’s what GCC 4.6.1 makes out of it on my linux

    main:
        pushq   %rbx
        leal    .LC0(%rip), %ebx
        movl    $.LC1, %esi
        subq    $16, %rsp
        movl    %ebx, %edx
        movl    $1, %edi
        movq    $.LC0, 8(%rsp)
        xorl    %eax, %eax
        call    __printf_chk
    
        movd    %ebx, %xmm0
        movl    $.LC1, %esi
        movl    $1, %edi
        movl    $1, %eax
        unpcklps    %xmm0, %xmm0
        cvtps2pd    %xmm0, %xmm0 # this is where your value went
        call    __printf_chk     # is NOT gonna read from xmm0!
    
        addq    $16, %rsp
        xorl    %eax, %eax
        popq    %rbx
        ret
    

    Same story with clang 2.9

        ...
        movl    $.L.str, %ebx
        xorb    %al, %al
        movl    $.L.str1, %edi     # .L.str1 is your format "%s\n"
        movl    $.L.str, %esi      # .L.str  is your static "string"
        callq   printf
    
        movd    %ebx, %xmm0        # your value is in xmm0 again
        cvtss2sd    %xmm0, %xmm0   # promoted to double, but still in xmm0
        movb    $1, %al
        movl    $.L.str1, %edi
        callq   printf             # printf has no idea
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please see the code below: #include <windows.h> int main(int argc, char* argv[]) { HANDLE
Please see this piece of code: #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int i
In code below (please see comment): #include stdafx.h #include <iostream> using std::cout; struct Base
Please see the code below: #include <iostream> #include <stdlib.h> #include <time.h> using namespace std;
Please see the C++ code fragment below: #include ..... Class1 class1; Class2 class2; ...
Please see the below codes : private string GetUserIPAddress() { string User_IPAddress = string.Empty;
Please see code below. The destructors are never called. Anyone know why and how
I am using vb.net code. I have grid view, please see the code below:
I am using jquery datepicker, please see below code $(document).ready(function() { $(.txtDate).datepicker({ showOn: 'button',
I have a grid view control in my application. Please see the below code.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.