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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:52:03+00:00 2026-05-13T10:52:03+00:00

I was told by c-faq that compiler do different things to deal with a[i]

  • 0

I was told by c-faq that compiler do different things to deal with a[i] while a is an array or a pointer. Here’s an example from c-faq:

char a[] = "hello";
char *p = "world";

Given the declarations above, when the compiler sees the expression a[3], it emits code to start at the location “a”, move three past it, and fetch the character there. When it sees the expression p[3], it emits code to start at the location “p”, fetch the pointer value there, add three to the pointer, and finally fetch the character pointed to.

But I was told that when dealing with a[i], the compiler tends to convert a (which is an array) to a pointer-to-array. So I want to see assembly codes to find out which is right.

EDIT:

Here’s the source of this statement. c-faq
And note this sentence:

an expression of the form a[i] causes the array to decay into a pointer, following the rule above, and then to be subscripted just as would be a pointer variable in the expression p[i] (although the eventual memory accesses will be different, ”

I’m pretty confused of this: since a has decayed to pointer, then why does he mean about “memory accesses will be different?”

Here’s my code:

// array.cpp
#include <cstdio>
using namespace std;

int main()
{
    char a[6] = "hello";
    char *p = "world";
    printf("%c\n", a[3]);
    printf("%c\n", p[3]);
}

And here’s part of the assembly code I got using g++ -S array.cpp

    .file   "array.cpp" 
    .section    .rodata
.LC0:
    .string "world"
.LC1:
    .string "%c\n"
    .text
.globl main
    .type   main, @function
main:
.LFB2:
    leal    4(%esp), %ecx
.LCFI0:
    andl    $-16, %esp
    pushl   -4(%ecx)
.LCFI1:
    pushl   %ebp
.LCFI2:
    movl    %esp, %ebp
.LCFI3:
    pushl   %ecx
.LCFI4:
    subl    $36, %esp
.LCFI5:
    movl    $1819043176, -14(%ebp)
    movw    $111, -10(%ebp)
    movl    $.LC0, -8(%ebp)
    movzbl  -11(%ebp), %eax
    movsbl  %al,%eax
    movl    %eax, 4(%esp)
    movl    $.LC1, (%esp)
    call    printf
    movl    -8(%ebp), %eax
    addl    $3, %eax
    movzbl  (%eax), %eax
    movsbl  %al,%eax
    movl    %eax, 4(%esp)
    movl    $.LC1, (%esp)
    call    printf
    movl    $0, %eax
    addl    $36, %esp
    popl    %ecx
    popl    %ebp
    leal    -4(%ecx), %esp
    ret 

I can not figure out the mechanism of a[3] and p[3] from codes above. Such as:

  • where was “hello” initialized?
  • what does $1819043176 mean? maybe it’s the memory address of “hello” (address of a)?
  • I’m sure that “-11(%ebp)” means a[3], but why?
  • In “movl -8(%ebp), %eax”, content of poniter p is stored in EAX, right? So $.LC0 means content of pointer p?
  • What does “movsbl %al,%eax” mean?
  • And, note these 3 lines of codes:
    movl $1819043176, -14(%ebp)
    movw $111, -10(%ebp)
    movl $.LC0, -8(%ebp)

    The last one use “movl” but why did not it overwrite the content of -10(%ebp)? (I know the anser now :), the address is incremental and “movl $.LC0 -8(%ebp) will only overwrite {-8, -7, -6, -5}(%ebp))

I’m sorry but I’m totally confused of the mechanism, as well as assembly code…

Thank you very much for your help.

  • 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-13T10:52:03+00:00Added an answer on May 13, 2026 at 10:52 am

    a is a pointer to an array of chars. p is a pointer to a char which happens to, in this case, being pointed at a string-literal.

    movl    $1819043176, -14(%ebp)
    movw    $111, -10(%ebp)
    

    Initializes the local “hello” on the stack (that’s why it is referenced through ebp). Since there are more than 4bytes in “hello”, it takes two instructions.

    movzbl  -11(%ebp), %eax
    movsbl  %al,%eax
    

    References a[3]: the two step process is because of a limitation in terms of access to the memory referenced though ebp (my x86-fu is a bit rusty).

    movl -8(%ebp), %eax does indeed reference the p pointer.

    LC0 references a “relative memory” location: a fixed memory location will be allocated once the program is loaded in memory.

    movsbl %al,%eax means: “move single byte, lower” (give or take… I’d have to look it up… I am a bit rusty on this front). al represent a byte from the register eax.

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

Sidebar

Related Questions

I've always been told that adding an element to an array happens like this:
Somebody told me that example.com/en is more friendly than example.com/index.php?lang=en . How can I
Someone told me That Serialization was not the best way to send things over
Recently I have been told that static class/methods are evil. Take for example my
Someone told me about swamp diagrams explaning that they were useful to predict code
Someone told me that it's faster to concatenate strings with StringBuilder. I have changed
Someone told me that openGL is for graphic only, and that it's very bad
A reliable coder friend told me that Python's current multi-threading implementation is seriously buggy
After being told by at least 10 people on SO that version control was
A sysadmin teacher told me one day that I should learn to use make

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.