Commit ed343199 authored by elijah vasquez's avatar elijah vasquez 🦍

AMBATUCOMMIT

parent 550c7799
...@@ -2,21 +2,26 @@ ...@@ -2,21 +2,26 @@
#include <stdio.h> #include <stdio.h>
#include <stdalign.h> #include <stdalign.h>
int main(void) { int main(void) {
int integer1 = 1; int integer1 = 1;
short short2 = 2;
int integer3 = 3; int integer3 = 3;
short short2 = 2;
char char1 = 4; //2
float float1 = 3.14; //2 float float1 = 3.14; //2
char char1 = 4; //2
printf("integer1 =%d Memory address %p\n size = %lu\n",integer1, &integer1, sizeof(integer1));
// printf("short2 =%d Memory address%p, size = %lu\n",short2, &short2, sizeof(short2));
printf("integer3 =%d Memory address %p\n size = %lu\n",integer3, &integer3, sizeof(integer3));
printf("short2 =%d Memory address %p\n size = %lu\n",short2, &short2, sizeof(short2));
printf("float1 = %d Memory address %p\n size = %lu\n", float1, &float1, sizeof(float1));
printf("char1 = %d Memory address %p\n size = %lu\n", char1, &char1, sizeof(char1));
printf("integer1 =%d Memory address%p\n",integer1, &integer1);
printf("size= %d\n", sizeof(integer1));
printf("short2 =%d Memory address%p\n",short2, &short2);
printf("integer3 =%d Memory address%p\n",integer3, &integer3);
printf("char1 = %d Memory address%p\n", char1, &char1);
printf("float1 = %d Memory address%p\n", float1, &float1);
// Exercise Tasks // Exercise Tasks
...@@ -33,11 +38,15 @@ printf("float1 = %d Memory address%p\n", float1, &float1); ...@@ -33,11 +38,15 @@ printf("float1 = %d Memory address%p\n", float1, &float1);
// integer3 =3 Memory address0x7ffeb7370264 // integer3 =3 Memory address0x7ffeb7370264
// 2 Add a char and a float variable, print out the value and address as above // 2 Add a char and a float variable, print out the value and address as above
// 3 Extend each printf to also print out the size of each variable type. // 3 Extend each printf to also print out the size of each variable type.
// 4 Rearrange the variable declaration order and see if it has an effect on the addresses // 4 Rearrange the variable declaration order and see if it has an effect on the addresses
/* integer1 =1 Memory address000000963e3ff8dc, size = 4
short2 =2 Memory address000000963e3ff8da, size = 2
integer3 =3 Memory address000000963e3ff8d4, size = 4
char1 = 4 Memory address000000963e3ff8d3, size = 1
float1 = 1610612736 Memory address000000963e3ff8cc, size = 4 */
// You may want to rearrange your printfs to match the order of your declarations // You may want to rearrange your printfs to match the order of your declarations
......
No preview for this file type
...@@ -5,13 +5,34 @@ int main () { ...@@ -5,13 +5,34 @@ int main () {
int var = 20; int var = 20;
int *ip1 = &var; int *ip1 = &var;
int *ip2; int *ip2 = NULL;
if(ip1 != NULL)
{
printf("Address in ip1 = %p\n", ip1);
printf("value in ip1 = %d\n", *ip1);
}
else
{
printf("ip2 done out ere\n");
}
if(ip2 != NULL)
{
printf("Address in ip2 = %p\n", ip2);
printf("value in ip2 = %d\n", *ip2);
}
else
{
printf("ip2 done out ere\n");
}
return 0; return 0;
} }
// Exercise Tasks // Exercise Tasks
// 1 Run the code. What is ip2 pointing at? Is this safe? // 1 Run the code. What is ip2 pointing at? Is this safe? Points at nothing
// 2 ip2 might be pointing at an integer. Print out the value ip2 is pointing at as an integer // 2 ip2 might be pointing at an integer. Print out the value ip2 is pointing at as an integer: Prints negative num
// 3 Initialise ip2 to NULL. Comment out the line printing out ip2 as an integer. Run the code. // 3 Initialise ip2 to NULL. Comment out the line printing out ip2 as an integer. Run the code.
// 4 Uncomment the line you just commented out and run the code. Why doe s the code crash? // 4 Uncomment the line you just commented out and run the code. Why doe s the code crash?
// 5 Add checks before trying to use ip1 or ip2, test to make sure they are not NULL before accessing them. // 5 Add checks before trying to use ip1 or ip2, test to make sure they are not NULL before accessing them.
\ No newline at end of file
...@@ -3,7 +3,13 @@ ...@@ -3,7 +3,13 @@
int main () { int main () {
int var = 20; int var = 9000; // 6 - CHANGED FROM 20
int* varPtr = &var; // 1
*varPtr = 8; // 7
printf("%p\n", &var); // 2
printf("%p\n", varPtr); // 3
printf("%d\n", var); // 4
printf("%d\n", *varPtr); // 5
// Exercise tasks // Exercise tasks
// 1 Create a pointer variable that stores the address of var // 1 Create a pointer variable that stores the address of var
......
File added
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment