#include <stdio.h> void alter(int *, int *) main() { int x,y; printf("Enter two integers: "); scanf("%d", &x); scanf("%d", &y); printf("x = %d x = %d\n",x,y); alter(&x,&y); printf("x = %d x = %d\n",x,y); } void alter(int *px, int *py) { *px = *px + *py; *py = *px * *py; }
#include <stdio.h> void swap(int *, int *) main() { int x,y; printf("Enter two integers: "); scanf("%d", &x); scanf("%d", &y); printf("x = %d x = %d\n",x,y); swap(&x,&y); printf("x = %d x = %d\n",x,y); } void swap(int *px, int *py) { int temp; temp = *px; *px = *py; *py = temp; }
a = x b = y a = p b = q
10 10 5 5 8 8
switch++; or ++switch; or switch += 1; or switch = switch + 1;
char name[10]; char *nameptr = name;
30 25 20 15 10 5 5 10 15 20 25 30 5 5 5 5 5 5 5 6 7 8 9 10
void reverse(int *one, int *two, int size) { int i, k; for (i = 0 ; i < size ; i++) { k = size - i - 1; *(one+i) = *(two+k); } }
int count(char *str, char ch) { int i; int thecount = 0; for (i = 0 ; *(str+i) ; i++) if (*(str+i) == ch) thecount++; return thecount; }
This code removes every occurrence of ch from the string str. Trace it very carefully with an array that contains the character a couple of times, and you will see. You could use 'b' as the second parm with the following array:
-------------------------------------- | g | x | b | p | m | b | t | z | \0 | --------------------------------------
int mystrlen(char *line) { int len; // stop loop at position of null byte, which is the length // no body needed for this loop for (len = 0 ; *(line+len) != '\0' ; len++); return len; }
// instead of adding the loop counter to the pointers, we increment // the pointers void mystrcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; src++; dest++; } *dest = '\0'; }
void mystrcat(char *one, char *two) { int i; // get length of one int len = mystrlen(one); // start at beginning of two and end of one // copy everything from two into one for (i = 0 ; *(two+i) != '\0' ; i++, len++) *(one+len) = *(two+i); // add null byte to the end of one *(one+len) = '\0'; }
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page
© Copyright Emmi Schatz 2014