Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to assign to array?

How to assign to array?

Scheduled Pinned Locked Moved C / C++ / MFC
questioncsharpjavadata-structureshelp
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    milestanley
    wrote on last edited by
    #1

    As follows:

    int arr[], brr[];
    arr = brr; //error: cannot assign one array to another,
    //and I have a question, Why can't, but other language(i.e java,C#) can do that?

    so, how to do that assign one array to another?

    I _ 2 Replies Last reply
    0
    • M milestanley

      As follows:

      int arr[], brr[];
      arr = brr; //error: cannot assign one array to another,
      //and I have a question, Why can't, but other language(i.e java,C#) can do that?

      so, how to do that assign one array to another?

      I Offline
      I Offline
      includeh10
      wrote on last edited by
      #2

      Use: int*arr=&brr;

      T 1 Reply Last reply
      0
      • I includeh10

        Use: int*arr=&brr;

        T Offline
        T Offline
        Tim Craig
        wrote on last edited by
        #3

        That may not accomplish what he wants. If he's making a temporary copy to use as a scratch pad in a calculation and wants to preserve the original, it certainly won't. Your example just provides a different way to access the same data, not a copy.

        You measure democracy by the freedom it gives its dissidents, not the freedom it gives its assimilated conformists.

        1 Reply Last reply
        0
        • M milestanley

          As follows:

          int arr[], brr[];
          arr = brr; //error: cannot assign one array to another,
          //and I have a question, Why can't, but other language(i.e java,C#) can do that?

          so, how to do that assign one array to another?

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          For copying arrays, you can use functions like memcpy_s[^] and CopyMemory[^]. These functions require you to know the size of the array. In C++, ideally you should be using the STL containers like vector, list etc. and then you can use the copy[^] function to make a copy of a container using iterators as shown in the example in the documentation.

          «_Superman_» I love work. It gives me something to do between weekends.
          Microsoft MVP (Visual C++)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups