user1782677
user1782677

Reputation: 2007

Copy (row) from 2D array to 1D array

So I have a 2d array multiarray[a][b] and another array buf[b].

I'm having trouble assigning 'buf' to be equal to one of the rows of the multiarray. What is the exact syntax to do this?

Upvotes: 2

Views: 5637

Answers (4)

Dalphat
Dalphat

Reputation: 41

If you're using vectors:

vector<vector<int> > int2D;
vector<int> int1D;

You could simply use the vector's built in assignment operator:

int1D = int2D[A];//Will copy the array at index 'A'

If you're using c-style arrays, a primitive approach would be to copy each element from the selected row to the single dimensional array:

Example:

//Assuming int2D is a 2-Dimensional array of size greater than 2.
//Assuming int1D is a 1-Dimensional array of size equal to or greater than a row in int2D.

int a = 2;//Assuming row 2 was the selected row to be copied.

for(unsigned int b = 0; b < sizeof(int2D[a])/sizeof(int); ++b){
    int1D[b] = int2D[a][b];//Will copy a single integer value.
}

Syntax is a rule, algorithm is what you probably meant/desired.

Upvotes: 0

wilbeibi
wilbeibi

Reputation: 3454

I read the code has similar function in snort (old version), it is borrowed from tcpdump, maybe helpful to you.

/****************************************************************************
 *
 * Function: copy_argv(u_char **)
 *
 * Purpose: Copies a 2D array (like argv) into a flat string.  Stolen from
 *          TCPDump.
 *
 * Arguments: argv => 2D array to flatten
 *
 * Returns: Pointer to the flat string
 *
 ****************************************************************************/
char *copy_argv(char **argv)
{
  char **p;
  u_int len = 0;
  char *buf;
  char *src, *dst;
  void ftlerr(char *, ...);

  p = argv;
  if (*p == 0) return 0;

  while (*p)
    len += strlen(*p++) + 1;

  buf = (char *) malloc (len);
  if(buf == NULL)
  {
     fprintf(stderr, "malloc() failed: %s\n", strerror(errno));
     exit(0);
  }
  p = argv;
  dst = buf;
  while ((src = *p++) != NULL)
  {
      while ((*dst++ = *src++) != '\0');
      dst[-1] = ' ';
  }
  dst[-1] = '\0';

  return buf;

}

Upvotes: 0

AnT stands with Russia
AnT stands with Russia

Reputation: 320381

Arrays are not assignable. There is no core language syntax for this. Array copying in C++ is implemented at library level or at user code level.

If this is supposed to be C++ and if you really need to create a separate copy buf of some row i of the 2D array mutiarray, then you can use std::copy

#include <algorithm>
...

SomeType multiarray[a][b], buf[b];
...
std::copy(multiarray[i], multiarray[i] + b, buf);

or in C++11

std::copy_n(multiarray[i], b, buf);

Upvotes: 2

Steve Jessop
Steve Jessop

Reputation: 279225

// a 2-D array of char
char multiarray[2][5] = { 0 };
// a 1-D array of char, with matching dimension
char buf[5];
// set the contents of buf equal to the contents of the first row of multiarray.
memcpy(buf, multiarray[0], sizeof(buf)); 

Upvotes: 3

Related Questions