Thread: Placing string into array

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    18

    Placing string into array

    Code:
    dyofwk = get_julian_day(1, month, year);
    if (dyofwk % 7 == 0) week = "Monday";
    else if (dyofwk % 7 == 1) week = "Tuesday";
    else if (dyofwk % 7 == 2) week = "Wednesday";
    else if (dyofwk % 7 == 3) week = "Thursday";
    else if (dyofwk % 7 == 4) week = "Friday";
    else if (dyofwk % 7 == 5) week = "Saturday";
    else if (dyofwk % 7 == 6) week = "Sunday";
    compiler states invalid array assignment on lines 2-8, the function is fine

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Look up strcpy; you can NOT assign c-string in C.

    strcpy - C++ Reference

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    18
    is there any way to do this without using string.h?

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    18
    Code:
    if (dyofwk % 7 == 0) strcpy("Monday",week);
    else if (dyofwk % 7 == 1) strcpy("Tuesday",week);
    else if (dyofwk % 7 == 2) strcpy("Wednesday",week);
    else if (dyofwk % 7 == 3) strcpy("Thursday",week);
    else if (dyofwk % 7 == 4) strcpy("Friday",week);
    else if (dyofwk % 7 == 5) strcpy("Saturday",week);
    else if (dyofwk % 7 == 6) strcpy("Sunday",week);
    Deprecated conversion from string to char array

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You can try making "week" a const char pointer instead of an array.

    Just saw your code in the above post, you got the arguments to strcpy switched around. You cannot do it the way you were attempting. You were trying to copy data from week and into a string literal.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    18
    Thank you both for helping me, it is not having compiler errors for that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Placing Integers Into Character String
    By Illusionist15 in forum C Programming
    Replies: 3
    Last Post: 09-29-2010, 02:44 PM
  2. placing struct members into an array
    By droseman in forum C Programming
    Replies: 5
    Last Post: 01-27-2009, 09:18 AM
  3. Placing array in temp table
    By cjohnman in forum C Programming
    Replies: 3
    Last Post: 04-16-2008, 12:53 PM
  4. Placing special characters inside a string
    By forum member in forum C Programming
    Replies: 3
    Last Post: 12-07-2007, 08:43 AM
  5. Placing #'s from a file into an array
    By Cornot2C in forum C Programming
    Replies: 5
    Last Post: 03-18-2006, 12:29 AM