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

  • Home
  • SEARCH
  • 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 8675441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:57:01+00:00 2026-06-12T19:57:01+00:00

I want to understand: why it happens that sometimes a char[1] in C is

  • 0

I want to understand:

  • why it happens that sometimes a char[1] in C is used as char* (why doing this?) and
  • how the internals works (what’s going on)

Giving following sample program:

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

struct test_struct {
    char *a;
    char b[1];
} __attribute__((packed)); ;

int main() {

    char *testp;
    struct test_struct test_s;

    testp = NULL;
    memset(&test_s, 0, sizeof(struct test_struct));

    printf("sizeof(test_struct) is: %lx\n", sizeof(struct test_struct));

    printf("testp at: %p\n", &testp);
    printf("testp is: %p\n", testp);

    printf("test_s.a at: %p\n", &test_s.a);
    printf("test_s.a is: %p\n", test_s.a);

    printf("test_s.b at: %p\n", &test_s.b);
    printf("test_s.b is: %p\n", test_s.b);

    printf("sizeof(test_s.b): %lx \n", sizeof(test_s.b));

    printf("real sizeof(test_s.b): %lx \n", ((void *)(&test_s.b) - (void *)(&test_s.a)) );

    return 0;
}

I get the following output (OS X, 64bit):

sizeof(test_struct) is: 9
testp at: 0x7fff62211a98
testp is: 0x0
test_s.a at: 0x7fff62211a88
test_s.a is: 0x0
test_s.b at: 0x7fff62211a90
test_s.b is: 0x7fff62211a90
sizeof(test_s.b): 1 
real sizeof(test_s.b): 8 

Looking at the memory addresses, one can see that even the struct is 9 bytes large, 16 bytes were allocated which seems to be caused by char b[1]. But I’m not sure if those extra bytes were allocated due to optimization/mem alignment reasons, or if this has to do with C’s internal treatment of char arrays.

A real world example can be seen in <fts.h>:

`man 3 fts` shows the struct member `fts_name` as:

            char *fts_name;                 /* file name */

while /usr/include/fts.h defines the member as:

            char fts_name[1];               /* file name */

In the end, fts_name can really be used as a pointer to a C-string. For example, printing to stdout with printf("%s", ent->fts_name) works.

So if a char[1] is really one byte large, it couldn’t be used as a memory pointer on my 64bit machine. On the other hand, treating this as a full blown char * doesn’t work either, as can be seen with the test_s.b is output above, which should show a NULL pointer then…

  • 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-12T19:57:02+00:00Added an answer on June 12, 2026 at 7:57 pm

    Here is an answer that describes the char[1] trick. Basically, the idea is to allocate more memory when malloc()ing the struct, to already have some storage for your string without additional allocation. You can sometimes even see char something[0] used for the same purpose, which makes even less intuitive sense.

    On the other hand, treating this as a full blown char * doesn’t work
    either, as can be seen with the test_s.b is output above, which should
    show a NULL pointer then…

    If something is an array, both its name and &name just give the pointer to the start of the array in C. This works regardless of whether it’s a member in a struct, or a free standing variable.

    printf("real sizeof(test_s.b): %lx \n", ((void *)(&test_s.b) - (void *)(&test_s.a)) );
    

    This line gives the size of space allocated for a, not b in this struct. Put something after b and used this to subtract. With the packed attribute (which means you disallow the compiler to mess with alignment, etc.), you should get 1.

    #include <stdio.h>
    #include <string.h>
    
    struct test_struct {
        char *a;
        char b[1];
        char c;
    } __attribute__((packed));
    
    int main() {
      struct test_struct s;
      printf("%lx\n", ((void*)&s.c) - ((void*)&s.b));
      return 0;
    }
    

    I get 1.

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

Sidebar

Related Questions

I want to understand how a C++ program that was given to me works,
I want to understand why the following is happening. My guess is that a
first I want to say that I hope this doesn't look like I am
Quick question. I want to understand the behaviour of *this in C++. Forgive me
I experience the following apparently undocumented issue, and I want to understand if I
I want to verify that the user email is valid and turn this email
I want to understand what happens when an element whose CSS is display:block is
I am trying to make sure that I understand what happens when I add
This example is JavaScript, since that's where I'm using callbacks mostly. I want to
I want to understand principles of Clock cache replacement policy. What happens whrn it

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.