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 8770169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:30:39+00:00 2026-06-13T17:30:39+00:00

While looking at: Can a C compiler add padding before the first element in

  • 0

While looking at:

Can a C compiler add padding before the first element in a structure?

I came up with the following code:
(Ignore the fact that memory isn’t freed in this example.)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    char *cstr;
    size_t len;
} str_t;

void setStr(str_t* dest, const char* src)
{
    size_t len = strlen(src);
    dest->cstr = malloc(len + 1);
    dest->len = len;
    memcpy(dest->cstr, src, len + 1);
}

int main(void)
{
    str_t str;
    setStr(&str, "woot!");
    printf("%s\n", str);
    return 0;
}

Amazingly, this actually works. This call:

printf("%s\n", str);

seems to be equivalent to this one:

printf("%s\n", str.cstr);

So one would think that the following is also possible:

char* plainstr = malloc(str.len + 1);
strcpy(plainstr, str);

But no go. In contrast to printf, strcpy is not variadic, so there’s type checking. The compiler rightfully complains:

passing 'str_t' to parameter of incompatible type 'const char *'

But trying to tell the compiler “I really mean it” by casting it:

strcpy(plainstr, (const char*)str);

Won’t work either:

operand of type 'str_t' where arithmetic or pointer type is required

Note that the following can’t work:

strcpy(plainstr, (const char*)&str);

Since str.cstr != &str. For example, the output of this:

printf("%p %p\n", str.cstr, &str);

Is the following:

0xbdb010 0x7fff788f6ab8

And indeed, garbage data is being copied to plainstr.

So the questions are:

  1. Why isn’t it allowed to cast a struct to a pointer type?
  2. How come that printf deals with this correctly if casting isn’t allowed?
  • 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-06-13T17:30:40+00:00Added an answer on June 13, 2026 at 5:30 pm

    Why isn’t it allowed to cast a struct to a pointer type?

    Because it makes no sense. How would you reinterpret a whole bunch of possibly unrelated information of distinct types as a concise memory address? However, in the previous question you asked, all of the people who answered, cited the C standard, and one particular statement in the standard stated that

    The address of the structure is the address of its first element

    So (as @Mat already pointed it out), you can indeed write

    strcpy(destination, *(const char **)&str);
    

    and that “will work” for the reasons I just enumerated.

    How come that printf deals with this correctly if casting isn’t allowed?

    Because in C, typecasting is often just for fooling the compiler (except when it isn’t). By passing the structure, the structure will be copied, and your stack will be something like (I’m intentionally omitting any padding from the structure for the sake of simplicity):

    > top of the stack: pointer to the format string
    > address of the copied struct *and*  address of the copy of the char pointer
    > address of the length of the string (size_t)
    > every other stuff
    

    So, now what printf() will do is:

    • pop off the first value off the stack. It will be the format string.
    • Now when it encounters the %s format specifier in the format string, it will pop off another char pointer – in reality, it’s the pointer to the structure, and the pointer to the first element, which is the string to be printed.
    • So it happily prints the string and returns.

    Also, this is still undefined behavior, despite the fact that it works – if you don’t specify a format string for printf() that actually corresponds to the types you pass in as its variadic arguments, that’s not conformant and you can expect anything to happen.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While looking through some old code I came across this gem: MyObject o =
while looking at some code I stumbled onto: throw /*-->*/new std::exception (//... and I
While looking at some conceptual questions in C,I came across this question in a
While looking at the Timer documentation I ran across the following example with this
Can the compiler make automatic use of SSE2 while optimisations are disabled? When optimisations
I am looking for a JIT compiler or a small compiler library that can
While looking at Linux kernel's implementation of doubly linked circular lists, I've found following
While looking into parallel programming, and subsequently evaluation strategies, the question whether thunks are
While looking up the answer to this question: Why is an out parameter not
While looking for a way to temporarily save the search results when a user

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.