SlideServe
Menu
Préférences
© SlideServe · v2026

Functions and Pointers

tasha-blackwell avatar
15 diapositives · 281 Views · Oct 31, 2014

Functions and Pointers. Dr. Sajib Datta CSE@UTA Oct 7, 2014. Passing multi-dimensional array. #include < stdio.h > void print( int [][3], int , int ); int main(void) { int a[2][3] = {{1,2,3}, {4,5,6}}; printf ("%d
", sizeof (a)); print(a,2,3); return 0; }

Functions and Pointers - slide 1
01 / 15Functions and Pointers
1 / 17

Télécharger la présentation

Functions and Pointers
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Functions and Pointers Dr. SajibDatta CSE@UTA Oct 7, 2014

  2. Passing multi-dimensional array #include <stdio.h> void print(int [][3], int , int); int main(void) { int a[2][3] = {{1,2,3}, {4,5,6}}; printf("%d\n", sizeof(a)); print(a,2,3); return 0; } void print(int b[][3], int r, int c) { inti,j; for(i=0;i<r;i++) { for(j=0; j<c;j++) { printf("%d", b[i][j]); } printf("\n"); } }

  3. Pointers

  4. Addresses in Memory • Everything in memory has an address. C allows us to obtain the address that a variable is stored at. • scanf() is an example using the address of a variable • scanf("%d", &year);

  5. Addresses in Memory • Preceding a variable name by an ampersand, known as the address operator, will return its address: • #include <stdio.h> • int main(void) • { • int x; • /* notice the format specifier in printf() for an • address is %p */ • printf("The address for the memory allocated to x is %p\n", &x); • return 0; • }

  6. hexadecimal (or called base-16) 0123456789ABCDEF • The address for the memory allocated to x is 0012FF60 in the previous example. • For 32 bits system the 0012FF60 hexadecimal will be converted to 32 bits binary (one hex character represented by 4 bits).

  7. Pointer • A pointer is a variable whose value is a memory address. • Note the type of a pointer indicates the type of variable which it points to. • E.g., int * (called pointer-to-int type) should be initialized to point to a variable of type int. • Similarly, we have char*, float *, …… • Given a pointer variable, assignment can be done by using: • int * ptr = &pooh; /*assigns pooh’s address to ptr, we say ptr points to pooh*/ • ptr = &bah; /*make ptr point to some other variables*/

  8. #include<stdio.h> • int main(void) • { • int num = 3, num1 = 5; • int* numptr; /* numptr is a pointer */ • printf("content of num is %d\n", num); • printf("address of num is %p\n", &num); • printf("address of num1 is %p\n", &num1); • numptr = &num; /* initialize numptr with the address of num */ • printf("content of numptr is %p\n", numptr); • numptr = &num1; • printf("content of numptr is %p\n", numptr); • return 0; • }

  9. Output: content of num is 3 address of num is 0012FF60 address of num1 is 0012FF54 content of numptr is 0012FF60 content of numptr is 0012FF54 Press any key to continue . . . • #include<stdio.h> • int main(void) • { • int num = 3, num1 = 5; • int* numptr; /* numptr is a pointer */ • printf("content of num is %d\n", num); • printf("address of num is %p\n", &num); • printf("address of num1 is %p\n", &num1); • numptr = &num; /* initialize numptr with the address of num */ • printf("content of numptr is %p\n", numptr); • numptr = &num1; • printf("content of numptr is %p\n", numptr); • return 0; • }

  10. Why pointers • C was developed when computers were much less powerful • The raw ability to work with particular memory locations was obviously a useful option to have. • Programming microcontrollers still need this. • Optimize a program to run faster or use less memory that it would otherwise.

  11. #include<stdio.h> • int main(void) • { • intarr[3] = {2, 4, 6}, i; • for(i = 0; i<3; i++) • printf("The address of %d element is %p.\n", i, &arr[i]); • return 0; • } • The address of 0 element is 0032FE08. • The address of 1 element is 0032FE0C. • The address of 2 element is 0032FE10.

  12. Indirection operator • Pointers allow us to modify content in memory by using indirection operator (or called dereference operator). • Putting an asterisk before your pointer variable • See the example in the next slide

  13. What’s going here? • #include <stdio.h> • int main(void) • { • int bah = 10, val; • int* ptr = &bah; • val = *ptr; • printf("The value of val is %d.\n", val); • return 0; • }

  14. Pointers We can use pointers in much the same way we do the variables that they point to. • #include <stdio.h> • int main(void) • { • int a = 3, b = 3; /* a and b start with equal values */ • int* bptr = &b; /* we’ll modify b using a pointer */ • a *= 4; • *bptr *= 4; • printf("a is %d, b is %d\n", a, b); • a--; • (*bptr)--; /* parentheses are necessary here to override the order of precedence */ • printf("a is %d, b is %d\n", a, b); • return 0; • }

  15. Output: • a is 12, b is 12 • a is 11, b is 11