Skip to content

Instantly share code, notes, and snippets.

@dittos
Created October 23, 2011 17:13
Show Gist options
  • Save dittos/1307593 to your computer and use it in GitHub Desktop.
Save dittos/1307593 to your computer and use it in GitHub Desktop.
Compiler HW
/* array.c -- Operations on arrays
*/
#include <stdio.h>
int main(void) {
int a[2] = {1,2}; /* The aggregates like {1,2} are literals for arrays */
int b[2] = {2,3};
int i;
/* It is legal to use subscripts on arrays, both on the left and on
* the right hand side of assignments. */
for(i=0;i<2;i++)
a[i]=b[i];
/* It is not legal to assign arrays, like in a=b; */
/* The comparison of two distinct arrays with the same content
* results in FALSE. So below we print "They are not equal"
*/
if(a==b)
printf("They are equal\n");
else
printf("They are not equal\n");
/* The following comparison results in TRUE. */
if(a==a)
printf("Of course a is equal to a\n");
else
printf("No, a is not equal to a\n");
/* The behavior of comparison is explained when we note that the
* comparison is a comparison of addresses, not contents.
*/
/* We cannot print out an array as a single unit. We have to print out
* its elements one at a time.
*/
for(i=0;i<2;i++)
printf("a[%1d] = %3d\n", i, a[i]);
}
:=
[]
<id:a> (node id=2)
<int:0> (node id=5)
<int:1> (node id=0)
:=
[]
<id:a> (node id=2)
<int:1> (node id=0)
<int:2> (node id=1)
:=
[]
<id:b> (node id=9)
<int:0> (node id=5)
<int:2> (node id=1)
:=
[]
<id:b> (node id=9)
<int:1> (node id=0)
<int:3> (node id=8)
:=
<id:i> (node id=14)
<int:0> (node id=5)
<
<id:i> (node id=14)
<int:2> (node id=1)
++
<id:i> (node id=14)
:=
[]
<id:a> (node id=2)
<id:i> (node id=14)
[]
<id:b> (node id=9)
<id:i> (node id=14)
==
<id:a> (node id=2)
<id:b> (node id=9)
printf
printf format
<str:They are equal> (node id=22)
printf format
<str:(newline)> (node id=23)
printf
printf format
<str:They are not equal> (node id=27)
printf format
<str:(newline)> (node id=23)
==
<id:a> (node id=2)
<id:a> (node id=2)
printf
printf format
<str:Of course a is equal to a> (node id=32)
printf format
<str:(newline)> (node id=23)
printf
printf format
<str:No, a is not equal to a> (node id=36)
printf format
<str:(newline)> (node id=23)
:=
<id:i> (node id=14)
<int:0> (node id=5)
<
<id:i> (node id=14)
<int:2> (node id=1)
++
<id:i> (node id=14)
printf
printf format
<str:a[> (node id=43)
printf format
<format specifier> (node id=44)
printf format
<str:] = > (node id=45)
printf format
<format specifier> (node id=46)
printf format
<str:(newline)> (node id=23)
printf arg
<id:i> (node id=14)
printf arg
[]
<id:a> (node id=2)
<id:i> (node id=14)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment