Daytab pointer to array of int char daytab13 daytab

Info icon This preview shows pages 10–18. Sign up to view the full content.

View Full Document Right Arrow Icon
/* daytab: pointer to array of int */ char *daytab[13]; /* daytab: array of pointer to int */ int *(*x[10])(void); /* x: array of pointer to function returning pointer to int */ char (*(*x())[])(); /* x: function returning pointer to array of pointer to function returning char */ CSC180 Fall 2008, University of Toronto 9
Image of page 10

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
char (*(*x[3])())[5]; /* x: array of pointer to function returning pointer to array of char */ Read C declarations “inside out” Use cdecl to encode and decode C declarations CSC180 Fall 2008, University of Toronto 10
Image of page 11
Pointers to arrays What is the difference between &a and a ? Type a is a pointer to int , but type &a is a pointer to an array of int s. int a[10]; int *p; int (*pa)[10]; p = a; /* OK */ p = &a[0]; /* OK */ p = &a; /* incompatible pointer type */ pa = &a; /* OK */ pa = a; /* incompatible pointer type */ Example: CSC180 Fall 2008, University of Toronto 11
Image of page 12

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
int (*pa)[10]; /* pointer to array of 10 ints */ int b[5][10] = {{0}}; /* set the first column to 1 */ for (pa = b; pa < &(b[5]); pa++) (*pa)[0] = 1; CSC180 Fall 2008, University of Toronto 12
Image of page 13
Static functions Function names are visible to the entire program. A static function is visible only within the file where it is defined. Example: /* foo_bar_baz.c */ void foo(void) { /* do sth */ } extern void bar(void) { CSC180 Fall 2008, University of Toronto 13
Image of page 14

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
/* do sth */ } static void baz(void) { /* do sth */ } /* main.c */ int main() { foo(); bar(); baz(); /* WRONG */ return 0; } Compile and link: [[email protected] ~]$ gcc foo_bar_baz.c main.c CSC180 Fall 2008, University of Toronto 14
Image of page 15
/tmp/cc8qrDCg.o(.text+0x27): In function ‘main’: : undefined reference to ‘baz’ CSC180 Fall 2008, University of Toronto 15
Image of page 16

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
More on scope We have seen file scope and block scope . The remaining two scopes are function scope and prototype scope . Function scope: entire function body. A statement label has function scope: /* determine whether two arrays a and b have an element in common */ int common(int a[], int n, int b[], int m) { int i, j; CSC180 Fall 2008, University of Toronto 16
Image of page 17
for (i = 0; i < n; i++) for (j = 0; j < m; j++) if (a[i] == b[j]) goto found; return 0; found: return 1; } Prototype scope: from point of declaration to end of prototype. Example: int bar(int foo, int bar); int foo(void); CSC180 Fall 2008, University of Toronto 17
Image of page 18
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern