pointers Flashcards
fonksiyona parametre geçirme yön ve birim
void ilerle(int *px, int *py, int yon, int birim);
int main()
{
int x = 0, y = 0;
ilerle(&x, &y, KUZEY, 3); printf("x: %d, y: %d\n", x, y); ilerle(&x, &y, GUNEY, 2); printf("x: %d, y: %d\n", x, y); ilerle(&x, &y, BATI, 5); printf("x: %d, y: %d\n", x, y); return 0; }
void ilerle(int *px, int *py, int yon, int birim)
{
if (yon == KUZEY)
{
*py += birim;
}
else if (yon == GUNEY)
{
*py -= birim;
}
else if (yon == DOGU)
{
*px += birim;
}
else if (yon == BATI)
{
*px -= birim;
}
}
fonksiyona parametre geçirme pass by value
int cubeByValue(int n);
int main(void) {
int number = 5; // initialize number
printf(“The original value of number is %d”, number);
number = cubeByValue(number);
printf(“\nThe new value of number is %d\n”, number);
printf(“number: %d\n”, number);
}
int cubeByValue(int n) {
return n * n * n;
}
fonksiyona parametre geçirme pass by reference
void cubeByReference(int *nPtr);
int main(void) {
int number = 5;
printf(“The original value of number is %d”, number);
cubeByReference(&number);
printf(“\nThe new value of number is %d\n”, number);
}
void cubeByReference(int *nPtr) {
*nPtr = *nPtr * *nPtr * *nPtr;
}
çıktı ne olur(işlem)
void f(int *p, int m);
int main()
{
int i = 5, j = 10;
f(&i, j);
printf(“%d\n”, i+j);
return 0; } void f(int *p, int m) { m = m + 3; *p = *p + m; }
28
çıktı ne olur(aritmetik)
char c[] = “BIL141”;
char *cp;
for (cp = &c[0]; cp <= &c[5]; cp++) { printf("%c", *cp); }
BIL141
çıktı ne olur(aritmetik)
int a[5] = {1, 2, 3, 4, 5};
int *p1 = a; int *p2 = p1 + 3; int *p3 = &a[1]; printf("%d %d %d", *p1, *p2, *p3);
1 4 2
non const p-non const data(convert string to uppercase)
include <ctype.h></ctype.h>
#include <stdio.h></stdio.h>
void convertToUppercase(char *sPtr);
int main(void) {
char string[] = “cHaRaCters and $32.98”;
printf(“The string before conversion is: %s\n”, string);
convertToUppercase(string);
printf(“The string after conversion is: %s\n”, string);
}
void convertToUppercase(char sPtr) {
while (sPtr != ‘\0’) {
sPtr = toupper(sPtr);
++sPtr;
}
}
non const p-const data(printing a string one character at a time)
include <stdio.h></stdio.h>
void printCharacters(const char *sPtr);
int main(void) {
char string[] = “print characters of a string”;
puts(“The string is:”);
printCharacters(string);
puts(“”);
}
void printCharacters(const char *sPtr) {
for (; *sPtr != ‘\0’; ++sPtr) {
printf(“%c”, *sPtr);
}
}
const p-non const data(modify data)
include <stdio.h></stdio.h>
int main(void) {
int x = 0;
int y = 0;
int * const ptr = &x;
*ptr = 7;
ptr = &y;
}
const p-const data(modify data)
include <stdio.h></stdio.h>
int main(void) {
int x = 5;
int y = 0;
const int *const ptr = &x; printf("%d\n", *ptr); *ptr = 7; ptr = &y; }
card shuffle-deal
include <stdio.h></stdio.h>
#include <stdlib.h>
#include <time.h></time.h></stdlib.h>
#define FACES 13
#define CARDS 52
// prototypes
void shuffle(int deck[][FACES]);
void deal(int deck[][FACES], const char *face[], const char *suit[]);
int main(void) {
// initialize deck array
int deck[SUITS][FACES] = {0};
srand(time(NULL)); // seed random-number generator
shuffle(deck); // shuffle the deck
// initialize suit array
const char *suit[SUITS] = {“Hearts”, “Diamonds”, “Clubs”, “Spades”};
// initialize face array
const char *face[FACES] = {“Ace”, “Deuce”, “Three”, “Four”, “Five”,
“Six”, “Seven”, “Eight”, “Nine”, “Ten”, “Jack”, “Queen”, “King”};
deal(deck, face, suit); // deal the deck
}
// shuffle cards in deck
void shuffle(int deck[][FACES]) {
// for each of the cards, choose slot of deck randomly
for (size_t card = 1; card <= CARDS; ++card) {
size_t row = 0; // row number
size_t column = 0; // column number
// choose new random location until unoccupied slot found do { row = rand() % SUITS; column = rand() % FACES; } while(deck[row][column] != 0); deck[row][column] = card; // place card number in chosen slot } }
// deal cards in deck
void deal(int deck[][FACES], const char *face[], const char *suit[]) {
// deal each of the cards
for (size_t card = 1; card <= CARDS; ++card) {
// loop through rows of deck
for (size_t row = 0; row < SUITS; ++row) {
// loop through columns of deck for current row
for (size_t column = 0; column < FACES; ++column) {
// if slot contains current card, display card
if (deck[row][column] == card) {
printf(“%5s of %-8s %c”, face[column], suit[row],
card % 4 == 0 ? ‘\n’ : ‘\0’); // 2-column format
}
}
}
}
}