- Write a function alter with two parms passed by reference: x and y. alter
changes the value of x to
x+y and changes the value of y to x*y. Write a main function to read in two integers,
print the two integers, call the function alter using the two integers as parameters,
and print the two integers again after the call to alter.
- Write a function swap which has two parms passed by reference: num1 and num2.
swap will exchange the values of the two parms. For example, if you
pass 5 and 10, swap will change them to 10 and 5. Write a main function to
read in two integers,
print the two integers, call the function swap using the two integers as parameters,
and print the two integers again after the call to swap.
- Show what is printed by the following program:
int main()
{
char a = 'x', b = 'y';
void func1(char,char);
void func2(char *,char *);
func1(a,b);
printf("a = %c b = %c\n",a,b);
func2(&a,&b);
printf("a = %c b = %c\n",a,b);
return 0;
}
void func1(char a, char b)
{
a = 'p';
b = 'q';
}
void func2(char *a, char *b)
{
*a = 'p';
*b = 'q';
}
Assume that the variables declared below are stored at the following
locations. Show what value is stored as a result of each of the following statements.
Assume that each statement uses the values stored by the previous statements.
int *p, *q, *r;
int a = 10; b = 25;
int c[4] = {6,12,18,24};
address variables
5000 p
5004 q
5008 r
500C a
5010 b
5014 c[0]
5018 c[1]
501C c[2]
5020 c[3]
- p = c;
- q = &a;
- r = p + 2;
- c[1] = *p;
- c[2] = *(p + 2);
- c[3] = *p + 2;
- *r = *q;
- r = q;
- p = &c[0];
- p++;
Change the following array references to equivalent pointer references:
- p[2]
- &p[3]
- What is output by the following program?
#include <stdio.h>
int main()
{
int num = 10;
int *ptr = #
printf("%d %d\n", num, *ptr);
num = 5;
printf("%d %d\n", num, *ptr);
*ptr = 8;
printf("%d %d\n", num, *ptr);
return 0;
}
Given the following code fragment. Suppose an integer occupies 4 bytes. Write a
C statement for each of the following 4 questions:
int z = 56;
int *switch = &z;
int y;
- Add 10 to the value referenced by pointer switch.
- Add 4 to the address stored in pointer switch.
- Subtract 5 from the value referenced by the pointer.
- Copy the value stored 8 bytes after the pointer
to the variable y.
- Write the statements to declare an array of 10 characters called name,
declare a character pointer called nameptr, and initialize nameptr to point
to the first element of name.
- Now declare nameptr and initialize it to point to the fourth element of name.
- If ptr is a pointer to a character and has been initialized to point to the
first element of arr, write a statement using ptr that is equivalent to the
statement array[4] = 12;
- If ptr is a pointer to a character array called text, what array element
is referenced by the statement x = *(ptr+5);?
- What is printed by the following?
int nums[6];
int *pi;
int i;
for (i = 5 ; i >= 0 ; i--) {
*(nums+i) = 5 * (i + 1);
printf("%d ",*(nums+i));
}
printf("\n");
for (pi = nums, i = 0 ; i < 6 ; i++)
printf("%d ",*pi++);
printf("\n");
for (i = 0 ; i < 5 ; i++)
*(nums+i+1) = *(nums+i);
for (i = 0 ; i < 6 ; i++)
printf("%d ",*(nums+i));
printf("\n");
for (i = 0 ; i < 6 ; i++)
*(nums+i) = *nums+i;
for (i = 0 ; i < 6 ; i++)
printf("%d ",*(nums+i));
printf("\n");
-
Write a function reverse which has 3 parms: two integer arrays
(named one and two) and the size of the arrays (an int). The
function copies the values from two into one in such that one
contains the reverse of two. That is, one[0] = two[size-1],
one[1] = two[size-2], etc. The lengths of both arrays are the same.
Use pointers; do not use subscripts.
-
Write a function called count that will count and return the occurrences
of a given character in a string named letters. The parameters will
be the string letters and the character to count. For example, if the
string contains the values
x8RAaa0sSaA
and the character to count is 'a', then the function returns 3.
Use pointers; do not use subscripts.
- What does the following code do? Trace it with a sample
string and sample character.
void process(char *str, char ch)
{
int from = 0;
while (*str)
{
if (*str == ch)
from++;
else
str++;
*str = *(str + from);
}
}
- Rewrite your strlen function using pointers.
- Rewrite your strcpy function using pointers.
- Rewrite your strcat function using pointers.