[PDF] [PDF] Pointers and Storage Classes - Cornell CS

8 fév 1999 · Storage Class auto Variables within functions or blocks default to auto- matic, but storage class can be given explicitly: auto int a, b, c; Memory 



Previous PDF Next PDF





[PDF] C - Storage Classes - Tutorialspoint

A storage class defines the scope visibility and life-time of variables and/or functions within a C Program These specifiers precede the type that they modify



[PDF] Storage Classes in C++ - Tutorialspoint

A storage class defines the scope visibility and life-time of variables and/or functions within a C++ Program These specifiers precede the type that they modify



[PDF] Q) Storage Classes in C - Khateeb Classes

c) All such variables are local to the loop in which they are declared d) If no storage class is specified for any variable then its default storage is always automatic



[PDF] Function, Scope Rules and Storage class - IIT Guwahati

Storage Class for variable • Scope Rules C standard library has a wide variety of functions In C++, Function can not be defined inside another function : But 



[PDF] Storage Classes - NIELIT

Programming and Problem Solving through C Language O Level / A Level Chapter - 7 : Storage Classes Storage Class A storage class defined the scope,  



Storage Classes and Scope

9, 2012 **/ #define MAXVAL 1000 int k = 0; void setup() { Serial begin(9600); } Arduino C recognizes four storage classes: auto, register, static, and extern



[PDF] C Storage Classes, Scope and Memory 1 - Tenouk

of the variable by its name In C program, there are four storage classes: automatic, register, external, and static □ Keep in 



[PDF] Pointers and Storage Classes - Cornell CS

8 fév 1999 · Storage Class auto Variables within functions or blocks default to auto- matic, but storage class can be given explicitly: auto int a, b, c; Memory 



[PDF] C - Storage Classes - CMP College

C - Storage Classes A storage class defines the scope (visibility) and life-time of variables and/or functions within a C Program They precede the type that they 

[PDF] storage classes in c language

[PDF] storage classes in c pdf

[PDF] storage of hand sanitizer

[PDF] storage of pointer in c

[PDF] stored procedure in sql server in depth

[PDF] stored procedure sql w3

[PDF] stories with dialogue conversation pdf

[PDF] story elements activities

[PDF] story elements worksheet pdf

[PDF] story fun for flyers pdf

[PDF] straight line equation calculator

[PDF] strands of art

[PDF] strasbourg france map europe

[PDF] strategies for reading math textbooks

[PDF] strategies of life cycle sustainment

Pointers and Storage Classes

COM S 113

February 8, 1999

Announcements

Assignment 2 can be turned in Tuesday; oce hours

today 2:00{3:30 in Upson 5162

Assignment 3 (short!) available, due Friday

Read Ch. 8 inC by Dissectionor K&R 5.1{5.9

1

Pointers andconst

const int a; /* a is a const int */ const int *b; /* b is a pointer to a const int */ int * const c; /* c is a const pointer to int */ const int * const d; /* d is a const pointer to a const int */ 2

Pointers tovoid

One pointer can be assigned to another only if both have same type or one is pointer tovoid void *is used as a generic pointer type malloc()returns a pointer tovoid, so we can assign the result to any pointer type without a cast 3

Examples of Pointers tovoid

int *p; float *q; void *v; /* Legal */ /* Illegal */ p = 0; p = 1; p = (int *) 1; v = 1; p = v = q; p = q; p = (int *) q; p = malloc(4 * sizeof(int)); 4

Example of Call-by-Reference

void swap(int *p, int *q) { int tmp = *p; *p = *q; *q = tmp; int main() { int a=3, b=7; swap(&a, &b); return 0; 5

Storage Classes

Every variable and function has atypeand astorage

class Four storage classes:auto,extern,register, andstatic 6

Storage Classauto

Variables within functions or blocks default to auto- matic, but storage class can be given explicitly: auto int a, b, c;

Memory allocated upon entering block, released at

exit, so values aren't kept between invocations 7

Storage Classstatic(rst use)

When applied to variables dened within a block, local variables retain their values between invocations void printletter(void) { static int parity; /* initially 0 */ putchar(parity ? 'A' : 'B'); parity = (parity + 1) % 2; 8

UsingstaticVariables for Debugging

static int cnt = 1; printf("On %dth iteration, d has value %d.\n", cnt, d); 9

Storage Classextern

Variables declared outside functions, and all functions themselves, have external storage class externtells the compiler to look for a variable else- where, either in the same le or in another le 10

Example of External Variables

#include int a = 1, b = 2, c = 3; /* global variables */ int f(void); /* function prototype */ int main() { int b, c; a = b = c = 4; printf("%3d\n", f()); printf("%3d%3d%3d\n", a, b, c); } 11

Useful Example ofextern

In lefile1.c:

int a = 1, b = 2, c = 3; int f(void); int main() { printf("%3d\n%3d%3d%3d\n", f(), a, b, c); }

In lefile2.c:

int f(void) { extern int a; int b, c; a = b = c = 4; return a + b + c; } 12

Storage Classregister

Advises (but doesn't require) compiler to store value in CPU register rather than in memory

Defaults toautoif compiler decides otherwise

Purpose is to speed program execution by keepingvery frequentlyaccessed variables (loop counters) imme- diately available 13

Storage Classregister(continued)

Was important when compilers weren't as smart about register allocation; many compilers now ignoreregister Becauseregistervariables not necessarily stored in memory, can't take the address of such a variable register int i; /* could be written: register i */ for (i = 0; i < LIMIT; i++) ... /* illegal to refer to &i */ 14

Storage Classstatic(second use)

When applied to external declarations (of functions or variables), scope is restricted to current le Functions in other les can't access externalstatic variables, even if they attempt to use theexternstor- age class keyword Good way to implement information hiding in C, like private variables and methods in Java, but limited 15

Example of ExternalstaticVariables

Consider implementation of a stack with operations push(i),pop(),empty(), andfull() We'll implement with integer arrays, using variable nextto point to next free element, both declared static, in lestack.c 16

Example of ExternalstaticVariables (continued)

#include "stack.h" static int s[MAX_SIZE], next = 0; void push(int i) { s[next++] = i; } int pop(void) { return s[--next]; } int empty(void) { return next == 0; } int full(void) { return next == MAX_SIZE; } 17

Review of Memory Allocation

#include int main() { int *a; a = malloc(sizeof(int)); *a = 3; printf("a is an int pointer with value %p\n", a); printf("a points to an int with value %d\n", *a); free(a); } 18

Allocating One-Dimensional Arrays

#include int main() { int *a, i, n = 100; a = malloc(n * sizeof(int)); for (i = 0; i < n; i++) a[i] = i; free(a); 19

Traversing an Array with Pointers

#define N 100 int sumarray(const int a[N]) { int *p, sum = 0; for (p = a; p < &a[N]; p++) sum += *p; return sum; 20quotesdbs_dbs20.pdfusesText_26