Edgar SIMO-SERRA
int my_var = 5000;
(hexadecimal 0x00001388)Address | Value | Contents |
---|---|---|
0x00000000 | 0x00 | integer |
0x00000001 | 0x00 | integer |
0x00000002 | 0x13 | integer |
0x00000003 | 0x88 | integer |
int my_var = 10000;
double my_double = 10000.;
Address | Value | Contents |
---|---|---|
0x00000000 | 0x00 | integer |
0x00000001 | 0x00 | integer |
0x00000002 | 0x27 | integer |
0x00000003 | 0x10 | integer |
0x00000004 | 0x00 | double |
0x00000005 | 0x00 | double |
0x00000006 | 0x00 | double |
0x00000007 | 0x00 | double |
0x00000008 | 0x40 | double |
0x00000009 | 0xC3 | double |
0x0000000A | 0x88 | double |
0x0000000B | 0x00 | double |
&
: gets the pointer value of a variable*
: gets the value of a pointer*
after the variable type:int *x; /* x is a pointer to an int-type variable */
int my_variable = 5;
int my_other_variable = 3;
int *my_pointer;
my_pointer = &my_variable;
printf( "%d\n", *my_pointer ); /* 5 */
my_pointer = &my_other_variable;
printf( "%d\n", *my_pointer ); /* 3 */
*my_pointer = 7;
printf( "%d\n", my_other_variable ); /* 7 */
#include <stdio.h>
void my_func( int *x ) {
*x = (*x) * 5 + 3;
}
int main (void) {
int a = 5;
my_func( &a );
printf( "%d\n", a ); /* ?? */
return 0;
}
scanf
int n;
scanf( "%d", &n );
[]
operator is performing arithmetic on pointersint my_array[3] = { 1, 3, 5 };
int *my_pointer = my_array; /* No need for & */
printf( "%d\n", my_array[1] ); /* 3 */
printf( "%d\n", *(my_array+1) ); /* 3 */
printf( "%d\n", my_pointer[1] ); /* 3 */
printf( "%d\n", *(my_pointer+1) ); /* 3 */
my_pointer = my_pointer + 1;
printf( "%d\n", my_pointer[1] ); /* ?? */
int my_int_array[3];
double my_double_array[3];
printf( "%p\n", my_int_array ); /* 0x7ffe1a6947b4 */
printf( "%p\n", my_int_array+1 ); /* 0x7ffe1a6947b8 */
printf( "%p\n", my_double_array ); /* 0x7ffe1a6947c0 */
printf( "%p\n", my_double_array+1 ); /* 0x7ffe1a6947c8 */
printf( "%d\n", &my_int_array[1]-&my_int_array[0] ); /* 1 */
#include <stdio.h>
int main (void)
{
int *my_var;
printf( "%d\n", *my_var );
return 0;
}
$ gcc crash.c -o crash
$ ./crash
bash: segmentation fault (core dumped) ./crash
#include <stdio.h>
int* my_func( int x ) {
int a[2];
a[0] = x+1;
a[1] = x;
return a;
}
int main (void)
{
int my_var = 5;
int *my_pointer = my_func( my_var );
printf( "%d\n", *my_pointer );
return 0;
}
#include <stdio.h>
void swap( int *a, int *b ) {
int c = *a;
*a = *b;
*b = c;
}
int main (void) {
int my_var = 3;
int my_other_var = 5;
printf( "var: %d, other: %d\n", my_var, my_other_var ); /* 3, 5 */
swap( &my_var, &my_other_var );
printf( "var: %d, other: %d\n", my_var, my_other_var ); /* 5, 3 */
return 0;
}
#include <stdio.h>
#define MAXN 30
void process( double *x ) {
*x = (*(x+1) + *x)/2;
}
int main (void) {
double my_array[MAXN];
for (int i=0; i<MAXN; i++) {
my_array[i] = (double)i;
}
for (int i=0; i<MAXN-1; i++) {
process( &my_array[i] );
}
for (int i=0; i<MAXN; i++) {
printf( "%.1f ", my_array[i] );
}
return 0;
}
#include <stdio.h>
int main (void) {
int my_num = 5;
int *my_pointer = &my_num;
int **my_double_pointer = &my_pointer;
printf( "Value of my_double_pointer = %p\n", *my_double_pointer );
printf( "Value of pointer pointed by my_double_pointer = %d\n", **my_double_pointer );
return 0;
}
#include <stdio.h>
#define N 20
void get_min_and_max( int *array, int n, /* more parameters needed */ ) {
/* implement */
}
int main (void) {
int array[N] = { 18, 16, 10, 12, 4, 21, 2, 20, 9, 7, 11, 19, 5, 14, 3, 15, 13, 8, 6, 17 };
/* get the min and max values of array and print them */
get_min_and_max( array, N, /* what parameters? */ );
return 0;
}
swap_nums
works but swap_pointers
does not. 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 (void) {
int a=3, b=4;
char *s1, *s2;
swap_nums(&a,&b);
printf("a is %d\nb is %d\n", a, b);
s1 = "I should print second";
s2 = "I should print first";
swap_pointers( s1, s2 );
printf("s1 is %s\ns2 is %s\n", s1, s2);
return 0;
}
#include <stdio.h>
#define N 20
void copy_array( /* parameters needed */ ) {
/* implement */
}
int main (void) {
int array[N] = { 18, 16, 10, 12, 4, 21, 2, 20, 9, 7, 11, 19, 5, 14, 3, 15, 13, 8, 6, 17 };
int output[N];
/* copy array into output backwards and print the result */
}
#include <stdio.h>
#define N 3
void m_print( double **M ) {
/* implement */
}
void m_swap_rows( double **M, int a, int b ) {
/* implement */
}
void m_swap_columns( double **M, int a, int b ) {
/* implement */
}
int main (void) {
double *M[N];
double A[N] = {1, 3, 5};
double B[N] = {2, 4, 6};
double C[N] = {1, 2, 3};
M[0] = A; M[1] = B; M[2] = C;
m_swap_columns( M, 0, 2 );
m_swap_rows( M, 0, 2 );
m_print( M );
return 0;
}