2

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?

9
  • What code do you have right now? Commented Nov 17, 2012 at 2:12
  • You can't assign arrays. Array names are not lvalues. Commented Nov 17, 2012 at 2:12
  • buf[0] = &multiarray[index]; is what I have. @Chris, but arrays are treated like pointers in C, yes? Commented Nov 17, 2012 at 2:12
  • 1
    Arrays are not pointers, either. They decay in some cases, but this is not one of them. Commented Nov 17, 2012 at 2:13
  • Upvoted because a -1 didn't seem justified. I detest drive-by downvoting. Commented Nov 17, 2012 at 2:16

4 Answers 4

3
// 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)); 
Sign up to request clarification or add additional context in comments.

Comments

2

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);

Comments

0

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;

}

Comments

0

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.

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.