Thread: incompatible types when assigning to type char 20 from type char *

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    5

    incompatible types when assigning to type char 20 from type char *

    Code:
    1 #include <stdio.h>
      2 #include <string.h>
      3
      4 struct employee{
      5     char firstName[20];
      6     char lastName[20];
      7 };
      8
      9 int main()
     10 {
     11     struct employee aEmployee;
     12     struct employee *employeePtr;
     13     aEmployee.firstName = "Fred";
     14     aEmployee.lastName = "Smith";
     15     employeePtr = &aEmployee;
     16     printf("%s%s%s\n", aEmployee.firstName, "is a" aEmployee.lastName);
     17     printf("%s%s%s\n", employeePtr -> firstName, "is a", employeePtr -> lastName);
     18     printf("%s%s%s\n", (*employeePtr).firstName, "is a", (*employeePtr).lastName);
     19 return 0;
     20}
    lines 13 and 14 are giving me this error and I'm not quite sure why if someone would just help me figure out this I know it should be easy but I'm getting quite frustrated trying to figure it out

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    An array is not a pointer, so cannot be used on the left hand side of an assignment. Which is what your code is doing on lines 13 and 14.

    Look up the strcpy() function (which is within <string.h>, which your code already #include's even though it doesn't use any functions from it).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-02-2014, 05:29 PM
  2. Replies: 4
    Last Post: 07-24-2012, 10:41 AM
  3. Replies: 2
    Last Post: 11-23-2011, 12:30 PM
  4. Replies: 17
    Last Post: 03-06-2008, 02:32 PM
  5. Converting type string to type const char*
    By rusty0412 in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2003, 05:59 PM