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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:16:03+00:00 2026-05-30T09:16:03+00:00

I have found myself unable to explain why the following piece of code works.

  • 0

I have found myself unable to explain why the following piece of code works. Needless to say, I am quite new to C++…

#include <cstdio>

void foo(char* p)
{
    p[0] = 'y';
}

int main()
{
    char a[1];
    a[0] = 'x';
    printf("a[0] = %c\n", a[0]);
    foo(a);
    printf("a[0] = %c\n", a[0]);
    return 0;
}

This program outputs

a[0] = x
a[0] = y

What intrigues me us that I am not passing a pointer, but an array, to foo. So how can foo change the value of the array a? Does this apply only to arrays of char?

The answer to Difference between char and char[1], confirms my observation, but it does not go into detail about why this is the case.

Thanks!

  • 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-30T09:16:05+00:00Added an answer on May 30, 2026 at 9:16 am

    In C, arrays, in most contexts (*), decay into a pointer to their first element.

    In your case, the array a decays into a pointer to a[0].

    The array int arr[12][23], when used just by its identifier, decays to a pointer to its first element, namely arr[0], which is of type int (*)[23] (pointer to array of 23 ints).

    (*) Arrays do not decay when used as the argument to the sizeof operator, or when used as argument to the & operator or when used as initializer (a string literal) for a character array.

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

Sidebar

Related Questions

I have found myself doing this in my code to 'cache' the work done
I am new to fairly new to AS3 and I have found myself needing
I have found myself having to turn a chunk of Lua code into a
I am new to android programming and have found myself stuck I have been
I am new to web development, and have found myself stuck on how to
For instance, I have just found myself writing the following traits like class: template<class
I am working on some reporting code and I have found myself repeating a
I have found myself repeatedly writing the following function : // This function will
I have found myself designing a language for fun that is a cross between
I am doing some C programming for school and I have found myself reusing

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.