Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
CTasks2
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
elijah vasquez
CTasks2
Commits
ed343199
Commit
ed343199
authored
Jan 14, 2023
by
elijah vasquez
🦍
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
AMBATUCOMMIT
parent
550c7799
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
51 additions
and
15 deletions
+51
-15
memoryaddresses.c
memoryaddresses.c
+19
-10
memoryaddresses.exe
memoryaddresses.exe
+0
-0
pointerchecking.c
pointerchecking.c
+24
-3
pointerchecking.exe
pointerchecking.exe
+0
-0
pointers.c
pointers.c
+8
-2
pointers.exe
pointers.exe
+0
-0
No files found.
memoryaddresses.c
View file @
ed343199
...
...
@@ -2,21 +2,26 @@
#include <stdio.h>
#include <stdalign.h>
int
main
(
void
)
{
int
integer1
=
1
;
short
short2
=
2
;
int
integer3
=
3
;
short
short2
=
2
;
char
char1
=
4
;
//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
...
...
@@ -33,11 +38,15 @@ printf("float1 = %d Memory address%p\n", float1, &float1);
// integer3 =3 Memory address0x7ffeb7370264
// 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.
// 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
...
...
memoryaddresses.exe
View file @
ed343199
No preview for this file type
pointerchecking.c
View file @
ed343199
...
...
@@ -5,13 +5,34 @@ int main () {
int
var
=
20
;
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
;
}
// 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
// 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
: Prints negative num
// 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
pointerchecking.exe
0 → 100644
View file @
ed343199
File added
pointers.c
View file @
ed343199
...
...
@@ -3,7 +3,13 @@
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
// 1 Create a pointer variable that stores the address of var
...
...
pointers.exe
0 → 100644
View file @
ed343199
File added
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment