Download presentation
Presentation is loading. Please wait.
1
Surviving C and PostgreSQL CS186 Supplemental Session 9/13/04 Matt Denny, Paul Huang, Murali Rangan (Originally prepared by Shariq Rizvi, Wei Xu, Shawn Jeffery)
2
Outline A Review of C PostgreSQL Basics Tour of Assignment 1 2Q
3
A Review of C Review Pitfalls of C Programming for Java Programmers Pointers and Arrays Strings Segmentation Faults For more information, consult “ The C Programming Language” by Kernighan and Ritchie or tutorial on web site
4
Pointer = variable containing address of another variable float f; /* data variable */ float *f_addr; /* pointer variable */ f_addr = &f; /* & = address operator */ ?? f f_addr 43004304 ? any float any address ?4300 f f_addr 43004304
5
*f_addr = 3.2; /* indirection operator */ float g=*f_addr; /* indirection:g is now 3.2 */ f = 1.3; f f_addr 43004304 3.24300 f f_addr 43004304 1.34300
6
int month[12]; /* month is a pointer to base address 430: system allocates 12*sizeof(int) bytes at 430 */ month[3] = 7; /* integer at address (430+3*sizeof(int)) is now 7 */ ptr = month + 2; /* ptr points to month[2], i.e. ptr =(430+2 * sizeof(int)) = 438 */ ptr[5] = 12; /* int at address (434+5*sizeof(int)) is now 12; same as month[7] */ Arrays and Pointers Now, month[6], *(month+6), (month+4)[2], ptr[4], *(ptr+4) are all the same integer variable.
7
Strings #include main() { char msg[10]; /* array of 10 chars */ char *p; /* pointer to a char */ char msg2[]=“Hello”; /* msg2 = ‘H’’e’’l’’l’’o’’\0’ */ msg = “Bonjour”; /* ERROR. msg has a const address. Think of space allocation*/ p = “Bonjour”; /* address of “Bonjour” goes into p */ p = msg; /* OK */ p[0] = ‘H’, p[1] = ‘i’,p[2]=‘\0’; /* *p and msg are now “Hi” Warning: be careful if you have space allocated for p!!*/ }
8
Segmentation Fault? You reference memory that the OS doesn’t want you to What to check? Dereferencing a null pointer 99% –Output or trace with a debugger –Check array bounds
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.