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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:29:09+00:00 2026-05-29T22:29:09+00:00

As a non-C/C++ expert I always considered square brackets and pointers arrays as equal.

  • 0

As a non-C/C++ expert I always considered square brackets and pointers arrays as equal.

ie :

char *my_array_star;
char my_array_square[];

But I noticed that when use in a structure/class they don’t behave the same :

typedef struct {
   char whatever;
   char *my_array_star;
} my_struct_star;

typedef struct {
   char whatever;
   char my_array_square[];
} my_struct_square;

The line below displays 16, whatever takes 1 byte, my_array_pointer takes 8 bytes.
Due to the padding the total structure size is 16.

printf("my_struct_star: %li\n",sizeof(my_struct_star));

The line below displays 1, whatever takes 1 byte, my_array_pointer isn’t taken in account.

printf("my_struct_square: %li\n",sizeof(my_struct_square));

By playing around I noticed that square brackets are used as extra space in the structure

my_struct_square  *i=malloc(2);

i->whatever='A';
i->my_array_square[0]='B';

the line blow displays A:

printf("i[0]=%c\n",((char*)i)[0]);

the line blow displays B:

printf("i[1]=%c\n",((char*)i)[1]);

So I cannot say anymore that square brackets are equals to pointers. But I’d like to understand the reason of that behavior. I’m afraid of missing a key concept of that languages.

  • 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-29T22:29:10+00:00Added an answer on May 29, 2026 at 10:29 pm

    Arrays and pointers don’t behave the same because they’re not the same at all, it just seems that way.

    Arrays are a group of contiguous items while a pointer is … well … a pointer to a single item.

    That single item being pointed to may well be the first in an array so that you can access the others as well, but the pointer itself neither knows nor cares about that.

    The reason that arrays and pointers often seem to be identical is that, in many cases, an array will decay to a pointer to the first element of that array.

    One of the places this happens is in function calls. When you pass an array to a function, it decays into a pointer. That’s why things like the size of an array don’t pass through to the function explicitly. By that I mean:

    #include <stdio.h>
    
    static void fn (char plugh[]) {
        printf ("size = %d\n", sizeof(plugh)); // will give char* size (4 for me).
    }
    
    int main (void) {
        char xyzzy[10];
        printf ("size = %d\n", sizeof(xyzzy)); // will give 10.
        fn (xyzzy);
    
        return 0;
    }
    

    The other thing you’ll find is that, while you can plugh++ and plugh-- to your hearts content (as long as you don’t dereference outside of the array), you can’t do that with the array xyzzy.

    In your two structures, there’s a major difference. In the pointer version, you have a fixed size pointer inside the structure, which will point to an item outside of the structure.

    That’s why it takes up space – your 8-byte pointer is aligned to an 8-byte boundary as follows:

    +----------------+
    | 1 char variable|
    +----------------+
    | 7 char padding |
    +----------------+
    | 8 char pointer |
    +----------------+
    

    With the “unbounded” array, you have it inside the structure and you can make it as big as you want – you just have to allocate enough memory when you create the variable. By default (ie, according to the sizeof), the size is zero:

    +----------------+
    | 1 char variable|
    +----------------+
    | 0 char array   |
    +----------------+
    

    But you can allocate more space, for example:

    typedef struct {
       char whatever;
       char my_array_square[];
    } my_struct_square;
    
    my_struct_square twisty = malloc (sizeof (my_struct_square) + 10);
    

    gives you a variable twisty which has a whatever character and an array of ten characters called my_array_square.

    These unbounded arrays can only appear at the end of a structure and there can be only one (otherwise the compiler would have no idea where these variable length section began and ended) and they’re specifically to allow arbitrarily sized arrays at the end of structures.

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

Sidebar

Related Questions

I have a non-.Net executable file that is included in my .net assembly as
I'm calling a non-.NET dll from my project using P/Invoke, meaning that the .dll
Note: Please note that the code below is essentially non-sense, and just for illustration
I have a service provider that requires a non-standard Authorization Type for their web
I'm not an expert on lower-level non-object-oriented programming languages, and I'm in the middle
Non inline function defined in header file with guards #if !defined(HEADER_RANDOM_H) #define HEADER_RANDOM_H void
The non-virtual interface idiom describes how the virtual methods are nonpublic customisation points, and
A non-believer of C# was asking me what the purpose to extension methods was.
Custom non-fatal exception derive from which class in asp.net 3.5?
hobbyist/non-profession programmer here. I don't have a strong grasp on network addressing. I have

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.