1

please can you help me? How can I copy part of one int array to another int array?

Example:

typedef struct part {
  int * array;
} PART;

int array[] = {1,2,3,4,5,6,7,8,9};
PART out[] = new PART[3];

for (int i = 0; i < 3; i++)
{
  memcpy((char *)array[i * 3], (char *)out[i].array, 3 * sizeof(int));
}

But this don't working... :(

2
  • 2
    besides the answer below, your use of memcpy is probably wrong. the first argument of memcpy is the destination, not the source. Commented Dec 10, 2010 at 19:03
  • 2
    I see your question has C++ tag, if so, don't uglify your code with "typedef struct" and don't use memcpy, C++ has std::copy for that. It's also a good idea to use ALL_CAPS names for macros only. Commented Dec 10, 2010 at 19:08

3 Answers 3

6

Ok you have 3 problems.

  1. You are casting an int to a char* (char *)array[i * 3]

    What you really mean is (char *)&array[i * 3]. ie take the address of the i*3th element.

  2. you are trying to copy data from an uninitialised array.

    you should allocate memory to out[i].array.

  3. You appear to have your memcpy the wrong way round.

The following code will work better:

typedef struct part {
  int * array;
} PART;

int array[] = {1,2,3,4,5,6,7,8,9};
PART out[] = new PART[3];

for (int i = 0; i < 3; i++)
{
  out[i].array = new int[3];
  memcpy( (char *)out[i].array, (char *)&array[i * 3], 3 * sizeof(int));
}

Make sure you remember to delete[] the memory allocated to out[i].array ...

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, in fact, in real problem, I initialise array in struct and use memcpy with right order of arguments, problem was in "&". Thank you again, stupid mistake... :)
@Sebastian: If the answer solves your problems feel free to accept my answer by clicking the "tick" in the top left of my post :)
3

You need to allocate memory for * array before doing memcpy.

Something like this:

typedef struct part {
    int * array;
} PART;

int array[] = {1,2,3,4,5,6,7,8,9};
PART out[] = new PART[3];

for (int i = 0; i < 3; i++)
{
     out[i].array = malloc(9*sizeof(int));
     // will copy 9 array values into out[i].array
     memcpy(out[i].array, array, 9 * sizeof(int));
}

3 Comments

More specifically, you have an array of PARTs. The int *array inside that struct is uninitialized. It must be initialized.
erm. I think he just needs 3 int s in each PART.
Isn't he OP trying to copy the whole array on every PART?
1

The struct part of this is kind of irrelevant. What you are trying to do can be accomplished this way:

int src[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int part[3][3];

for (int i = 0; i < 3; ++i)
{
    std::copy(src + (3 * i), src + (3 * (i + 1)), part[i]);
}

An even better solution can be done by using std::vector instead of C-style arrays:

std::vector<int> a(src, src + 9);
std::vector<std::vector<int> > b;

for (int i = 0; i < 3; ++i)
{
    std::vector<int> c(a.begin() + (3 * i), a.begin() + (3 * (i + 1)));
    b.push_back(c);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.