L9_Pointers1 PDF - Programmeringsteknik DT143G Lecture Notes

Document Details

SaintlySphene5524

Uploaded by SaintlySphene5524

Örebro University

2024

Programmeringsteknik

Pascal Rebreyend

Tags

pointers dynamic arrays C programming computer science

Summary

This document is a lecture from Programmeringsteknik DT143G (C programming) from Örebro University, given on 3-12-2024. It covers pointers, memory allocation, functions and dynamic arrays. The lecture notes provide explanations and examples related to various programming concepts used in C and dynamic array implementations.

Full Transcript

Introduction Programmeringsteknik DT143G Lecture 9: Pointers and Dynamic Arrays Pascal Rebreyend 3-12-2024 Pascal Rebreyend Programmeringsteknik DT143G Introduction Today’s contents Pointers - definition How to use...

Introduction Programmeringsteknik DT143G Lecture 9: Pointers and Dynamic Arrays Pascal Rebreyend 3-12-2024 Pascal Rebreyend Programmeringsteknik DT143G Introduction Today’s contents Pointers - definition How to use them with functions Dynamic Arrays Memory Allocations Pascal Rebreyend Programmeringsteknik DT143G Introduction Why pointers? The main particularity of C! Because allows use to have dynamic arrays! Because very convenient and efficient when dealing with irregular data-structures In fact, they exists in many other languages but they are hidden or named with different words! Needed to understand many algorithms and data-structures (later courses) Pascal Rebreyend Programmeringsteknik DT143G Introduction Definition and usage A pointer (pekare) is a variable that contains an address (to the memory) Declaration: type *name; Behaves like other classical types. &a : address of the variable a *p : value at the address p Pascal Rebreyend Programmeringsteknik DT143G Introduction Example 1: modification via a function #include void swap(int *p1,int *p2) { int m; m=*p2; *p2=*p1; *p1=m; } int main() { int a=3,b=10; int *p1,*p2; printf("a= %d b= %d\n",a,b); swap(&a,&b); printf("a= %d b= %d\n",a,b); // ANOTHER WAY p1=&a; p2=&b; swap(p1,p2); printf("a= %d b= %d\n",a,b); return 0; } Pascal Rebreyend Programmeringsteknik DT143G Introduction Comments Explanation why we use the & in the scanf! Useful if functions may need to modify several variables! BUT we need to have a variable to have a dedicated space in the memory to hold the value! In fact, pointers are numbers. If they point to a wrong location: either unexpected values written or read, either segmentation fault (try to access prohibited memory location). SIDE EFFECTS! Pascal Rebreyend Programmeringsteknik DT143G Introduction Memory Allocation malloc(size t s): allocates space in memory (s bytes) and return a pointer to it calloc(size t nb,size t size): allocates space in memory (and array of nb items of s bytes each ) and return a pointer to it free(void *ptr): frees the memory pointed by ptr sizeof(type): returns the size of the corresponding type. Pascal Rebreyend Programmeringsteknik DT143G Introduction Example: Dynamic arrays #include #include void fill_array(double *d,int n) { int i; for (i=0;i

Use Quizgecko on...
Browser
Browser