Before the tutorial session, try your best to solve problems below and be prepared to discuss them at the tutorial session.
int x;
int &x;
ptr x;
int *x;
a
?
*a;
a;
&a;
address(a);
a
?
a;
*a;
&a;
address(a);
a
?
a;
val(a);
*a;
&a;
new
malloc
create
value
free
delete
clear
remove
static inline void swap(int *m, int *n)
{
int temp = *m;
*m = *n;
*n = temp;
}
int main(int argc, char **argv)
{
int x,y;
x=42;
y=100;
swap(&x, &y);
}
#include <stdlib.h>
static inline void swap(int *m, int *n)
{
int temp = *m;
*m = *n;
*n = temp;
}
int main(int argc, char **argv)
{
int * x;
int * y;
x = calloc(1,sizeof(int));
y = calloc(1,sizeof(int));
*x = 42;
*y = 100;
swap(x, y);
}
#include <stdlib.h>
static inline void swap(int *m, int *n)
{
int * temp = m;
m = n;
n = temp;
}
int main(int argc, char **argv)
{
int * x;
int * y;
x = calloc(1,sizeof(int));
y = calloc(1,sizeof(int));
*x = 42;
*y = 100;
swap(x, y);
}
#include <stdlib.h>
static inline void swap(int **m, int **n)
{
int * temp = *m;
*m = *n;
*n = temp;
}
int main(int argc, char **argv)
{
int * x;
int * y;
x = calloc(1,sizeof(int));
y = calloc(1,sizeof(int));
*x = 42;
*y = 100;
swap(&x, &y);
}
Analyze the following C source code. Discuss what it does (this prepares you for the 1st programming projecct).
dm510_msgbox.c
#include "dm510_msgbox.h"
#include <stdlib.h>
#include <string.h>
typedef struct _msg_t msg_t;
struct _msg_t{
msg_t* previous;
int length;
char* message;
};
static msg_t *bottom = NULL;
static msg_t *top = NULL;
int dm510_msgbox_put( char *buffer, int length ) {
msg_t* msg = malloc(sizeof(msg_t));
msg->previous = NULL;
msg->length = length;
msg->message = malloc(length);
memcpy(msg->message, buffer, length);
if (bottom == NULL) {
bottom = msg;
top = msg;
} else {
/* not empty stack */
msg->previous = top;
top = msg;
}
return 0;
}
int dm510_msgbox_get( char* buffer, int length ) {
if (top != NULL) {
msg_t* msg = top;
int mlength = msg->length;
top = msg->previous;
/* copy message */
memcpy(buffer, msg->message, mlength);
/* free memory */
free(msg->message);
free(msg);
return mlength;
}
return -1;
}
int main(int argc, char** argv) {
char *in = "This is a stupid message.";
char msg[50];
int msglen;
/* Send a message containing 'in' */
dm510_msgbox_put(in, strlen(in)+1);
/* Read a message */
msglen = dm510_msgbox_get(msg, 50);
return 0;
}
#include <stdio.h>
int main() {
int x;
int y;
int array[8][8];
for ( x = 0; x < 8; x++ ) {
for ( y = 0; y < 8; y++ ) {
array[x][y] = x * y;
}
}
}
#include <stdio.h>
int main(int argc, char** argv)
{
stupid();
stupid();
return 0;
}
int stupid()
{
static int i=17;
printf("i is %d\n", i);
i++;
}
void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
#include <stdio.h>
#include <stdlib.h>
static int comp(const void * x, const void * y)
{
int a = (int)(*(int*)x);
int b = (int)(*(int*)y);
if (a==b)
return 0;
else
if (a < b)
return -1;
else
return 1;
}
int main(int argc, char* argv[])
{
int numbers[10]={1892,45,200,-98,4087,5,-12345,1087,88,-100000};
int i;
/* Sort the array */
qsort(numbers,10,sizeof(int),comp) ;
for (i=0;i<9;i++)
printf("Number = %d\n",numbers[ i ]) ;
return 0;
}
#include <stdio.h>
#define SQR(a)((a)*(a))
#define CUB(a)((a)*(a)*(a))
inline static int cub(int x) { return x*x*x; }
int main()
{
int x;
x=3;
printf("square? :%d\n",SQR(x));
printf("x: %d\n",x);
x=3;
printf("square? :%d\n",SQR(++x));
printf("x: %d\n",x);
x=3;
printf("cub? :%d\n",CUB(x++));
printf("x: %d\n",x);
x=3;
printf("cub? :%d\n",CUB(++x));
printf("x: %d\n",x);
x=3;
printf("cub? :%d\n",cub(++x));
printf("x: %d\n",x);
return 0;
}
Discuss the exercises prepared at home, form teams (2 students) for programming projects.