Docsity

Understanding Const and Pointers in C: Deep Dive into Constants, Storage Classes, and Func, Study notes of Operating Systems

Toggle sidebar
bg1
------
0x0d2C
May your references be bounded
May your signals all trap
Floats to ints round
All memory aligned
remember ...
++ adds one
and, NULL is for none
Arrays start with zero
Ox means hex
For octal, use zero
==means test
= will set
a dot if its not
use -> for a pointer
use them a lot
? : is confusing
there's no U in foobar
a.out is your program
and, char (*(*x())[])() is
"ode to C"
Monday, September 18, 2006
4:09 PM
Types Page 1
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Partial preview of the text

Download Understanding Const and Pointers in C: Deep Dive into Constants, Storage Classes, and Func and more Study notes Operating Systems in PDF only on Docsity!

0x0d2C May your references be bounded May your signals all trap Floats to ints round All memory aligned remember ... ++ adds one Non - zero is true and, NULL is for none Arrays start with zero Ox means hex For octal, use zero ==means test = will set a dot if its not use - > for a pointer use them a lot ? : is confusing there's no U in foobar a.out is your program and, char ((x())[])() is "ode to C" Monday, September 18, 2006 4:09 PM Docsity.com

a function returning a pointer to an array of pointers to functions returning char. and, char ((x())[])() is Docsity.com

If you have a pointer to a thing, you can write to it. The const declaration allows you to decide you aren't going to write to a pointer. const void foo(const int p) { printf("%d\n",p); } // I am not going to write to *p // but I can write to p Example Non-const implicitly casts to const. E.g., int *p; const int *q; q=p; // works const does not implicitly cast to non-const. E.g., const int *p; int *q; q=p; // compiler error const int *p; int *q q = (int *) p; // warning Rules for const const int *p; // *p is const int * const p; // p is const, *p remains mutable! const int * const p; // both are const (int a[5]; // sets a to type int * const) position of const in the declaration describes exactly what is constant and what remains mutable: To be nerdy about this: const int * const * const r; means that const Thursday, September 15, 2011 2:50 PM Docsity.com

r is const *r is const **r is const means that Note the pattern: the thing immediately after the const is the thing that is constant. More mundane use: const int i=1; Docsity.com

#include <string.h> char *strcpy(char *dest, const char *src); we are going to change *dest we are not going to change *src says that: How this is typically implemented: char ret = dest; while (src) dest++=src++; *dest='\0'; return ret; char *strcpy(register char *dest, register const char *src) { } register char dest: store this in a register, not a memory location while (src): until you get to a '\0' dest++=src++: copy from src to dest, then increment Explanation: Example: strcpy Thursday, September 15, 2011 3:01 PM Docsity.com

 Start by peeling back one layer at a time.  Use the meaning of *, and the fact that * and & cancel. There is an easy trick for reading complex C types. int ***c; means ***c is an int which means **c is of type int * which means c is of type int ** which means c is of type int *** (in each step, we apply & to both sides) Example: int ** (foo) (int x, int y); foo(5,7) is of type int ** *foo(5,7) is of type int * **foo(5,7) is of type int Example: Reading complex C types Monday, September 18, 2006 4:05 PM Docsity.com

So far, we've concentrated on reading manual pages. Today, we consider the larger issue of reading C header files (.h) Goal for today What can we write? Why do we write it? Two issues: Goal for today Tuesday, September 20, 2011 1:16 PM Docsity.com

Header files: declare things to be used by others (.h). Source files: define things that take up memory (.c). There are two kinds of C source files Definitions consume memory. Declarations do not. Reason for the split: one should only define something once, but one can declare it in every file that uses it. Kinds of C source files Tuesday, September 21, 2010 12:59 PM Docsity.com

inline int foo() { ... } should be in a header file, while int foo() { ... } should not be there. Reason: an inline function is a macro in disguise. It is a declaration. A subtle distinction: extern int foo; /* declaration / versus int foo; / definition */ Another subtle distinction Docsity.com

#include <stdio.h> void foo(const char *s) { printf("hello %s\n",s); } A typical source file foo.c void foo(const char *); or extern void foo(const char *); A header file foo.h that declares that: #include "foo.h" … foo("harry"); Any program that wishes to use foo: If a subroutine is undeclared at time of use, undeclared types default to int! Watch out: A source file Tuesday, September 21, 2010 1:01 PM Docsity.com

#define FOO 10 #define BAR(X) int X[FOO]; BAR(goo); // means int goo[10]; #ifdef FOO // this part is only interpreted if FOO is defined #else // this part is only interpreted if FOO is not defined #endif #define DEBUG 1 … … some awful debugging write… #ifdef DEBUG #endif /* DEBUG */ Example use of preprocessor directives: gcc - DDEBUG=1 foo.c # includes debugging gcc - UDEBUG foo.c # doesn't include debugging Then Preprocessor directives Tuesday, September 20, 2011 2:22 PM Docsity.com

const : the thing I am declaring can't be changed. static : the thing I am declaring is local to this file, but is a global variable extern : the thing I am declaring is defined elsewhere. The storage classes of variables Tuesday, September 21, 2010 12:51 PM Docsity.com

extern int foo(); // someone else defined foo extern double timer; // someone else defined timer extern allows one to specify that something is available elsewhere. Used to give the type of something that will be found elsewhere (in another file) extern Tuesday, September 21, 2010 12:55 PM Docsity.com

Function names are pointers to their addresses. Can declare a "pointer to function" that describes where a function is. If bar is a function, Then bar; // computes where the function is, and discards. But bar(); // calls bar. Pointers to function void bar() { printf("hi"); } void pan(int i) { printf("%d\n",i); } void (foo)(); // foo is a pointer to a function of zero args foo=bar; // no &: bar is already a pointer! foo(); // exactly equivalent to bar() foo = (void ()()) pan; // ignore the arguments! ((void (*)(int))foo)(1); // call correctly via another cast! ^^^^^^^^^^ think of thing as a function of one argument: called a method cast. foo is a function of 0 arguments. override that (and waste space!) Examples Note: if a function is called with extra arguments, they are in general ignored. If a function is called with fewer arguments, unpredictable stack data get interpreted as arguments (whether they are valid or not!) Pointers to function Wednesday, September 14, 2005 4:33 PM Docsity.com