



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Excercises On Pointer In C Programming Language.
Typology: Exercises
1 / 7
This page cannot be seen from the preview
Don't miss anything!




/* 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; }
/* 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); }
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
' '
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'