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

  • Home
  • SEARCH
  • 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 130915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:01:23+00:00 2026-05-11T06:01:23+00:00

I am a little surprised by the following. Example 1: char s[100] = abcd;

  • 0

I am a little surprised by the following.

Example 1:

char s[100] = 'abcd'; // declare and initialize - WORKS 

Example 2:

char s[100]; // declare s = 'hello'; // initalize - DOESN'T WORK ('lvalue required' error) 

I’m wondering why the second approach doesn’t work. It seems natural that it should (it works with other data types)? Could someone explain me the logic behind this?

  • 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. 2026-05-11T06:01:24+00:00Added an answer on May 11, 2026 at 6:01 am

    When initializing an array, C allows you to fill it with values. So

    char s[100] = 'abcd'; 

    is basically the same as

    int s[3] = { 1, 2, 3 }; 

    but it doesn’t allow you to do the assignment since s is an array and not a free pointer. The meaning of

    s = 'abcd'  

    is to assign the pointer value of abcd to s but you can’t change s since then nothing will be pointing to the array.
    This can and does work if s is a char* – a pointer that can point to anything.

    If you want to copy the string simple use strcpy.

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

Sidebar

Related Questions

I was a little surprised that the following code did not work as expected:
I'm a little surprised that it is not possible to set up a binding
Recently I have started learning rails and was a little surprised that the default
A little example TTest<T> = class private f : T; public function ToString :
I'm a little surprised that there isn't some information on this on the web,
I just found myself a little bit surprised being unable to simply use a
Possible Duplicate: What is the “cost” of .NET reflection? I'm a little surprised this
Trying to understand the behaviour of pointers in C, I was a little surprised
After starting a new Rails 3.0.5 project, I am surprised a little to find
This surprised me a little bit, but I was playing around with some code

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.