0

Hi All,

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

Regards,
Pramod

9
Contributors
19
Replies
22
Views
7 Years
Discussion Span
Last Post by Ancient Dragon
Featured Replies
  • 1
    Salem 5,138   7 Years Ago

    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? Read More

  • 1
    WaltP 2,905   7 Years Ago

    [QUOTE=pramoda.ma;1184076]How to copy one 2D-array to another without using loops and library function of C. Not pointers.[/QUOTE] 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 … Read More

  • 1

    As to the original 'problem', would [ICODE]goto[/ICODE] be accepted? Read More

0

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.

Edited by Ancient Dragon: n/a

0

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?

0

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

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

1

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?

1

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

1

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.

Votes + Comments
Programming language "historian" rep
0

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.

0

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.

0

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 ?

1

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]);
}
Votes + Comments
Nice, but it still probably resolves to a hidden call to memcpy
0

>>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.

Edited by Ancient Dragon: n/a

1

>>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.

Votes + Comments
1

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...

Votes + Comments
i thought the same thing.
0

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]

Edited by Ancient Dragon: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.