如何在结构中将值从一个数组复制到另一个数组?

时间:2016-10-30 20:04:57

标签: c++ arrays struct copy

我正在尝试创建一个新数组,该数组通过添加一个新值来复制旧数组的所有值。原始数组无法更改。这些数组存储在结构中,如下所示。

import os,sys
import urllib,urllib2;
php_test = submission_path + php_test_folder + test_file  # these are set somewhere
os.system("echo " + php_test)  # prints http://localhost:1438/~cs1xx/1x/007.php
php_data = urllib2.urlopen(php_test)

我正在调用函数如下

typedef float Elem;

typedef struct Vector {
    unsigned int size = 0;
    Elem arrayOfElem[];
}VECTOR;


// extend_vec
// Takes in one vector, creates a new vector, copies all of the values and adds one new value
// Original vector unchanged, returns a reference to a new vector
// In:
// Vector array of x elements
// Out:
// Vector array of x + 1 elements
// Return: 
// A pointer to the new vector

Vector *extend_vec(Vector *numbers, float element) {
    Vector *bigger = new Vector;
    bigger->size = numbers->size + 1; // set the size of the new vector to the size of the old plus one more element
    unsigned int i = 0;
    while (i < bigger->size) {
        bigger->arrayOfElem[i] = numbers->arrayOfElem[i]; // step through the vectors and copy the values
        i++;
    }
    bigger->arrayOfElem[bigger->size - 1] = element; // set last place in the new vector to the new element
    return bigger;
}

“test”是我原来的硬编码数组,big应该是新数组,而我给出的输出是

// Testing for *extend_vec
       Vector big = *extend_vec(&test, 4.4);
        print_vec(&test);
        printf("\n");
        print_vec(&big);

所以函数是按照我想要的那样添加一个数组的大小,但它没有复制任何值,并且没有添加新值来发现它刚创建。

我的问题是我应该如何构建这个以使这些值复制到另一个数组中?我确信这对我的while循环来说是微不足道的。

0 个答案:

没有答案