Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic memory allocation and Intraprogram Communication.

Similar presentations


Presentation on theme: "Dynamic memory allocation and Intraprogram Communication."— Presentation transcript:

1 Dynamic memory allocation and Intraprogram Communication

2 Pointers as 1D array #include int main(void) { char *array; int len = 20,i=0; array = (char *)calloc(len+1, 1); gets(array); printf("%s\n", array); while(array[i]) printf("%c", array[i++]); free((void *)array); printf("\n%s", array); return 0; }

3 Pointers as 2D array #include int main(void) { int **array; int row=5, col=3; int i,j; array = (int **)malloc(row*sizeof(int *)); for(i=0;i<row;i++) array[i] = (int *)malloc(col * sizeof(int)); for(i=0;i<row;i++) { for(j=0;j<col;j++) { scanf("%d", &array[i][j]); } for(i=0;i<row;i++) { for(j=0;j<col;j++) { printf("%d ", array[i][j]); } printf("\n"); } for(i=0;i<row;i++) { free(array[i]); } free(array); return 0; }

4 Study Reference C By Discovery 4.10 and 11.5 Teach yourself C Chapter 5,6

5 #include int increment(); int main(void) { printf("%d\n", increment()); return 0; } int increment() { int number = 0; return ++number; }

6 Output is ▫1 No storage specifier for number so it’s automatic, so number is created and initialized each time increment() is called. The value of number will be zero each time increment() starts executing.

7 Static variables A static variable exits the whole time the program is executing. To declare a static variable ▫static int counter; Without explicit initialization, a static variable will be given the default initial value. [0] static int counter = 1;

8 #include int increment(); int main(void) { printf("%d\n", increment()); return 0; } int increment() { static int number = 0; return ++number; }

9 Output is ▫1 ▫2 number is just initialized once when increment() is called the first time and holds its value between classes to increment().

10 Scope of an identifier The scope of an identifier determines which part of the program will know about the identifier and be able to use it. Local and global variables

11 Local variables A local variable is local to a block which may be a function block or a compound statement block. It’s declared inside the braces for the block and only be referenced inside its block. A local variable’s scope is from its point of declaration to the end of the block in which it was declared.

12 Example of local variables #include int main(void) { int a = 10; { int b = 8; } printf("%d\n", b); return 0; }

13 An error in the previous example: ▫error C2065: 'b' : undeclared identifier

14 Another example #include int addit(); int main(void) { int counter; for(counter = 1; counter < 5; counter++) { int i; i = counter; i = i + 5; addit(); } printf("i was %d; addit() returned %d.\n", i, addit()); return 0; } int addit(void) { int sum = 0; sum += counter; return sum; }

15 Another example #include int addit(); int main(void) { int counter; for(counter = 1; counter < 5; counter++) { int i; i = counter; i = i + 5; addit(); } printf("i was %d; addit() returned %d.\n", i, addit()); //first error: error C2065: 'i' : undeclared identifier return 0; } int addit(void) { int sum = 0; sum += counter; //second error: error C2065: 'counter' : undeclared identifier return sum; }

16 Global variables A global variable is declared outside all function blocks. It’s possible to access a global variable in more than one functions. If a global variable is declared before all the functions in a source file, it can be referenced by all the functions in that file. The scope of a global variable declared either before the functions in a file or between two functions in a file is from its point of declaration to the end of the file.

17

18 A global variable is in existence during the full execution time of the program. It has a default value if no initializer is given.


Download ppt "Dynamic memory allocation and Intraprogram Communication."

Similar presentations


Ads by Google