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 <stdio.h> #include<stdlib.h> int main(void) { char *array; int len = 20,i=0; array = (char *)calloc(len+1, 1); fgets(array,21,stdin); 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 <stdio.h> #include<stdlib.h>
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(j=0;j<col;j++) scanf("%d", &array[i][j]); } printf("%d ", array[i][j]); printf("\n"); free(array[i]); free(array); return 0;

4 Example Creating a 2D array that stores 5 different words of size 5.
int main() { int i; char **two_d_array; two_d_array = (char **) malloc (5 * sizeof(char *) ); for(i = 0; i < 5; i++) { printf("Enter Word %d :", i+1); two_d_array[i] = (char *) malloc (5 * sizeof(char) ); scanf("%s", two_d_array[i]); printf("\n"); } printf("Printing The Words...\n"); for(i = 0; i < 5; i++) printf("Word %d :%s\n", i+1, two_d_array[i]); return 0;

5 #include <stdio.h> 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 <stdio.h> 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 <stdio.h> 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 <stdio.h> 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 <stdio.h> 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 #include<stdio.h>
void output(); int main() { printf("%d.\n",x); //error output(); return 0; } int x=5; void output() printf("In the output, %d.\n",x);

18 A global variable is in existence during the full execution time of the program.

19 Communication across files
Functions in separate files share the same variable. Define a global variable in one file, and other files can declare (can not define) this same variable and give it the storage class extern. Note you can only define a variable once and for all other places, you can declare.

20 Driver.c #include <stdio.h> int x = 5; extern void output();
extern void incr(); int main(void) { output(); incr(); return 0; }

21 Output.c #include<stdio.h> extern int x; void output() {
printf("The value of x is %d.\n", x); }

22 Incr.c extern int x; void incr() { x++; }

23 The value of x is 5. The value of x is 6.


Download ppt "Dynamic memory allocation and Intraprogram Communication"

Similar presentations


Ads by Google