copying an **int array passed through parameters

This is a discussion on copying an **int array passed through parameters within the C++ Programming forums, part of the General Programming Boards category; Is there any other way to copy an array or rather 'pass by value'? Heres what im currently doing, I ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    copying an **int array passed through parameters

    Is there any other way to copy an array or rather 'pass by value'?

    Heres what im currently doing, I do not like it much... (I need to work with the data in array but I can't have it changed once the method returns.

    Code:
    int dijkstra(int buildings,int **tempEdges)
    {
        //crude way of copying an array
      	int **edges;
    	edges = new int*[buildings+1];
    	for(int i=0;i <= buildings;++i)
    		edges[i] = new int[buildings+1];
    	for(int i=0;i <= buildings;++i)
    	for(int j=0;j <= buildings;++j)
    		edges[i][j] = tempEdges[i][j];
    
    //jumble array around to work with it
    //return value
    }
    Any ideas?

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,032
    For primitive and POD types you can use the C function memcpy to copy blocks of continuous memory.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    I'd use a std::vector. Every element in the vector will be copied if you pass it by value.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. Problem Copying char array
    By NullStyle in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2004, 07:06 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21