Professional Documents
Culture Documents
8 93 SE2S03, 2006 — WOLFRAM KAHL, Computing and Software, McMaster University 3.21 106
SE2S03, 2006 — WOLFRAM KAHL, Computing and Software, McMaster University 3.17 102 SE2S03, 2006 — WOLFRAM KAHL, Computing and Software, McMaster University 3.31 116
• call by reference
yPtr = &y;
• (certain) subarrays
printf ( "%d\n", *yPtr );
• recursive datastructures
*yPtr = 7;
• graph-like datastructures printf ( "%d\n", y);
• higher-order functions The dereferencing operator, applied to a pointer value p, returns the variable p
points to, i.e.,
But: Pointers must be used very carefully! • in an expression: the contents of the memory address p
• to the left of an assignment: the variable at memory address p
SE2S03, 2006 — WOLFRAM KAHL, Computing and Software, McMaster University 3.36 121 SE2S03, 2006 — WOLFRAM KAHL, Computing and Software, McMaster University 3.47 132
SE2S03, 2006 — WOLFRAM KAHL, Computing and Software, McMaster University 3.55 140 SE2S03, 2006 — WOLFRAM KAHL, Computing and Software, McMaster University 3.57 142
General rules:
• a byte has 8 bits
• in C, characters are 8-bit integral values
• on a n-bit architecture, int and pointers occupy n bits, i.e., n/ 8 bytes
• (double variables occupy twice as much space as floats)
• long occupies not less space than int
SE2S03, 2006 — WOLFRAM KAHL, Computing and Software, McMaster University 3.60 145 SE2S03, 2006 — WOLFRAM KAHL, Computing and Software, McMaster University 3.74 159
• constant: read-only one may calculate the pointer difference ptrB − ptr , which will be n.
• any pointer value can be assigned to ptr char * reverse(int length, char * string) {
• ptr can be assigned to any pointer variable char result[length+1];
int i,j;
• void pointers are used as “pointers to anything” — faking polymorphism for ( i = length−1, j=0; i ≥ 0; i−−, j++ ) result[j] = string[i];
result[length] = ’\0’;
• void pointers cannot be dereferenced — a cast is necessary first
return result;
#include <stdio.h> }
int main() {
char s[] = "Hello World!"; int main() {
void * p = s; char msg1[] = "Hello world!";
void * q = p + 1; char * msg2;
printf ("%d “%s”\n", sizeof(void), (char *)q); msg2 = reverse(strlen(msg1), msg1);
return 0; printf ("Reversing finished!\n");
} printf ("msg2=‘%s’\n", msg2);
return 0;
}
SE2S03, 2006 — WOLFRAM KAHL, Computing and Software, McMaster University 3.82 167 SE2S03, 2006 — WOLFRAM KAHL, Computing and Software, McMaster University 3.91 176
int main() {
...
sort(b, SIZE, leq);
...
}
SE2S03, 2006 — WOLFRAM KAHL, Computing and Software, McMaster University 3.103 188 SE2S03, 2006 — WOLFRAM KAHL, Computing and Software, McMaster University 3.106 192
SE2S03, 2006 — WOLFRAM KAHL, Computing and Software, McMaster University 3.109 195 SE2S03, 2006 — WOLFRAM KAHL, Computing and Software, McMaster University 3.120 206
• printf and scanf Setting endptr to first invalid character, and setting errno:
long int strtol(const char *nptr , char **endptr , int base);
long long int strtoll(const char *nptr , char **endptr , int base);
unsigned long int strtoul(const char *nptr , char **endptr , int base);
unsigned long long int strtoull(const char *nptr , char **endptr , int base);
errno = 0;
int strcmp(const char *s1, const char *s2);
d = strtod( argv[1], &restPtr );
int strncmp(const char *s1, const char *s2, size_t n);
if (errno) perror ("ERROR in strtod");
printf ( "Conversion of the string \"%s\"\n", argv[1] ); int strcasecmp(const char *s1, const char *s2);
printf ( " * produces the double value “%.2g”\n", d); int strncasecmp(const char *s1, const char *s2, size_t n);
printf ( " * and leaves the remainder string \"%s\"\n", restPtr );
int strcoll(const char *s1, const char *s2); /* uses locale */
return 0;
}
SE2S03, 2006 — WOLFRAM KAHL, Computing and Software, McMaster University 3.126 212 SE2S03, 2006 — WOLFRAM KAHL, Computing and Software, McMaster University 3.132 218
int putchar (int c); /* returns c , or EOF on error */ size_t strspn(const char *s, const char *accept);
int puts(const char *s); /* returns EOF on error */ size_t strcspn(const char *s, const char *reject);
int sprintf ( char *str , const char *format, ...); char *strpbrk(const char *s, const char *accept);
int snprintf (char *str , size_t size, const char *format, ...);
char *strstr (const char *haystack, const char *needle);
From the man page: The function snprintf does not write more than size bytes
(including the trailing ’\0’). If the output was truncated due to this limit then the char *strtok( char *s, const char *delim); /* Don’t use! */
return value is the number of characters (not including the trailing ’\0’) which char *strtok_r(char *s, const char *delim, char **ptrptr ); /* Don’t use! */
would have been written to the final string if enough space had been available.
SE2S03, 2006 — WOLFRAM KAHL, Computing and Software, McMaster University 3.133 219