SE250:lab-2:shua066

From Marks Wiki
Jump to navigation Jump to search

Lab2

Q 1

The question is use the 'sizeof()' operator to determine the size of a point.

int main(){
   int *ip;
   int *dpt;
   printf("%d %d\n",sizeof(ip),sizeof(dpt));
   return 0;
}

and i got

4 4

the diff names of the pointer , the same size.

I try to use the other types I got the answers all the same.

4

so the sizeof a pointer is "4".

and I got that I can declare the variavles first as well

double x=10;
double *dpt=&x;

Q2

to find the address of two pointer and find out the diff between these address.

 int x;
 int y;
 printf("&x=%p\n &y=%p\n diff=%ld\n,&x,&y,(long)(&x-&y));

I got the outcome is

&x=0x22ccc4
&y=0x22ccc0
diff=1

I change

printf("&x=%p\n &y=%p\n diff=%ld\n,&x,&y,(long)&x-(long)&y);

result

&x=0x22ccc4
&y=0x22ccc0
diff=4

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.

Q3

int x;
char arr [4];
int y;

check the size of the array ,it is

printf("%d\n", sizeof(arr[4]));

result

 1
printf("&arr=%p\n", &arr);

result

&arr = 0x22ccc0
printf("arr+4=%p, &arr+4=%p, &arr[4]=%p\n",arr+4,&arr+4,&arr[4]);

result

 arr+4=0x22ccc4, &arr+4=0x22ccd0, &arr[4]=0x22ccc4

the "arr+4" and "&arr[4]" are the same.

Next ,I change the size of the array from 0 to 10.

                  the address of x      add of y   diff
 the value is 0   0x22ccbc              0x22cc9c    8
 the value is 1   0x22ccc4              0x22ccbc    2 
 the value is 8   0x22ccc4              0x22ccb4    4  

the value is 1&2,3,4 are the same the value is 0&3,5,6,7,9,10 are the same looks like the number play around of that address, it is the compiler give the arrary the space.

Set the value of x&y =0,and arr[4]=10. result

 10 0

Q4

code

int main(){
    int g_x;
    int g_y;
    printf("&g_x=%p,&g_y=%p,diff =%ld\n", &g_x,&g_y,(long)(&g_x-&g_y));
    printf("&g_x=%p,&g_y=%p,diff =%ld\n", &g_x,&g_y,(long)&g_x-(long)&g_y);
    return 0;
}

result

&g_x=0x22ccc4,&g_y=0x22ccc0,diff =1
&g_x=0x22ccc4,&g_y=0x22ccc0,diff =4

same as Q2

code

int main(){
    int g_x;
    char arr[4];
    int g_y;
    printf("&g_x=%p,&g_y=%p,diff =%ld\n", &g_x,&g_y,(long)(&g_x-&g_y));
    printf("&g_x=%p,&g_y=%p,diff =%ld\n", &g_x,&g_y,(long)&g_x-(long)&g_y);
    printf("size of arr[4] is %d\n",sizeof(arr[4]));
    printf("arr+4=%p, &arr+4=%p, &arr[4]=%p\n",arr+4,&arr+4,&arr[4]);
    return 0;
}

result

the same as Q2

result

                the address of g_x      add of g_y   diff
 the value is 0   0x22ccbc              0x22cc9c    8
 the value is 1   0x22ccc4              0x22ccbc    2 
 the value is 8   0x22ccc4              0x22ccb4    4 

the result of 0&3,5,6,7,9,10 are the same, the result of 1&2,4 are the same. it is not the same as in Q2

set the value x ,y to 0,and arr[4]to 10

result

10 0

Q5

code

int main(){
    int *p1,*p2;
    {int q; p1 = &q;}
    {int r; p2 = &r;}
    printf("%d %d\n",p1,p2);
    return 0;
}

result

2280636 2280632

Q6

code

char *local_str(){
     char s[8]="0123456";
     return s;
}
char *local_str2(){
    char s[8]="abcdefg";
    return s;
}
char *static_str(){
    static char s[8]="tuvwxyz";
    return s;
}
char *malloc_str(){
    char *s=malloc(8);
    strcpy(s, "hijklmn");
    return s;
}
int main(){
    char *sp;
    sp=local_str();
    printf("sp=%p(%s)\n",sp,sp);
    sp = local_str();
     local_str2();
    printf("sp=%p(%s)\n",sp,sp);
    sp=static_str();
    local_str2();
    printf("sp=%p(%s)\n",sp,sp);
    sp=malloc_str();
    loca_str2();
    printf("sp=%p(%s)\n",sp,sp);
    return 0;
}

result

gcc lab.c -o lab&& ./lab.exe
lab.c: In function `local_str':
lab.c:3: warning: function returns address of local variable
lab.c: In function `local_str2':
lab.c:7: warning: function returns address of local variable
/cygdrive/c/Users/shua066/AppData/Local/Temp/ccabFLmD.o:lab.c:(.text+0x112): undefined reference to `_loca_str2'
collect2: ld returned 1 exit status

Q7

code

struct {
    char my_char;
    short my_short;
    int my_int;
    long my_long;
    float my_float;
    double my_double;
}my_struct;

result

gcc task7.c -o task7&& ./task7.exe
&my_struct = 0x0
offsets:
my_char: 0
my_short: -2
my_int: -4
my_long: -8
my_float: -12
my_double: -16

Q8

change the struct to union

result

gcc task7.c -o task7&& ./task7.exe
&my_struct = 0x0
offsets:
my_char: 0
my_short: 0
my_int: 0
my_long: 0
my_float: 0
my_double: 0