Programming Language About Pointers, Exercises of C programming

Excercises On Pointer In C Programming Language.

Typology: Exercises

2021/2022

Uploaded on 02/11/2022

kaijiang
kaijiang ๐Ÿ‡บ๐Ÿ‡ธ

4.5

(8)

280 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSC 2400 โ€“ Exercises on Pointers in C
This handout involves playing with a number of C programs to solve some problems involving pointers.
There are 4 different problems. Work on as many as you can in class, and finish them at home. If you are a
pointer guru, try your hand at the segvhunt (exercise 5). Exercise 5 goes beyond what we expect you to know
at the moment ...
The exercises here should help make sure you understand pointers. It's important to make sure you understand
things (get help if you are stuck!).
Exercise 1
/* p1.c
Write a short C program that declares and initializes (to any value you like) a
double, an int, and a char. Next declare and initialize a pointer to each of
the three variables. Your program should then print the address of, and value
stored in, and the memory size (in bytes) of each of the six variables.
Use the โ€œ0x%xโ€ formatting specifier to print addresses in hexadecimal. You
should see addresses that look something like this: "0xbfe55918". The initial
characters "0x" tell you that hexadecimal notation is being used; the remainder
of the digits give the address itself.
Use โ€œ%fโ€ to print a floating value. Use the sizeof operator to determine the
memory size allocated for each variable.
Sample output:
The address of char ___ is 0x_______
The address of int ___ is 0x_______
The address of double ___ is 0x_______
The address of char* ___ is 0x_______
The address of int* ___ is 0x_______
The address of double* ___ is 0x_______
The value of char ___ is _______
The value of int ___ is _______
The value of double ___ is _______
The value of char* ___ is 0x_______
The value of int* ___ is 0x_______
The value of double* ___ is 0x_______
The size of char is _______ bytes
The size of int is _______ bytes
The size of double is _______ bytes
The size of char* is _______ bytes
The size of int* is _______ bytes
The size of double* is _______ bytes
*/
pf3
pf4
pf5

Partial preview of the text

Download Programming Language About Pointers and more Exercises C programming in PDF only on Docsity!

CSC 2400 โ€“ Exercises on Pointers in C

This handout involves playing with a number of C programs to solve some problems involving pointers.

There are 4 different problems. Work on as many as you can in class, and finish them at home. If you are a

pointer guru, try your hand at the segvhunt (exercise 5). Exercise 5 goes beyond what we expect you to know

at the moment ...

The exercises here should help make sure you understand pointers. It's important to make sure you understand

things (get help if you are stuck!).

Exercise 1

/* p1.c Write a short C program that declares and initializes (to any value you like) a double, an int, and a char. Next declare and initialize a pointer to each of the three variables. Your program should then print the address of, and value stored in, and the memory size (in bytes) of each of the six variables. Use the โ€œ0x%xโ€ formatting specifier to print addresses in hexadecimal. You should see addresses that look something like this: "0xbfe55918". The initial characters "0x" tell you that hexadecimal notation is being used; the remainder of the digits give the address itself. Use โ€œ%fโ€ to print a floating value. Use the sizeof operator to determine the memory size allocated for each variable. Sample output: The address of char ___ is 0x_______ The address of int ___ is 0x_______ The address of double ___ is 0x_______ The address of char* ___ is 0x_______ The address of int* ___ is 0x_______ The address of double* ___ is 0x_______ The value of char ___ is _______ The value of int ___ is _______ The value of double ___ is _______ The value of char* ___ is 0x_______ The value of int* ___ is 0x_______ The value of double* ___ is 0x_______ The size of char is _______ bytes The size of int is _______ bytes The size of double is _______ bytes The size of char* is _______ bytes The size of int* is _______ bytes The size of double* is _______ bytes */

Exercise 2 /* p2.c Find out (add code to print out) the address of the variable x in foo1, and the variable y in foo2. What do you notice? Can you explain this? / #include <stdio.h> void foo1(int xval) { int x; x = xval; / print the address and value of x here / } void foo2(int dummy) { int y; / print the address and value of y here */ } int main() { foo1(7); foo2(11); return 0; }

Exercise 4 /* p4.c swap_nums seems to work, but not swap_pointers. Fix it. */ #include <stdio.h> void swap_nums(int *x, int *y) { int tmp; tmp = *x; *x = *y; *y = tmp; } void swap_pointers(char *x, char *y) { char *tmp; tmp = x; x = y; y = tmp; } int main() { int a,b; char s1,s2; a = 3; b=4; swap_nums(&a,&b); printf("a is %d\n", a); printf("b is %d\n", b); s1 = "I should print second"; s2 = "I should print first"; swap_pointers(s1,s2); printf("s1 is %s\n", s1); printf("s2 is %s\n", s2); return 0; }

Exercise 5

For those who want more!

Hints:

If you are still not comfortable with pointers, you might try the suggestions below to work through things.

Draw a table as a representation of the memory of the computer that is used by your process. Something

like this:

/* segvhunt.c Find and eliminate all code that generates Segmentation Fault */ #include <stdio.h> int main() { char **s; char foo[] = "Hello World"; *s = foo; printf("s is %s\n",s); s[0] = foo; printf("s[0] is %s\n",s[0]); return(0); }

Now keep in mind the following examples of how various C expressions relate to the picture above:

Expression Description Value from example i The value of variable^ i^ 22 &i The address of variable i 112 s The value of variable^ s^ 116 &s The address of variable s *104 s The character at the address s (remember that s has a value, it's value is an address, so this refers to the character at address 116). 'H' s[3] The character at the address 3 bytes past the address in s, in other words, at address s+3 or

'F' s+ The address obtained by adding 2 to the value of s.^118 *(s+2) The character at the address 2 bytes past the address in s, in other words, at address s+2 or

' '

Want more? OK - add this to the picture:

char **p = &s;

Now p is a variable that holds in it the address of s. Assume p is placed in memory at address 100:

100 value of p: 104

104 value of s: 116

112 value of i: 22

116 Hi F

120 red\ 0

Expression Description Value from example p The value of variable^ p^ 104 &p The address of variable^ p^ *100 p The value (of type char *) in memory at address p (p is an address, it's value is 104). 116 (p) The character at the address *p, which is the character at address 116 'H'