<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://wiki.kram.nz/index.php?action=history&amp;feed=atom&amp;title=SE250%3Alab-2%3Ashua066</id>
	<title>SE250:lab-2:shua066 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.kram.nz/index.php?action=history&amp;feed=atom&amp;title=SE250%3Alab-2%3Ashua066"/>
	<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE250:lab-2:shua066&amp;action=history"/>
	<updated>2026-05-01T20:55:57Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.3</generator>
	<entry>
		<id>https://wiki.kram.nz/index.php?title=SE250:lab-2:shua066&amp;diff=5228&amp;oldid=prev</id>
		<title>Mark: 33 revision(s)</title>
		<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE250:lab-2:shua066&amp;diff=5228&amp;oldid=prev"/>
		<updated>2008-11-03T05:19:10Z</updated>

		<summary type="html">&lt;p&gt;33 revision(s)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;Lab2&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
==Q 1==&lt;br /&gt;
&lt;br /&gt;
The question is use the &amp;#039;sizeof()&amp;#039; operator to determine the size of a point.&lt;br /&gt;
&lt;br /&gt;
 int main(){&lt;br /&gt;
    int *ip;&lt;br /&gt;
    int *dpt;&lt;br /&gt;
    printf(&amp;quot;%d %d\n&amp;quot;,sizeof(ip),sizeof(dpt));&lt;br /&gt;
    return 0;&lt;br /&gt;
 }&lt;br /&gt;
and i got &lt;br /&gt;
 4 4&lt;br /&gt;
the diff names of the pointer , the same size.&lt;br /&gt;
&lt;br /&gt;
I try to use the other types I got the answers all the same.&lt;br /&gt;
 4&lt;br /&gt;
&lt;br /&gt;
so the sizeof a pointer is &amp;quot;4&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
and I got that I can declare the variavles first as well&lt;br /&gt;
 double x=10;&lt;br /&gt;
 double *dpt=&amp;amp;x;&lt;br /&gt;
&lt;br /&gt;
==Q2==&lt;br /&gt;
&lt;br /&gt;
to find the address of two pointer and find out the diff between these address.&lt;br /&gt;
  int x;&lt;br /&gt;
  int y;&lt;br /&gt;
  printf(&amp;quot;&amp;amp;x=%p\n &amp;amp;y=%p\n diff=%ld\n,&amp;amp;x,&amp;amp;y,(long)(&amp;amp;x-&amp;amp;y));&lt;br /&gt;
I got the outcome is &lt;br /&gt;
 &amp;amp;x=0x22ccc4&lt;br /&gt;
 &amp;amp;y=0x22ccc0&lt;br /&gt;
 diff=1&lt;br /&gt;
&lt;br /&gt;
I change &lt;br /&gt;
 printf(&amp;quot;&amp;amp;x=%p\n &amp;amp;y=%p\n diff=%ld\n,&amp;amp;x,&amp;amp;y,(long)&amp;amp;x-(long)&amp;amp;y);&lt;br /&gt;
result&lt;br /&gt;
 &amp;amp;x=0x22ccc4&lt;br /&gt;
 &amp;amp;y=0x22ccc0&lt;br /&gt;
 diff=4&lt;br /&gt;
&lt;br /&gt;
the same address of x and y ,but not the same as diff,because the first one is the diff of two address ,so it is one byte. and the second one is the diff of two numbers,so it is 4.&lt;br /&gt;
&lt;br /&gt;
==Q3==&lt;br /&gt;
 int x;&lt;br /&gt;
 char arr [4];&lt;br /&gt;
 int y;&lt;br /&gt;
&lt;br /&gt;
check the size of the array ,it is &lt;br /&gt;
 printf(&amp;quot;%d\n&amp;quot;, sizeof(arr[4]));&lt;br /&gt;
&lt;br /&gt;
result&lt;br /&gt;
  1&lt;br /&gt;
&lt;br /&gt;
 printf(&amp;quot;&amp;amp;arr=%p\n&amp;quot;, &amp;amp;arr);&lt;br /&gt;
result&lt;br /&gt;
 &amp;amp;arr = 0x22ccc0&lt;br /&gt;
&lt;br /&gt;
 printf(&amp;quot;arr+4=%p, &amp;amp;arr+4=%p, &amp;amp;arr[4]=%p\n&amp;quot;,arr+4,&amp;amp;arr+4,&amp;amp;arr[4]);&lt;br /&gt;
&lt;br /&gt;
result&lt;br /&gt;
  arr+4=0x22ccc4, &amp;amp;arr+4=0x22ccd0, &amp;amp;arr[4]=0x22ccc4&lt;br /&gt;
