Name: ______________________ NetId: _______________ Section: _______
Print out this prelab and answer the following questions. Hand in this assignment at the beginning of Lab 11. You should use the material found in Lectures 18, 19&20 of the course notes and/or the course C book Chapters 12.1, 12.2, 20, and 22.2.1. As with all prelabs/labs, if you are not sure of the answer you may go to any EWS lab on campus and check your results by using the gcc compiler, or you can ask anyone of the CS101 staff for help.
The following exercises are about pointers in C and should help prepare you for future lab activities MP2 and the Final Exam.
In this prelab, you'll demonstrate your knowledge regarding,
Scope of identifiers
Global variables
Pointers, & and * operators
Write the output when the following program executes.
#include <stdio.h>
int i = -10; /* global variable */
int function2(int x)
{
int i = 20; /* another variable named i */
printf(" %i \n", i);
return x * i;
}
void main(void)
{
printf(" %i \n", function2(i));
printf(" %i\n", i);
}
________________________________________________
__________________________________________
__________________________________________
Write the output of the following code. You may have to compile and run this program.
#include <stdio.h>
void main(void)
{
int x;
int * ptr; /* ptr is a "pointer" variable. Assign only computer memory addresses to ptr !!! */
x = 50;
printf("x is %i\n", x);
ptr = &x; /* ptr now points to x , &x means... address of x */
*ptr = 20; /* * operator (dereferencing operator), *ptr means ... the thing ptr points to (which is x in this example)*/
printf("x is %i\n", x);
++x;
printf("*ptr = %i \n", *ptr); /* so using *ptr is like using x */
++(*ptr); /* has same effect as ++x */
printf("x = %i\n",x);
}
________________________________________
________________________________________
________________________________________
________________________________________
Write the output when the following program executes. See lecture 19&20 slide #21.
#include <stdio.h>
void main(void)
{
char str[] = "royal";
char * ptrA = "crown"; /* note that ptrA is a pointer, it won't hold data of type char, it holds addresses */
char * ptrB = str; /* the name of an array holds the address of the first byte of an array */
/* so an array name is a pointer (its value can't be changed however) */
printf(" %s ", str);
printf(" %s ", ptrA);
printf(" %s \n", ptrB);
printf("%c %c \n", ptrA[3], *ptrB); /* if the name of an array is a pointer conversely a pointer can be used */
/* an array name */
}
_________________________________________________________
_________________________________________________________
Write the output of the following program.
#include <stdio.h>
void swap1(int a,int b)
{
int temp = b;
b = a;
a = temp;
}
void swap2(int *a,int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void swap3(int * x)
{
int temp = x[0];
x[0] = x[1];
x[1] = temp;
}
void main(void)
{
int a = 10, b = 5;
int c[2];
printf("%i %i\n",a,b);
swap1(a,b);
printf("%i %i\n",a,b);
swap2(&a,&b);
printf("%i %i\n",a,b);
c[0] = a + b; c[1] = b - a;
/* 'c' is the name of an array therefore c is a constant pointer */
/* see lecture 19&20 slide #18 */
swap3(c);
printf("%i %i\n",c[0],c[1]);
}
_________________________________
_________________________________
_________________________________
_________________________________
Write the output of the following code. It might help to draw a picture of memory. Remember, if p2 is a pointer then p2[0] means *(p2 + 0).
#include <stdio.h>
void main(void)
{
int x[2]={1,2};
int *p1, *p2;
int a = 10, b = 30;
printf("%i %i \n",x[0],x[1]);
p1 = x;
printf("%i %i \n",p1[0],p1[1]);
p2 = &p1[1];
printf("%i \n",p2[0]);
*p1 = a * b;
*p2 = b / a;
printf("%i %i \n",p1[0],p1[1]);
printf("%i \n",p2[0]);
}
_________________________________
_________________________________
_________________________________
_________________________________
_________________________________