Lecture4 - 211 Computer Architecture Fall 2011 Abhishek Bhattacharjee Topic C Programming Anatomy of a C Program#include <stdio.h>#include <stdlib.h>

Lecture4 - 211 Computer Architecture Fall 2011 Abhishek...

This preview shows page 1 - 10 out of 47 pages.

Abhishek Bhattacharjee Topic: square6 C Programming 211: Computer Architecture Fall 2011
Image of page 1
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
Image of page 2
Rutgers University Abhishek Bhattacharjee 3 Pointers A pointer is just an address Can have variables of type pointer square6 Hold addresses as values square6 Used for indirection When declaring a pointer variable, need to declare the type of the data item the pointer will point to square6 int *p; /* p will point to a int data item */ Pointer operators square6 De-reference: * circle6 *p gives the value stored at the address pointed to by p square6 Address: & circle6 &v gives the address of the variable v
Image of page 3
Rutgers University Abhishek Bhattacharjee 4 Arrays Arrays are contiguous sequences of data items square6 All data items are of the same type square6 Declaration of an array of integers: “ int a[20]; square6 Access of an array item: “ a[15] Array index always start at 0 The C compiler and runtime system do not check array boundaries square6 The compiler will happily let you do the following: circle6 int a[10]; a[11] = 5;
Image of page 4
Rutgers University Abhishek Bhattacharjee 5 Arrays & Pointers Given char word[10]; char *cptr; cptr = word; Each row in following table gives equivalent forms cptr word &word[0] cptr + n word + n &word[n] *cptr *word word[0] *(cptr + n) *(word + n) word[n]
Image of page 5
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: square6 int *i; …; i++; /* i = i + 4 */ square6 char *c; …; c++; /* c = c + 1 */ square6 double *d; …; d++; /* d = d + 8 */ Another example: double x[10]; double *y = x; *(y + 3) = 13; /* x[3] = 13 */
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
Image of page 7
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 { char flightNum[7]; /* max 6 characters */ int altitude; /* in meters */ int longitude; /* in tenths of degrees */ int latitude; /* in tenths of degrees */ int heading; /* in tenths of degrees */ double airSpeed; /* in km/hr */ }; This tells the compiler how big our struct is and how the different data items are laid out in memory square6 But it does not allocate any memory square6 Memory is only allocated when a variable is declared
Image of page 8
Rutgers University
Image of page 9
Image of page 10

You've reached the end of your free preview.

Want to read all 47 pages?

  • Fall '08
  • Staff
  • Rutgers University, Garbage collection, Abhishek Bhattacharjee

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes