ECEN 330 Introduction to Embedded Systems K&R 5

// Topic: Basic pointers.
// Question: What value with the program print?
#include <stdio.h>
int main() {
int x=4;
int *y = &x;
y++;
printf("%d\n", *y);
}

GARBAGE
incrementing y increments the ADDRESS it points to (not the value in the address that it points to), which could be totally random garbage in the memory

// Topic: Basic pointers with post-increment
// Question: What value with the program print?
#include <stdio.h>
int main() {
int x = 4;
int y = &x;
y++;
printf("%d\n", y++);
}

GARBAGE
specifically it will print out the memory location AFTER the memory address of x

#include <stdio.h>
// Topic: Basic pointers with post-increment.
// Question: What value with the program print?
int main() {
int x = 4;
int* y = 5;
y++;
y++;
printf("%d\n", y++);
}

It will print out the address TWO int addresses after 5, which, if your int is 4 bytes long, will be 13
that is, y is a pointer that contains the address 5
after double incrementing, it contains the address 5 + 2*sizeof(int)

// Topic: Basic pointers with post-increment.
// Question: What value with the program print?
#include <stdio.h>
int main() {
int x = 4;
int* y = &x;
(*y)++;
printf("%d\n", y);
}

So this program will print out the value of the pointer y, which is the address of x
admittedly this program DOES properly increment x to 5, but that's not what it is printing

// Topic: Basic pointers with post-increment.
// Question: What value with the program print?
#include <stdio.h>
int main() {
int x = 4;
int* y = &x;
(*y)++;
printf("%d\n", *y++);
}

5
x is assigned 4
the pointer y is assigned the address of x
the pointer is dereferenced and therefore the value it points to is incremented (so x goes from 4 to 5)
print the dereferenced pointer and then increment

