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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:52:22+00:00 2026-05-28T00:52:22+00:00

The program is: #include <stdio.h> #include <stdlib.h> int main(void) { char *a=abc,*ptr; ptr=a; ptr++;

  • 0

The program is:

#include <stdio.h>
#include <stdlib.h>
int main(void) {
    char *a="abc",*ptr;
    ptr=a;
    ptr++;
    *ptr='k';
    printf("%c",*ptr);
    return 0;
}

The problem is in the

*ptr='k';  

line, when I remove it program works normally. But I can’t figure out the reason.

  • 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-28T00:52:23+00:00Added an answer on May 28, 2026 at 12:52 am

    The problem is because you are trying to change the string literal "abc" with:

    char *a="abc",*ptr;
    ptr=a;                  // ptr points to the 'a'.
    ptr++;                  // now it points to the 'b'.
    *ptr='k';               // now you try to change the 'b' to a 'k'.
    

    That’s undefined behaviour. The standard explicitly states that you are not permitted to change string literals as per section 6.4.5 String literals of C99:

    It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

    It will work if you replace:

    char *a="abc",*ptr;
    

    with:

    char a[]="abc",*ptr;
    

    since that copies the string literal to a place that’s safe to modify.

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

Sidebar

Related Questions

See the following program: #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h> main(void){ printf(Array
My program is like this ( main.c ): #include <stdlib.h> #include <stdio.h> void main(){
Here is my program time_play.c : #include <stdlib.h> #include <stdio.h> #include <time.h> int main
So basically: #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h> int main(void){ //test strrev
I make this program :: #include<stdio.h> char *raw_input(char *msg); main() { char *s; *s
I am studying pointers and I encountered this program: #include <stdio.h> void swap(int *,int
When I write the following program: file 1: #include <stdio.h> int global; void print_global1()
I just translated this program, #include <stdio.h> int dam[1000][1000]; int main (int argc, const
The following C++ program compiles and runs as expected: #include <stdio.h> int main(int argc,
#include <stdio.h> #include <stdlib.h> #include <stdarg.h> void minprintf(char *fmt, ...) { va_list ap; char

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.