2

I get the error message "error: incompatible types when assigning to type ‘char[10]’ from type ‘char *’ ", when trying to initialize name in the following structure:

#include <stdio.h>

struct info
{
  char name[10]; 
  int years;
};

int main(void){
  struct info  b;

  b.name = "Michael";
  b.years = 19;
  return 0 
;}
1
  • array is different from pointer, check out the link in Jay's answer Commented Jun 6, 2012 at 5:42

6 Answers 6

4

Since you're just trying to initialize your struct, you could probably get away with:

 struct info b = {"Michael", 19};

C makes a distinction between initialization and assignment (even though they use the same operator). Arrays cannot be assigned, (memcpy or similar must be used instead) but data can come into existence with a certain value, which the initializer specifies.

Sign up to request clarification or add additional context in comments.

Comments

2

Use this instead:

strcpy(b.name,"Michael");

5 Comments

Is there a way to do it without using the string library? I don't understand why this doesn't work.
@Michael_19: What you call "string library", is actually part of the C standard library. You should be ok using it, unless you have a good reason for not linking your program with the C standard library.
@Michael_19: if you prefer, you can use loop instead of: struct info in; char name[] = "Michael"; int i = 0; while(i < 9) { in.name[i] = name[i]; i ++; } in.name[i] = '\0';
...that's basically same as strcpy() function.
He is asking for initialization, your answer doesn't do that but does something equivalent to assignment. See @Dave's answer for clear and simple initialization.
2

Arrays are not pointers in C. You need to strcpy to copy a string into another location.

Comments

1

Let's see what happens when the main() runs.

struct info  b;

By this you initialize the struct, including the name in it.

name is a char array (as Jay pointed out, it's different from a pointer), and now it is assigned a chunk of memory with some random stuff in it.

Next

b.name = "Michael";

By this you try to assign a string literal to your char array.

Normally, you assign string literal to char array in the declaration like this char a[] = "hello";

It will copy the string literal to a chunk of memory and let a "sit aside the memory". However in your case, b.name is already sitting aside another chunk of memory, and you cannot change the sitting by just do an assignment, because an array is not a pointer.

So, if you want to modify b.name, you either use strcpy as Vaughn Cato said, or you do that character by charater, like

b.name[0] = 'M';
b.name[1] = 'i';
...

which essentially is the same thing as 'strcpy'

Comments

0

if you do char name[10] you are staticaly allocating the memory whose base address can be accessed by the name variable. You cannot chage the address pointer to a statically allocated array. i.e you cannot add a different address to name variable.

In you do b.name = "Micheal", you are doing just that. You are trying to add the base address of "Micheal" string to name variable.

So to do that as @Vaughn said, copy the "Micheal" to the memory pointed by b.name. or in the structure, change char name[10] to char *name

Comments

0

The problem is that name's address is necessarily inside the memory for the info object containing it, while the string literal is in some distinct area of memory. The compiler makes sure their locations are sensible given where they're mentioned in your program, and they can't be moved or merged arbitrarily. Either:

  • name should be changed to a const char* and pointed at the literal (here, const char* as string literals must not be modified), or
  • the content from the literal strcpy()-ied into name (but you'll need to be careful to make sure name is always large enough for the text and terminating ASCII NUL/0 character), or
  • name changed to a char* and assigned to a strdup() heap-allocated copy of the literal (which ensures you can modify name afterwards, and that the memory controlled by name will be large enough to store a copy of the literal, but you will need to free(name) when you've finished with it).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.