Assigning a value to an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NewProgrammer1
    New Member
    • Nov 2011
    • 2

    Assigning a value to an array

    i need to assign a value to an array but it is not in the normal way, here is the code so far:
    Department[1] = "Accounts";
    Department[2] = "Teller";
    Department[3] = "Loans";
    Department[4] = "Operations ";
    Department[5] = "Personnel" ;
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    in c you cannot just assign a literal string.

    use strcpy function, if you are using wide string use wcscpy but if you are using multibyte string you better use strncpy but strcpy will do the same purpose

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      declare the array like this....note the first element in an array is element[0].
      string Department [6]= {"","Accounts", "Teller","Loans :,"Operations", "Personnel" };

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Do you intend to skip the first element in the Department array; that is, Department[0]?

        I can think of several possibilities for the type of array variable Department. What did you choose for its type? Each data type has a preferred way to do assignments.

        Comment

        • NewProgrammer1
          New Member
          • Nov 2011
          • 2

          #5
          an example of how this could be done??
          could this work:
          strcpy ( department1, "Accounts" );

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            What is the type of array variable Department?

            Do the values of the Department array change while the program runs or do the initial values persist?

            Comment

            Working...