// P6 - P11 are variations on the swap() program.
// See how the various changes to the code impact the results.
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
}
// Question: What will this program print?
int main() {
int x = 10, y = 20;
swap(x,

10
swap receives the VALUES of x and y and messes around with it's own a and b values, but the original x and y remain untouched

// P6 - P11 are variations on the swap() program.
// See how the various changes to the code impact the results.
// Question: What will this program print?
void swap(int
a, int
b) {
int temp = a;
a = b;
b = temp;
}
#include <stdio.h>
int main() {
int x =

10
First off this program gives you tons of warnings
a is a pointer to an int
b is a pointer to an int
x gets passed to a, so a is pointing to whatever is at address 10
y gets passed to b, so b is pointing to whatever is at address 20
an INT temp gets ass

// P6 - P11 are variations on the swap() program.
// See how the various changes to the code impact the results.
void swap(int
a, int
b) {
int temp = a;
*a = b;
b = temp;
}
#include <stdio.h>
int main() {
int x = 10, y = 20;
swap(&x, &y);
printf("%d\n\r",

GARBAGE
okay this one is fun
it start out okay - the ADDRESSES of x and y (see the &s in the function call?) get passed to the pointers a and b, so a now points to x (value of 10) and b points to y (value of 20)
but then temp gets assigned the ADDRESS of

// P6 - P11 are variations on the swap() program.
// See how the various changes to the code impact the results.
// Question: What will this program print (note that it is printing y)?
void swap(int
a, int
b) {
int* temp = a;
a =
b;
*b = temp;
}
#include

This program prints y which has been assigned the address of x : (
X DOES get swapped to 20 though!
anyway, the issue is that *b (which is the dereferenced point b pointing at y) is assigned the value of temp. Sitting in temp is the address of x, because

// P6 - P11 are variations on the swap() program.
// See how the various changes to the code impact the results.
// Question: What will this program print (note that it is printing y)?
void swap(int
a, int
b) {
int temp = a;
a =
b;
*b = temp;
}
#include <

This prints out the address of x again
SOO close to being good but the issue is that *b gets temp and temp gets a (which is NOT dereferenced)

// P6 - P11 are variations on the swap() program.
// See how the various changes to the code impact the results.
// Question: What will this program print (note that it is printing y)?
void swap(int
a, int
b) {
int temp = *a;
a =
b;
*b = temp;
}
#include

10
YESSS
SUCCESSS!!!!
everything is properly dereferenced and what not here : )

// Topic: Pointer declaration syntax with arrays.
// Question:(T or F) The program below will generate an error or warning when compiled.
#include <stdio.h>
#define SIZE 20
int main() {
char str[SIZE] = "hi there!\n\r";
char* strp = str;
}

FALSE
an array is actually just a pointer to the address of the first element in the array, so assigning a pointer to an array is fiiiiine!

// Topic: Using pointers and arrays.
// Question: What will this program print?
#include <stdio.h>
#define SIZE 20
int main() {
char str[SIZE] = "hi-there!\n\r";
char* strp = &str[8];
printf("%c\n\r", strp);
}

GARBAGE crazy stuff, who knows what
strp gets assigned the address of the 8th element in the array (don't forget to count from 0) which is '!'
however, when printing, strp isn't dereferenced and so the ADDRESS of that '!' is passed in and the %c goes and

// Topic: Using pointers, arrays, and pointer-addressing.
// Question: What will this program print?
#include <stdio.h>
#define SIZE 20
int main() {
char str[SIZE] = "hi-there!\n\r";
char* strp = &str[7];
printf("%c\n\r", *(strp+1));
}

!
strp was given the address for the 7th element in the array, the 'e', but then strp+1 is dereferenced in the printf, so the 8th element is passed in and printed
note that adding 1 to a pointer doesn't just add 1 to the value, but rather adds 1*sizeof(ty

// Topic: Using pointers, arrays, and pointer-addressing.
// Question: What will this program print?
#include <stdio.h>
#define SIZE 20
int main() {
char str[SIZE] = "hi-there!\n\r";
char* strp = &str[1];
printf("%c\n\r", *(strp++));
}

i
similar to the other one - strp gets the address for the 1st element in the array, the 'i', and then that is what is dereferenced in print (and THEN strp is incremented to be pointing at the '-')

// Topic: Using pointers, arrays, and pointer-addressing.
// Question: What will this program print?
#include <stdio.h>
#define SIZE 20
int main() {
char str[SIZE] = "hi-there!\n\r";
char* strp = str;
printf("%c\n\r", *(++strp));
}

i
strp gets the address for 'h' but is incremented to the address for 'i' before being dereferenced

// Topic: Using pointers, arrays, and pointer-addressing.
// Question: (T or F) This program will compile correctly.
#include <stdio.h>
#define SIZE 20
int main() {
char str[SIZE] = "hi-there!\n\r";
char* strp = str++;
printf("%c\n\r", *strp);
}

FALSE
the issue isn't that strp is passed str
the issue is that arrays are IMMUTABLE and you can't change where they are pointing to (though you CAN change the value inside the address they point to)
so the ++ after str really freaks out the compiler

// Question: What does this program print?
#include <stdio.h>
void mystery(char a[]) {
a[1] = 'b';
}
#define SIZE 20
int main() {
char str[SIZE] = "hi-there!\n\r";
mystery(str);
printf("%c\n\r", str[1]);
}
// Ans: h, (b), i, none of these

b
passing in arrays is just like passing in pointers and so the ACTUALL outter array gets changed!
very convenient but also tricky sometimes...
(ps after this program runs the whole str is "hb-there!\n\r")

// Question: What does this program print?
#include <stdio.h>
void mystery(char a[]) {
*(a+1) = 'b';
}
#define SIZE 20
int main() {
char str[SIZE] = "hi-there!\n\r";
mystery(str);
printf("%c\n\r", str[1]);
}

b
very similar to the other mystery function
the array gets passed into the function and the dereferenced address, *(a+1) is the same is saying a[1], and it gets the value of 'b'
at the end of this, str is "hb-there!\n\r

// Question: What does this program print?
#include <stdio.h>
void mystery(char* a) {
*(a+1) = 'b';
}
#define SIZE 20
int main() {
char str[SIZE] = "hi-there!\n\r";
mystery(str);
printf("%c\n\r", str[1]);
}

b
very similar to the other mystery function
the array gets passed into the function and the dereferenced address, *(a+1) is the same is saying a[1], and it gets the value of 'b'
it's okay that mystery takes a pointer, because that is what an array actual

// Question: What does this program print?
#include <stdio.h>
void mystery(char a[]) {
*(a++) = 'b';
}
#define SIZE 20
int main() {
char str[SIZE] = "hi-there!\n\r";
mystery(str);
printf("%c\n\r", str[1]);
}

i
so this function seems the same as the other myster functions, but the address a is referring to isn't incremented until AFTER dereferencing (because a++ is using the POST decrement operator)
so str[1] is still 'i'
but str[0] is now 'b' instead of 'h' !

/ Topic: Pointers to pointers and character arrays.
// Question: What does this program print?
#include <stdio.h>
void swap(char
a, char
b) {
char tmp = *a;
a =
b;
*b = tmp;
}
#define SIZE 20
int main() {
char s1[SIZE] = "hello";
char s2[SIZE] = "bye";
sw

(none of these)
what will actually print is
bello
why?
you need a pointer to a pointer to handle this one
the first string become "bello" and the second, "hye", because swap simply swaps the values at the 0th indexes, not the entire strings

// Question: What does this program print?
#include <stdio.h>
void swap(char*
a, char
* b) {
char* tmp;
tmp = *a;
a =
b;
*b = tmp;
}
#define SIZE 20
int main() {
char s1[SIZE] = "bye";
char* s1p = s1;
char** s1pp = &s1p;
char s2[SIZE] = "hello";
char* s2p

bye
so what's going on?
first off, note that you need to declare the pointers s1p and s2p, because you ARE NOT ALLOWED TO CHANGE ARRAYS. they are immutable and putting them in the swap function raises an error
anyway - s1p is a pointer to the first elemen

// Topic: Pointers and 2D arrays.
// Note that the swap() function is identical though P29.
// Question: What does this program print?
#include <stdio.h>
void swap(char*
a, char
* b) {
char* tmp;
tmp = *a;
a =
b;
*b = tmp;
}
#define SIZE 20
int main() {
c

hello
so this one SEEMS more difficult than the previous, but it's ACTUALLY the same!
think of s as a 2d array, an array of arrays of chars (BUT IT ISN"T IT IS A POINTER TO POINTERS POINTING @ CHARS, see pg 113 of K&R)
s is pointing to the first address i

// Topic: Pointers and 2D arrays.
// Question: What does this program print?
#include <stdio.h>
void swap(char*
a, char
* b) {
char* tmp;
tmp = *a;
a =
b;
*b = tmp;
}
int main() {
char *s[10] = {"bye", "hello"};
swap(s, s+1);
printf("%s\n\r", s[0]);
}

hello
same as above
*s == s[0]

// Topic: Pointers and 2D arrays.
// Question: What does this program print?
#include <stdio.h>
void swap(char*
a, char
* b) {
char* tmp;
tmp = *a;
a =
b;
*b = tmp;
}
int main() {
char *s[10] = {"bye", "hello"};
swap(s, s+1);
printf("%s\n\r", s+1);
}

GARBAGE
same as above but instead of "s+1" it needs to be "*(s+1)" not dereferencing it passes the actual address of the pointer being pointed at, instead of the address of the data being pointed at by the pointer being pointed at (did you follow that? ^_

// Topic: Pointers and 2D arrays.
// Question: What does this program print?
#include <stdio.h>
void swap(char*
a, char
* b) {
char* tmp;
tmp = *a;
a =
b;
*b = tmp;
}
int main() {
char *s[10] = {"bye", "hello"};
swap(s, s+1);
char
sp =
s;
printf("%s\n\r",

ello
easiest way to think of it is that s is an array of arrays of chars (BUT IT ISN"T IT IS A POINTER TO POINTERS POINTING @ CHARS, see pg 113 of K&R)
sp is assigned the address of the element in the 0th place of the array in the 0th place of the s array

// Topic: Pointers and 2D arrays.
// Question: What does this program print?
#include <stdio.h>
void swap(char*
a, char
* b) {
char* tmp;
tmp = *a;
a =
b;
*b = tmp;
}
int main() {
char *s[10] = {"bye", "hello"};
swap(s, s+1);
char** t = s;
printf("%s\n\r

bye
unlike the previous example, t is a DOUBLE POINTER to a char, not a single pointer to a char
t gets assigned to s. so t is pointing at the first element in s, which is the pointer pointing at "hello"
but then ++t moves t from pointing at the pointer p

// Topic: Pointers to functions.
// This is a simpler example than the one in the book.
// Used for numerical comparisons, same return value as strcmp.
// Note: (return-value < 0), a < b, (return-value > 0), b < a, (return-value == 0), a==b
// Question: W

5
hello
I have no idea on this one, honestly

// Question: What will this program print?
// Question: (T or F) Is the result implementation-independent.
#include <stdio.h>
int main() {
int* x = 4;
printf("%d\n\r", ++x);
}

8
(IMPLEMENTATION DEPENDENT)
AND it will give you warnings
so x is a pointer and it is pointing at whatever is in address 4
pre incrementing it increase the address that it has to point by the size of int, which in the implementation I'm using is 4 bytes.