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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:16:57+00:00 2026-06-11T17:16:57+00:00

While running following code, my program crashes unexpectedly! #include<stdio.h> #include<string.h> int main(){ char *str

  • 0

While running following code, my program crashes unexpectedly!

#include<stdio.h>
#include<string.h>

int main(){

    char *str = NULL;
    strcpy(str, "swami");
    printf("%s", str);
    return 0;
}

But if I do like this:

#include<stdio.h>
#include<string.h>

int main(){

    char *str;
    strcpy(str, "swami");
    printf("%s", str);
    return 0;
}

This code works fine and generates correct output!

I am using gcc compiler(codeblocks IDE). Also, both the codes lead to program crash in DevCpp. Can anyone please explain me why this is so!?

  • 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-06-11T17:16:58+00:00Added an answer on June 11, 2026 at 5:16 pm

    You cannot write to NULL pointers.

    In the second case, it happened that your pointer was randomly initialized to a valid location in your program’s memory. That is why you could do a strcpy into it.

    Change both programs to have

    str = malloc(size)
    

    or the option with calloc before the strcpy. (size is the size of the space you want to reserve.)

    As per comment, you can also change the declaration of str to be char str[6] (or more).

    Last edit: I’ll present you this picture showing the memory of your program and the pointers:

    enter image description here

    The gray areas and the red one are forbidden (you cannot write or read from them; the top gray one is for kernel memory while the others are spaces not yet reclaimed). The red area at the bottom is the special 0 page. Since NULL is 0 your str = NULL will point to this and your program will fail.

    If you don’t assign anything to str it will end up pointing randomly. It can still point to the red area or to a grey area -> your program will fail. It could point to a green or blue (both hues) area, making your program work (excepting the cases where it is pointing to a read-only location and you write to it). Allocating area for the pointer makes it point to the green area, enlarging it to the top.

    The other option, with str[6] enlarges the stack area to bottom. All local variables have space reserved into the stack while all space allocated with malloc, realloc, calloc and other friends goes into the heap.

    Lastly, have a look at a blog article about the difference between char[] and char *.

    PS: If you’d want to use a GNU extension you can look into the asprintf function. It would allocate space for a string and write some content there:

    asprintf(&str, "swami");
    

    or

    asprintf(&str, "%d + %d == %d\n", 1, 2, 3);
    

    But, if you want portability, you’d stay away from this function.

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

Sidebar

Related Questions

The following two code snippets compiles without any errors/warnings but while running it crashes.
I have a Linear layout initialized. While running program, i am using following code
While I am running this code it showing me a following error:- Column 'virtuemart_product_id'
I am getting he following Exception while running my Quartz Schdular program. Below is
I am running a c++ program which tries the following code: socket(PF_INET6, SOCK_DGRAM, 0)
The following code: int z = 0; while(z < 4) { printf(iteration %d\n,z); sleep(1);
Imagine a program with two threads. They are running the following code (CAS refers
While running a perl program I encountered the following error *** glibc detected ***
I got the following error while running my perl script with the negative lookaround
I am getting the following error while running my MVC application, which uses the

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.