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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:19:51+00:00 2026-06-04T03:19:51+00:00

I am coming back from after reading this c-faq question I am totaly confused

  • 0

I am coming back from after reading this c-faq question I am totaly confused what happening here.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main  ()
 {
   char ar[3]="NIS", *c;
   printf ("%s\n", ar);
   strcpy (c, ar);
   printf ("%s\n", c);
   if (ar[4] == '\0')
{
   printf ("Null");
 }
else 
  {
  printf ("%c\n", ar[4]);
  }
}

Here I have assigned “NIS” Equal size of declare array.and when i try to access ar[3],ar[4] it giving null why ? it’s ok in case of ar[3] but why in case of ar[4] Another thought: In c-faq it was mentioned that if you assign any string equal to the size of declared array, you can’t use printf (“%s”), and strcpy() on that array as it is mentioned in c-faq. But in my above code i have used printf as well as strcpy here both working fine.It might be i have interpreted wrong please correct me. and another question is that When I try to compare ar[5] with null it is not printing anything that’s ok but why it is printing Null for ar[4].My thought on this “NIS” String will store in memory like this..

Thanks in advance.

  --------------------------------------------------------
  |   N   |   I    |   S   |   /0   |  Garbage value here
  |_______|________|_______|________|_____________________
    ar[0]    ar[1]   ar[2]    ar[3]  

Well Here ar[3] is giving null when I compare it with ‘\0’ that’s ok but when I comapre it with ar[4] still it giving me null instead of some garbage value..

  • 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-04T03:19:56+00:00Added an answer on June 4, 2026 at 3:19 am

    Your code exhibits undefined behaviour. It works for you by chance, but on another machine it could fail. As you understood from the FAQ, the code is not valid. But that does not mean it will always fail. That is simply the nature of undefined behaviour. Literally anything can happen.

    Accessing ar[3] is illegal because that is beyond the end of the array. Valid indices for this array are 0, 1 and 2.

    You did not allocate memory for c so any de-referencing of the pointer is undefined behaviour.

    Your main declaration is wrong. You should write:

    int main(void)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm seeing some weird stuff happening in my app. After coming back to the
This is another question from cracking coding interview, I still have some doubt after
Here is what I'm trying to accomplish. I have an object coming back from
I'm having a lot of trouble with Ruby after coming back to it from
How can I set the content inset on a UITableView after coming back from
I have a newbie question! I have a problem coming back from a view
Am coming back to building a FB app after some time away from the
After a break from Python(and I knew very little then!) I'm coming back to
I've got XML coming back from an Ajax.Request in Ajax.Response.responseXML . My XML actually
I have the following JSON coming back from a web service which I am

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.