Hi All,

How to copy one 2D-array to another without using loops and library function of C. Not pointers.

Regards,
Pramod

write a recursive function to do it. But don't make the two arrays very large because recursion takes up a lot of stack space.

write a recursive function to do it. But don't make the two arrays very large because recursion takes up a lot of stack space.

other than recursive any other way?

Not that I know of.

no loops, no library functions, no pointers, no recursion ...

well, i don't know what else is left. you've got to use something

Great, another "teacher" who thinks "stupid programming tricks" is a way to teach students.

> other than recursive any other way?
Any other restrictions, before we decide to waste any more time thinking about this pointless question?

Didn't you forget the rule about "Must not use any cpu cycles"? ;)

Hm... then print source array on paper and write destination array with pencil yourself :twisted:

How to copy one 2D-array to another without using loops and library function of C. Not pointers.

No C function? Write it in ForTran, or Algol, or Pascal, or Cobol.
Bliss would be interesting, or Lisp.
It would be hard in RPG-II.
Maybe assembler -- no, there you'd need a loop.

commented: Programming language "historian" rep +4

Trivial if the arrays are the same size.

int array1[10][100];
int array2[10][100];

array2 = array1;

Trivial if the arrays are the same size.

int array1[10][100];
int array2[10][100];

array2 = array1;

array2, array1 will be pointers to first elements in memory blocks.
So you are manipulating with pointers. This does not copy array elements, certainly.

Array1 and array2 are not pointers, they are arrays,
for example, sizeof(array1) = 4000 bytes.
The equal sign produces efficient code to copy the entire array.
array1 = array2; /* copies the entire 2d array */
If you don't believe it, try it.

I agree it would not work if it was a malloc'ed array,
but in this case the size is compiled in as [10][100]
and both arrays are the same size.

Array1 and array2 are not pointers, they are arrays,
for example, sizeof(array1) = 4000 bytes.

There is 2 separate situations here (you missed second);
- sizeof() is call in the same code block where array declaration is - then yes it shows array size in bytes.
- sizeof() is called from where it don't sees array declaration - then it shows pointer to array size (32 or 64 bits regarding CPU architecture).
For example -> try to pass array into function as parameter, and in that function call sizeof(array) - you will see what i am talking about.

Besides notations "array[5]" and "*(array+5)" are EQUAL. So it can be safe to say that when we accessing array element - we are actually doing pointer arithmetics.

The equal sign produces efficient code to copy the entire array.
array1 = array2; /* copies the entire 2d array */
If you don't believe it, try it.

Tried, no luck. My compiler barked something about "incompatible types in assignment". What C compiler you are using ?

Oops, it works when the array is part of a structure or union.

struct { 
  int elem[10][100]; 
} array1, array2;

main()
{
  array1.elem[0][0] = 1;
  array1.elem[9][99] = 9;
  array2 = array1;
  printf("%d %d\n", array2.elem[0][0], array2.elem[9][99]);
}
commented: Nice, but it still probably resolves to a hidden call to memcpy +20

>>Besides notations "array[5]" and "*(array+5)" are EQUAL. So it can be safe to say that when we accessing array element - we are actually doing pointer arithmetics.

Not really. array can not be incremented like a pointer, so it's not a pointer. You can't do array++; in the same function as where the array was originally declared.

>>Besides notations "array[5]" and "*(array+5)" are EQUAL. So it can be safe to say that when we accessing array element - we are actually doing pointer arithmetics.

Not really. array can not be incremented like a pointer, so it's not a pointer. You can't do array++; in the same function as where the array was originally declared.

Where i said that array IS pointer ? That's one point. Second point is that it is true what you said. For that reason I call array - "static address" and pointer - "dynamic address". But they both represent ADDRESSES to memory. So they has differences, but also similarities too.

commented: Agree +28

Oops, it works when the array is part of a structure or union.

struct { 
  int elem[10][100]; 
} array1, array2;

main()
{
  array1.elem[0][0] = 1;
  array1.elem[9][99] = 9;
  array2 = array1;
  printf("%d %d\n", array2.elem[0][0], array2.elem[9][99]);
}

Yes, such copy is working. But... don't you think that your solution misses the question, because question is about copying arrays, not structures. :) . Whatever...

commented: i thought the same thing. +7

As to the original 'problem', would goto be accepted?

commented: you scared me for a second. +7

As to the original 'problem', would goto be accepted?

hahahah

LOL

As to the original 'problem', would goto be accepted?

No -- that would just make another kind of loop.

[edit]Oops! didn't notice the sarcasm. [/edit]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.