Commit 550c7799 authored by elijah vasquez's avatar elijah vasquez 🦍

commit2

parent 61630b94
{
"files.associations": {
"stdio.h": "c"
}
}
\ No newline at end of file
// Ex 4
#include <stdio.h>
int main(void) {
int a[] = {1,1,1,1};
int b[] = {2,2,2,2};
for (int i=0;i<4;i++)
{
printf("array a[%d] %d at address %p\n",i,a[i], &a[i]);
}
printf("\n");
for (int i=0;i<4;i++)
{
printf("array b[%d] %d at address %p\n",i,b[i], &b[i]);
}
printf("\n");
for (int i=0; i<8; i++)
{
b[i]=3;
printf("setting array b[%d] to %d at address %p\n",i,b[i], &b[i]);
}
printf("\n");
for (int i=0;i<4;i++)
{
printf("array a[%d] %d at address %p\n",i,a[i], &a[i]);
}
printf("\n");
for (int i=0;i<4;i++)
{
printf("array b[%d] %d at address %p\n",i,b[i], &b[i]);
}
printf("\n");
return 0;
}
// Task Exercises
// 1 Resize the a[] array to store six integers and set them all to 1. Change the loops printing out a[] to match. Run the code - what is the effect?
// 2 Resize the b[] array to store six integers and set them all to 2. Change the loops printing out b[] to match. Run the code - what is the effect? Why is a[] now unaffected by overrunning b[]?
// 3 Change the loop that sets b[] to 3 from i<8 to i<10. Run the code. Does a[] change at all now?
// 4 Create a new array c[] immeadiately after creating b[]. Make it four long and set each value in it to 4. Print out all of c[] after printing out b[] each time. Run the code. Does c[] change?
// 5 Change the loop that sets b[] to 3 from int i=0 to int i=-2. Run the code. Does c[] change at all now? Why?
// Ex 1
#include <stdio.h>
#include <stdalign.h>
int main(void) {
int integer1=1;
short short2=2;
int integer3=3;
char char1=4;
float float1=3.14;
int integer1 = 1;
short short2 = 2;
int integer3 = 3;
char char1 = 4; //2
float float1 = 3.14; //2
printf("integer1 =%d Memory address%p\n",integer1,&integer1);
printf("short2 =%d Memory address%p\n",short2,&short2);
printf("integer3 =%d Memory address%p\n",integer3,&integer3);
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
// 1 Run this code and take note of the addresses. Run it again and compare the addresses.
......@@ -33,8 +36,10 @@ printf("float1 = %d Memory address%p\n", float1, &float1);
// 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
// You may want to rearrange your printfs to match the order of your declarations
......
// Ex 3
#include <stdio.h>
int main () {
int var = 20;
int *ip1 = &var;
int *ip2;
return 0;
}
// Exercise Tasks
// 1 Run the code. What is ip2 pointing at? Is this safe?
// 2 ip2 might be pointing at an integer. Print out the value ip2 is pointing at as an integer
// 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?
// 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
// Ex 2
#include <stdio.h>
int main () {
int var = 20;
// Exercise tasks
// 1 Create a pointer variable that stores the address of var
// 2 Print out the address of var using &var
// 3 Print out the value stored in your pointer, ensure it is the same as the address from &var
// 4 Print out the value of var
// 5 Print out the value your pointer is pointing to using the * redirector
// 6 After your printfs change the value stored in var using var = and output the four printfs from 2-6 again.
// 7 Now change the value using your pointer and the * redirection. Again output the four printfs after to see what has changed.
return 0;
}
\ No newline at end of file
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