&lt;br /&gt;
the &amp;quot;arr+4&amp;quot; and &amp;quot;&amp;amp;arr[4]&amp;quot; are the same.&lt;br /&gt;
&lt;br /&gt;
Next ,I change the size of the array from 0 to 10.&lt;br /&gt;
                   the address of x      add of y   diff&lt;br /&gt;
  the value is 0   0x22ccbc              0x22cc9c    8&lt;br /&gt;
  the value is 1   0x22ccc4              0x22ccbc    2 &lt;br /&gt;
  the value is 8   0x22ccc4              0x22ccb4    4  &lt;br /&gt;
the value is 1&amp;amp;2,3,4 are the same&lt;br /&gt;
the value is 0&amp;amp;3,5,6,7,9,10 are the same&lt;br /&gt;
looks like the number play around of that address, it is the compiler give the arrary the space.&lt;br /&gt;
&lt;br /&gt;
Set the value of x&amp;amp;y =0,and arr[4]=10.&lt;br /&gt;
result&lt;br /&gt;
  10 0&lt;br /&gt;
==Q4==&lt;br /&gt;
===code===&lt;br /&gt;
&lt;br /&gt;
 int main(){&lt;br /&gt;
     int g_x;&lt;br /&gt;
     int g_y;&lt;br /&gt;
     printf(&amp;quot;&amp;amp;g_x=%p,&amp;amp;g_y=%p,diff =%ld\n&amp;quot;, &amp;amp;g_x,&amp;amp;g_y,(long)(&amp;amp;g_x-&amp;amp;g_y));&lt;br /&gt;
     printf(&amp;quot;&amp;amp;g_x=%p,&amp;amp;g_y=%p,diff =%ld\n&amp;quot;, &amp;amp;g_x,&amp;amp;g_y,(long)&amp;amp;g_x-(long)&amp;amp;g_y);&lt;br /&gt;
     return 0;&lt;br /&gt;
 }&lt;br /&gt;
===result===&lt;br /&gt;
 &amp;amp;g_x=0x22ccc4,&amp;amp;g_y=0x22ccc0,diff =1&lt;br /&gt;
 &amp;amp;g_x=0x22ccc4,&amp;amp;g_y=0x22ccc0,diff =4&lt;br /&gt;
same as &amp;#039;&amp;#039;Q2&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
===code===&lt;br /&gt;
 int main(){&lt;br /&gt;
     int g_x;&lt;br /&gt;
     char arr[4];&lt;br /&gt;
     int g_y;&lt;br /&gt;
     printf(&amp;quot;&amp;amp;g_x=%p,&amp;amp;g_y=%p,diff =%ld\n&amp;quot;, &amp;amp;g_x,&amp;amp;g_y,(long)(&amp;amp;g_x-&amp;amp;g_y));&lt;br /&gt;
     printf(&amp;quot;&amp;amp;g_x=%p,&amp;amp;g_y=%p,diff =%ld\n&amp;quot;, &amp;amp;g_x,&amp;amp;g_y,(long)&amp;amp;g_x-(long)&amp;amp;g_y);&lt;br /&gt;
     printf(&amp;quot;size of arr[4] is %d\n&amp;quot;,sizeof(arr[4]));&lt;br /&gt;
     printf(&amp;quot;arr+4=%p, &amp;amp;arr+4=%p, &amp;amp;arr[4]=%p\n&amp;quot;,arr+4,&amp;amp;arr+4,&amp;amp;arr[4]);&lt;br /&gt;
     return 0;&lt;br /&gt;
 }&lt;br /&gt;
===result===&lt;br /&gt;
the same as &amp;#039;&amp;#039;Q2&amp;#039;&amp;#039;&lt;br /&gt;
===result===&lt;br /&gt;
                 the address of g_x      add of g_y   diff&lt;br /&gt;
  the value is 0   0x22ccbc              0x22cc9c    8&lt;br /&gt;
  the value is 1   0x22ccc4              0x22ccbc    2 &lt;br /&gt;
  the value is 8   0x22ccc4              0x22ccb4    4 &lt;br /&gt;
the result of 0&amp;amp;3,5,6,7,9,10 are the same, the result of 1&amp;amp;2,4 are the same.&lt;br /&gt;
&amp;#039;&amp;#039;it is not the same as in Q2&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
set the value x ,y to 0,and arr[4]to 10&lt;br /&gt;
===result===  &lt;br /&gt;
 10 0&lt;br /&gt;
&lt;br /&gt;
==Q5==&lt;br /&gt;
===code===&lt;br /&gt;
 int main(){&lt;br /&gt;
     int *p1,*p2;&lt;br /&gt;
     {int q; p1 = &amp;amp;q;}&lt;br /&gt;
     {int r; p2 = &amp;amp;r;}&lt;br /&gt;
     printf(&amp;quot;%d %d\n&amp;quot;,p1,p2);&lt;br /&gt;
     return 0;&lt;br /&gt;
 }&lt;br /&gt;
===result===&lt;br /&gt;
 2280636 2280632&lt;br /&gt;
==Q6==&lt;br /&gt;
===code===&lt;br /&gt;
 char *local_str(){&lt;br /&gt;
      char s[8]=&amp;quot;0123456&amp;quot;;&lt;br /&gt;
      return s;&lt;br /&gt;
 }&lt;br /&gt;
 char *local_str2(){&lt;br /&gt;
     char s[8]=&amp;quot;abcdefg&amp;quot;;&lt;br /&gt;
     return s;&lt;br /&gt;
 }&lt;br /&gt;
 char *static_str(){&lt;br /&gt;
     static char s[8]=&amp;quot;tuvwxyz&amp;quot;;&lt;br /&gt;
     return s;&lt;br /&gt;
 }&lt;br /&gt;
 char *malloc_str(){&lt;br /&gt;
     char *s=malloc(8);&lt;br /&gt;
     strcpy(s, &amp;quot;hijklmn&amp;quot;);&lt;br /&gt;
     return s;&lt;br /&gt;
 }&lt;br /&gt;
 int main(){&lt;br /&gt;
     char *sp;&lt;br /&gt;
     sp=local_str();&lt;br /&gt;
     printf(&amp;quot;sp=%p(%s)\n&amp;quot;,sp,sp);&lt;br /&gt;
     sp = local_str();&lt;br /&gt;
      local_str2();&lt;br /&gt;
     printf(&amp;quot;sp=%p(%s)\n&amp;quot;,sp,sp);&lt;br /&gt;
     sp=static_str();&lt;br /&gt;
     local_str2();&lt;br /&gt;
     printf(&amp;quot;sp=%p(%s)\n&amp;quot;,sp,sp);&lt;br /&gt;
     sp=malloc_str();&lt;br /&gt;
     loca_str2();&lt;br /&gt;
     printf(&amp;quot;sp=%p(%s)\n&amp;quot;,sp,sp);&lt;br /&gt;
     return 0;&lt;br /&gt;
 }&lt;br /&gt;
===result===&lt;br /&gt;
 gcc lab.c -o lab&amp;amp;&amp;amp; ./lab.exe&lt;br /&gt;
 lab.c: In function `local_str&amp;#039;:&lt;br /&gt;
 lab.c:3: warning: function returns address of local variable&lt;br /&gt;
 lab.c: In function `local_str2&amp;#039;:&lt;br /&gt;
 lab.c:7: warning: function returns address of local variable&lt;br /&gt;
 /cygdrive/c/Users/shua066/AppData/Local/Temp/ccabFLmD.o:lab.c:(.text+0x112): undefined reference to `_loca_str2&amp;#039;&lt;br /&gt;
 collect2: ld returned 1 exit status&lt;br /&gt;
&lt;br /&gt;
==Q7==&lt;br /&gt;
&lt;br /&gt;
===code===&lt;br /&gt;
 struct {&lt;br /&gt;
     char my_char;&lt;br /&gt;
     short my_short;&lt;br /&gt;
     int my_int;&lt;br /&gt;
     long my_long;&lt;br /&gt;
     float my_float;&lt;br /&gt;
     double my_double;&lt;br /&gt;
 }my_struct;&lt;br /&gt;
===result===&lt;br /&gt;
 gcc task7.c -o task7&amp;amp;&amp;amp; ./task7.exe&lt;br /&gt;
 &amp;amp;my_struct = 0x0&lt;br /&gt;
 offsets:&lt;br /&gt;
 my_char: 0&lt;br /&gt;
 my_short: -2&lt;br /&gt;
 my_int: -4&lt;br /&gt;
 my_long: -8&lt;br /&gt;
 my_float: -12&lt;br /&gt;
 my_double: -16&lt;br /&gt;
==Q8==&lt;br /&gt;
change the &amp;#039;&amp;#039;struct&amp;#039;&amp;#039; to &amp;#039;&amp;#039;union&amp;#039;&amp;#039;&lt;br /&gt;
===result===&lt;br /&gt;
 gcc task7.c -o task7&amp;amp;&amp;amp; ./task7.exe&lt;br /&gt;
 &amp;amp;my_struct = 0x0&lt;br /&gt;
 offsets:&lt;br /&gt;
 my_char: 0&lt;br /&gt;
 my_short: 0&lt;br /&gt;
 my_int: 0&lt;br /&gt;
 my_long: 0&lt;br /&gt;
 my_float: 0&lt;br /&gt;
 my_double: 0&lt;/div&gt;</summary>
		<author><name>Mark</name></author>
	</entry>
</feed>