6CS005 Lecture 2 - High Performance Computing - PDF

Document Details

ModestCobalt

Uploaded by ModestCobalt

University of Wolverhampton

Tags

pointers C programming dynamic memory allocation file handling

Summary

This lecture covers pointers, dynamic memory allocation, and file handling in C. It introduces fundamental concepts of pointers and memory management in a programming context.

Full Transcript

6CS005 High Performance Computing Lecture 2 Pointers, Dynamic Memory Allocation, and File Handling in C Topics Covered ∙ Pointer Fundamentals ∙ Initializing Pointers ∙ Using Pointers ∙ Pointers as Function Parameters ∙ Pointer Arithmetic ∙ Pointers and Arrays ∙ Using Pointers to Ac...

6CS005 High Performance Computing Lecture 2 Pointers, Dynamic Memory Allocation, and File Handling in C Topics Covered ∙ Pointer Fundamentals ∙ Initializing Pointers ∙ Using Pointers ∙ Pointers as Function Parameters ∙ Pointer Arithmetic ∙ Pointers and Arrays ∙ Using Pointers to Access Array Elements ∙ Dynamic memory allocation ∙ File Handling Pointer Fundamentals When a variable is defined the compiler (linker/loader actually) 00000000 allocates a real memory 00000000 address for the variable. x – int x; will allocate 4 bytes in the 00000000 main memory, which will 00000011 be used to store an integer value. When a value is assigned to a variable, the value is actually placed to the memory that was allocated. – x=3; will store integer 3 in the 4 bytes of memory. Pointers 00000000 When the value of a variable is used, the 00000000 contents in the memory are used. x – y=x; will read the contents in the 4 00000000 bytes of memory, and then 00000011 assign it to variable y. &x can get the address of x. (referencing operator &) The address can be passed to a function: – scanf("%d", &x); y The address can also be stored in a variable …… Pointers To declare a pointer variable – type * pointername; For example: – int * p1; p1 is a variable that tends to point to an integer, (or p1 is a int pointer) – char *p2; – unsigned int * p3; p1 = &x; scanf("%d", p1); p2 = &x; Initializing Pointers Like other variables, always initialize pointers before using them!!! For example: int main(){ int x; int *p; scanf("%d",p); Don’t p = &x; scanf("%d",p); } Using Pointers You can use pointers to access the values of other variables, i.e. the contents of the memory for other variables. To do this, use the * operator (dereferencing operator). – Depending on different context, * has different meanings. For example: int n, m=3, *p; p=&m; n=*p; printf("%d\n", n); printf("%d\n",*p); An Example int m=3, n=100, *p; p=&m; printf("m is %d\n",*p); m++; printf("now m is %d\n",*p); p=&n; printf("n is %d\n",*p); *p=500; printf("now n is %d\n", n); Pointers as Function Parameters Sometimes, you want a function to assign a value to a variable. – e.g. scanf() E.g. you want a function that computes the minimum AND maximum numbers in 2 integers. Method 1, use two global variables. – In the function, assign the minimum and maximum numbers to the two global variables. – When the function returns, the calling function can read the minimum and maximum numbers from the two global variables. This is bad because the function is not reusable. Pointers as Function Parameters int main() Instead, we use the following function { int x,y; void min_max(int a, int b, int small,big; int *min, int *max){ printf("Two integers: "); if(a>b){ *max=a; scanf("%d %d", &x, &y); *min=b; min_max(x,y,&small,&big); } printf("%d

Use Quizgecko on...
Browser
Browser