Lecture4

Lecture4 - 211: Computer Architecture Fall 2011 Abhishek...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Abhishek Bhattacharjee Topic: s C Programming 211: Computer Architecture Fall 2011
Background image of page 1

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

View Full DocumentRight Arrow Icon
Rutgers University Abhishek Bhattacharjee 2 Anatomy of a C Program #include <stdio.h> #include <stdlib.h> char cMessage[] = "Hello\n"; /* Execution will start here */ int main (int argc, char **argv) { int i, count; count = atoi(argv[1]); for (i = 0; i < count; i++) { printf("Hello %d\n", i); } } include files declaration of global variables one or more function; each program starts execution at “main” declaration of local variables code implementing function comment
Background image of page 2
Rutgers University Abhishek Bhattacharjee 3 Pointers A pointer is just an address Can have variables of type pointer s Hold addresses as values s Used for indirection When declaring a pointer variable, need to declare the type of the data item the pointer will point to s int *p; /* p will point to a int data item */ Pointer operators s De-reference: * c *p gives the value stored at the address pointed to by p s c gives the address of the variable v
Background image of page 3

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

View Full DocumentRight Arrow Icon
Rutgers University Abhishek Bhattacharjee 4 Arrays Arrays are contiguous sequences of data items s All data items are of the same type s Declaration of an array of integers: “ int a[20]; s Access of an array item: “ a[15] Array index always start at 0 The C compiler and runtime system do not check array boundaries s The compiler will happily let you do the following: c int a[10]; a[11] = 5;
Background image of page 4
Rutgers University Abhishek Bhattacharjee 5 Given char word[10]; char *cptr; cptr = word; Each row in following table gives equivalent forms cptr word cptr + n word + n *cptr *word word[0] *(cptr + n) *(word + n) word[n]
Background image of page 5

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

View Full DocumentRight Arrow Icon
Rutgers University Abhishek Bhattacharjee 6 Pointer Arithmetic Be careful when you are computing addresses Address calculations with pointers are dependent on the size of the data the pointers are pointing to Examples: s int *i; …; i++; /* i = i + 4 */ s char *c; …; c++; /* c = c + 1 */ s double *d; …; d++; /* d = d + 8 */ Another example: double x[10]; double *y = x; *(y + 3) = 13; /* x[3] = 13 */
Background image of page 6
Rutgers University Abhishek Bhattacharjee 7 Strings: Arrays of Characters Allocate space for a string just like any other array: char outputString[16]; Each string should end with a ‘\0’ character Special syntax for initializing a string: char outputString[16] = "Result"; …which is the same as: outputString[0] = 'R'; outputString[1] = 'e'; ... outputString[6] = ‘\0’; The ‘\0’ allows functions like strlen() to work on arbitrary strings
Background image of page 7

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

View Full DocumentRight Arrow Icon
Rutgers University Abhishek Bhattacharjee 8 Defining a Struct We first need to define a new type for the compiler and tell it what our struct looks like. struct flightType {
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 47

Lecture4 - 211: Computer Architecture Fall 2011 Abhishek...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online