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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:51:51+00:00 2026-06-07T09:51:51+00:00

Why this code is not running? Why str1 is not assigned to str2 ??

  • 0

Why this code is not running? Why str1 is not assigned to str2 ?? I know i have an option of using strcpy but i wish to know the reason why this is not working??

#include<stdio.h>
int main()
{
char str1[]="hello";
char str2[10];
str2=str1;
printf("%s",str2);
return 0;
}

Whereas if I use pointers than it works like here..

#include<stdio.h>
int main()
(
char *s="good morning";
char *q;
q=s;
while(*q!='\0')
{
printf("%c",*q);
q++;
}
return 0;
}

This works.
Now the string has been copied via pointers so why such difference??

  • 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-07T09:51:52+00:00Added an answer on June 7, 2026 at 9:51 am

    You need to do

    strcpy (str2, str1)

    you need to copy each element of the str1 to str2 character by character.

    What you are doing is to attempt assign the address of the string in str1 assign to the array var str2 which is not allowed. Static arrays couldn’t be used to be assigned values.

    Even if you had char *str2; and then str2 = str1 although your code would work, but in such a case the string is not copied, instead the address of the string in str1 is copied in str2 so now dereferencing str2 will point to the string

    Also note that when you copy string into a char *str2 always allocate enough memory before copy.
    one possibility is:

    str2 = malloc (sizeof (char) * (strlen (str1) + 1));
    strcpy (str2, str1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Code is not running on .click when I have this: $(.cancel).click(function() { alert(got here);
This code is not pretty, but I want to print out the last generated
This code does not throw an error but the query fails, that is, the
this code is not running due to end errors. I can't figure out what's
This code does not seem to be running correctly: var addEventListener = (function ()
Why does this code not print an exception stack trace? public class Playground {
This is probably a quicky. Why does this code not return anything? import java.util.Scanner;
Possible Duplicate: Comparing Strings in Cocoa Why is this code not recognising the NSString
This code is not compilable. I can't find why in standard. Can someone explain?
This code does not function as expected: // $field contains the name of a

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.