C Programming: Structure Assignment
這篇文章主要是記錄自己的孤陋寡聞 ... 今天同事在聊一段程式碼(內容就不多講了,反正簡化後如下)。乍看之下大吃一驚,為什麼Structure 間可以直接使用 Assignment?在我過去的認知裡面,如果要對 Structure 作拷貝的動作,應該要使用 memcpy 才對吧?所以才寫了小小的程式碼測試一下。 # include < stdio.h > # include < stdlib.h > struct tObj { int a ; double b ; int c [ 3 ] ; } ; int main ( ) { struct tObj obj1 ; struct tObj obj2 ; obj1 . a = 1 ; obj1 . b = 2.0 ; obj1 . c [ 0 ] = 3 ; obj1 . c [ 1 ] = 4 ; obj1 . c [ 2 ] = 5 ; obj2 . a = 0 ; obj2 . b = 0.0 ; obj2 . c [ 0 ] = 0 ; obj2 . c [ 1 ] = 0 ; obj2 . c [ 2 ] = 0 ; obj2 = obj1 ; printf ( " obj2.a = %d \n " , obj2 . a ) ; printf ( " obj2.b = %f \n " , obj2 . b ) ; printf ( " obj2.c[0] = %d \n " , obj2 . c [ 0 ] ) ; printf ( " obj2.c[1] = %d \n " , obj2 . c [ 1 ] ) ; printf ( " obj2.c[2] = %d \n " , obj2 . c [ 2 ] ) ; ret...