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 question.(5)

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

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Error: assignment to expression with array type - how can i circumvent the error?
Next
Answered
Mia Noel
  • 30
Mia Noel
Asked: May 17, 20222022-05-17T12:49:57+00:00 2022-05-17T12:49:57+00:00In: c

Error: assignment to expression with array type – how can i circumvent the error?

  • 30

. Advertisement .

..3..

. Advertisement .

..4..

I’m trying to run a new project. I do a couple of things like this:

#include <stdio.h>
 
 #define N 30
 
 typedef struct{
  char name[N];
  char surname[N];
  int age;
 } data;
 
 int main() {
  data s1;
  s1.name="Paolo";
  s1.surname = "Rossi";
  s1.age = 19;
  getchar();
  return 0;
 }
s1.name="Paolo" 
 s1.surname="Rossi"
data s1 = {"Paolo", "Rossi", 19};

But in my program, I am getting the warning:

"error: assignment to expression with array type error"

Can someone explain why the “error: assignment to expression with array type” issue happened? Where have I gone wrong? Thank you!

assignment to expression
  • 2 2 Answers
  • 2k Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    dttutoria Expert
    2022-06-04T08:23:49+00:00Added an answer on June 4, 2022 at 8:23 am

    The cause: It shows the error “Not Assignable” because assigning a value to a string directly in C is not feasible.

    Solution:

    You should use strcpy() function to modify the value

    Do it in the following way:

    strcpy(s1.name , "Egzona"); 
    printf( "Name : %s\n", s1.name);
    • 24
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Julia Gilbert
    2022-05-25T19:57:18+00:00Added an answer on May 25, 2022 at 7:57 pm
    typedef struct{
     char name[30];
     char surname[30];
     int age;
    } data;

    data is a block of memory that can hold 60 characters plus 4 for the int. (See note).

    [----------------------------,------------------------------,----]
     ^ this is name ^ this is surname ^ this is age

    This allocates memory to the stack.

    data s1;

    Assignments are not just copies of numbers; sometimes they can be pointers.

    This is a failure

    s1.name = "Paulo";

    Because s1.name is at the beginning of a 64-byte-long struct, and "Paulo" is 6 bytes (6 because of trailing 0 in C string strings), the compiler can assign a pointer for a string to a string.

    To copy “Paulo into struct at point name and “Rossi into struct at Point surname.

    memcpy(s1.name, "Paulo", 6);
    memcpy(s1.surname, "Rossi", 6);
    s1.age = 1;

    What do you end up with?

    [Paulo0----------------------,Rossi0-------------------------,0001]

    strcpy does the exact same thing, but it knows about \0 termination and does not require the length to be hardcoded.

    Alternativly, you can also define a structure that points at char arrays any length.

    typedef struct {
     char *name;
     char *surname;
     int age;
    } data;

    This will result in

    [----,----,----]

    Now, you can fill the struct using pointers.

    s1.name = "Paulo";
    s1.surname = "Rossi";
    s1.age = 1;

    This is what it looks like

    [---4,--10,---1]

    Pointers are 4 and 10.

    Please note that the pointers and ints can have different sizes. For example, size 4 is 32bit.

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

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

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.