gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > C question (*pointer = *pointer)

#94309 - LOst? - Mon Jul 24, 2006 10:39 am

I have never encountered this way of coding before:

Code:

typedef struct
{
 int a;
 int b;
 int c;
} BLAH;

int main (void)
{
 BLAH blah1 = {1, 2, 3};
 BLAH blah2 = {3, 2, 1};

 BLAH* p_blah1 = &blah1;
 BLAH* p_blah2 = &blah2;


  // What the hell does this do?:
  *p_blah2 = *p_blah1;

 return 0
}



What is happening here? You can't copy structures so I assume this trick does something I can never guess by my own.
_________________
Exceptions are fun

#94321 - wintermute - Mon Jul 24, 2006 10:55 am

LOst? wrote:

What is happening here? You can't copy structures so I assume this trick does something I can never guess by my own.


Yes, you can copy structs and that's exactly what the code is doing.
_________________
devkitPro - professional toolchains at amateur prices
devkitPro IRC support
Personal Blog

#94325 - LOst? - Mon Jul 24, 2006 11:51 am

wintermute wrote:
LOst? wrote:

What is happening here? You can't copy structures so I assume this trick does something I can never guess by my own.


Yes, you can copy structs and that's exactly what the code is doing.


Thank you! I didn't know it was possible. No more memcpy sizeof (struct) for me :P

I never thought it was possible because this fails:
Code:

blah2 = blah1;

_________________
Exceptions are fun

#94327 - MrD - Mon Jul 24, 2006 12:06 pm

It shouldn't fail. :/
_________________
Not active on this forum. For Lemmings DS help see its website.

#94612 - sgeos - Tue Jul 25, 2006 2:42 pm

How does it fail? I can't get onto a unix box to find out.

-Brendan

#94731 - MrD - Tue Jul 25, 2006 11:33 pm

It's possible it could fail in a different lanuage, such as the one which exhibits the bizarre property described in your sig, LOst :P
_________________
Not active on this forum. For Lemmings DS help see its website.

#94732 - tepples - Tue Jul 25, 2006 11:40 pm

LOst's sig talks about some old version of Fortran that had only what C calls 'static' local variables, not the 'auto' variables that allow for recursion. Such versions of Fortran probably didn't even have structs, nor did they have pointer arithmetic. Many implementations of pre-ANSI C didn't have operator = perform a struct copy either.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#94735 - MrD - Tue Jul 25, 2006 11:45 pm

Gosh... so... I was right then? :)
_________________
Not active on this forum. For Lemmings DS help see its website.

#94747 - PeterM - Wed Jul 26, 2006 12:58 am

I have a suspicion that C doesn't let you copy structures using = but C++ does.

That could be complete rubbish though since I use C++ almost exclusively nowadays.
_________________
http://aaiiee.wordpress.com/

#94749 - tepples - Wed Jul 26, 2006 1:02 am

ISO C added a lot of features from early C++, such as function prototypes in their modern form. Struct copying with operator = may have been one of them.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#94750 - PeterM - Wed Jul 26, 2006 1:09 am

You could be right there.

Visual C++ Express seems quite happy for me to copy a struct in C. I must have been mistaken before.
_________________
http://aaiiee.wordpress.com/

#94828 - chishm - Wed Jul 26, 2006 9:23 am

I often use struct copying with = in GCC C89 compilation mode. I haven't come across any problems yet.
_________________
http://chishm.drunkencoders.com
http://dldi.drunkencoders.com

#94831 - keldon - Wed Jul 26, 2006 9:42 am

chishm wrote:
I often use struct copying with = in GCC C89 compilation mode. I haven't come across any problems yet.


But doesn't C89 have a lot of features borrowed from C++? But then again the compiler he'd be using should be C89 too. Were you using a different compiler when you tried that code?

#94835 - Cearn - Wed Jul 26, 2006 11:07 am

Just checked in the library here. Might be slightly paraphrased. Emphasis mine.
K&R wrote:
The only legal operations on structs are copying or assigning to it as a unit, getting its address using &, and accessing its members

- The C programming language, 2nd Edition, section 6.2, first line (forgot to look up page number, sorry)

Might be reading it wrong, but that would seem to suggest it was possible from the start. Besides, I know that it is definitely possible to pass whole structures (that is, copies of structures) to functions, so it seems a little silly if stand-alone struct-copies would not be possible.

#95199 - sgeos - Fri Jul 28, 2006 5:33 am

Silly? Consider this:
Code:
#define STRING_MAX 82
char myString[STRING_MAX] = "hello world";
myString = "good bye creul world";

IIRC it doesn't work.

-Brendan

#95208 - tepples - Fri Jul 28, 2006 5:54 am

sgeos wrote:
Silly? Consider this:
Code:
#define STRING_MAX 82
char myString[STRING_MAX] = "hello world";
myString = "good bye creul world";

IIRC it doesn't work.

-Brendan

That demonstrates that you can copy structs but you can't copy arrays. Strings in C are arrays.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#95218 - sgeos - Fri Jul 28, 2006 6:29 am

When you put it that way it makes sense. Arrays can be of any size, and structs generally have a fixed size. In C99 can't a variable length array be the last member of a struct? Can such structs be copied?

-Brendan

#95223 - sajiimori - Fri Jul 28, 2006 7:00 am

Um... how would it know how much to copy?

Anyway, the char array initialization example you posted is a special case shortcut for this:
Code:
char myString[STRING_MAX] =
    {'h','e','l','l','o',' ','w','o','r','l','d','\0'};

It's the only shortcut of its kind in C. In all other situations, string literals evaluate to const char* (or sometimes char* for backward compatability).

#95309 - tepples - Fri Jul 28, 2006 6:00 pm

sgeos wrote:
In C99 can't a variable length array be the last member of a struct?

A variable-length array as the last field of a struct is just a formalization of the semantics that C compilers have long used for a zero-length array as the last field.

Quote:
Can such structs be copied?

With memcpy(): Yes.
With operator = : I don't know. Either it'll fail at compile time, or it just won't copy the members of the variable-length array.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.