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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:43:34+00:00 2026-05-25T17:43:34+00:00

#include<stdio.h> #include<string.h> int main() { char a[]=aaa; char *b=bbb; strcpy(a,cc); printf(%s,a); strcpy(b,dd); printf(%s,b); return

  • 0
#include<stdio.h>
#include<string.h>
int main()
{
        char a[]="aaa";
        char *b="bbb";
        strcpy(a,"cc");
        printf("%s",a);
        strcpy(b,"dd");
        printf("%s",b);
        return 0;
}

We could not modify the contents of the array but the above program does not show any compile time error.When run it printed cc and terminated. The contents of the array i think will get stored in the read only section of the data segment and so its not possible to change the value of array as its a const.But here in the above program the value got changed to cc and the program terminated.The value got changed here why is it so.Please help me understand.

  • 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-25T17:43:34+00:00Added an answer on May 25, 2026 at 5:43 pm

    Here’s a hypothetical memory map showing how the string literals, array, and pointer all relate to each other:

    Item              Address            0x00  0x01  0x02  0x03
    ----              -------            ----  ----  ----  ----
    "aaa"             0x00040000          'a'   'a'   'a'  0x00
    "bbb"             0x00040004          'b'   'b'   'b'  0x00
    "cc"              0x00040008          'c'   'c'   0x00 ???
    "dd"              0x0004000C          'd'   'd'   0x00 ???
    ...
      a               0x08000000          'a'   'a'   'a'  0x00
      b               0x08000004          0x00  0x04  0x00 0x00
    

    This is the situation at line 6 in your code, after a and b have been declared and initialized. The string literals "aaa", "bbb", "cc", and "dd" all reside somewhere in memory such that they exist over the lifetime of the program. They are stored as arrays of char (const char in C++). Attempting to modify the contents of a string literal (in the case of this hypothetical, attempting to write to any memory location starting with 0x0004) invokes undefined behavior. Some platforms store string literals in read-only memory, some store them in writable memory, but in all cases, they should be treated as though they are unwritable.

    The object a is an array of char, and it has been initialized with the contents of the string literal "aaa". The object b is a pointer to char, and it has been initialized with the address of the string literal "bbb". In the line

    strcpy(a, "cc");
    

    you’re copying the contents of the string literal "cc" to a; after the line is executed, your memory map looks like this:

    Item              Address            0x00  0x01  0x02  0x03
    ----              -------            ----  ----  ----  ----
    "aaa"             0x00040000          'a'   'a'   'a'  0x00
    "bbb"             0x00040004          'b'   'b'   'b'  0x00
    "cc"              0x00040008          'c'   'c'   0x00 ???
    "dd"              0x0004000C          'd'   'd'   0x00 ???
    ...
      a               0x08000000          'c'   'c'   0x00 0x00
      b               0x08000004          0x00  0x04  0x00 0x00
    

    So when you print a to standard output, you should see the string cc. Note: printf is buffered, so it’s possible that output may not be written to the terminal immediately – either add a newline character to the format string (printf("%s\n", a);) or call fflush(stdout); after the printf to make sure all your output shows up.

    In line 9, you attempt to copy the contents of the string literal "dd" to the location pointed to by b; unfortunately, b points to another string literal, which as mentioned above invokes undefined behavior. At this point, your program could literally do anything from run as expected to crash outright to anything in between. This could be part of the reason you only see the output for cc.

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

Sidebar

Related Questions

#include<string.h> #include<stdio.h> int main() { char* charPtr=see me; printf(%s\n, charPtr); printf(%d, charPtr); return 0;
#include<stdio.h> int main() { char *p=mystring; return 0; } String literal mystring, where will
#include<string.h> #include<stdio.h> int main() { char *p; strcpy(p,hello world); } Well,Does it show undefined
#include <stdio.h> #include <string.h> int main(){ char *command=0; do { printf([A]dd, [P]rint, [Q]uit\n); scanf(%s,
Possible Duplicate: Segmentation Fault - C #include<stdio.h> #include<string.h> int main() { char *p; printf(enter
Code : #include stdio.h #include string.h int main() { char *p = abc; printf(p
#include <stdio.h> #include <string.h> int main() { char* p = new char[10]; memset(p,0,10); printf(%c,*p);
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { // int char str[40],ch; FILE*fp,*fp1,*fp2; fp=fopen(ide_input,w); fp1=fopen(error_log,w); fp2=fopen(lex_output,w);
#include <string.h> #include <stdlib.h> #include <stdio.h> int main(void) { unsigned char *stole; unsigned char
#include<stdio.h> #include<zlib.h> #include<unistd.h> #include<string.h> int main(int argc, char *argv[]) { char *path=NULL; size_t size;

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.