SlideServe
Menu
Préférences
© SlideServe · v2026

Pointers

hope-shelton avatar
19 diapositives · 484 Views · Nov 07, 2014

Pointers. CSE 2451 Yang Zhang. Language comparison. C has pointers Java has references C++ has pointers and references. Pointers. Values of variables are stored in memory, at a particular location A location is identified and referenced with an address

Pointers - slide 1
01 / 19Pointers
1 / 17

Télécharger la présentation

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. Pointers CSE 2451 Yang Zhang

  2. Language comparison • C has pointers • Java has references • C++ has pointers and references

  3. Pointers • Values of variables are stored in memory, at a particular location • A location is identified and referenced with an address • Analogous to identifying a house’s location via an address • Pointer  address • Address vs. contents • Pointer variable • Value holds the address of a memory location

  4. Pointers and Addresses • A byte-addressable machine has consecutively numbered or addressed memory cells (bytes) which may be manipulated individually or in contiguous groups. char * ip1; char ip1 short int/long ip2 double double * ip2;

  5. Pointers and Addresses • a pointer array holding addresses of characters; • char * a[2]; • A pointer array holding addresses of integers; • int * b[2];

  6. Pointer syntax • * is used in the declaration of a pointer • int*p -> pointer to an integer • & (unary operator) gives the “address of” an object • p = &c means the address of c is assigned to the variable p

  7. Pointer syntax • * (unary operator) is a dereferencing operator when applied to pointers • Access the variable the pointer points to • * in front of a pointer variable means “get the value at that address” i.e. “contents of” • inta = *p • Get the value at the address designated by p and assign it to a • *p = 1 • Assign the value of 1 to the memory location designated by the address of p

  8. Declaring pointers • int *a; • int* a; • int *b, *c, *d • char *message = “Hello world!”; • char *message • message = “Hello world!”;

  9. Pointers and Addresses • pointers and references: “&” vs “*” • “&” is used on a variable (can be char, int, double, array element) to get the address: int i, *ip1, *ip2, a[8]; char c, *cp; ip1 = &i; ip2 = &a[0]; cp = &c;

  10. Pointers and Addresses • pointers and references: “&” vs “*” • “&” can not be used on a constant, array name, register variable and expressions: char * ip1; register int a; char b[10]; int * ip2; ip1=&’y’; /* WRONG */ ip1=&”abcdef”; /*WRONG*/ ip1=“abcedf”; /*RIGHT*/ ip2=&7; /* WRONG */ ip2=&a; /*WRONG*/ ip1=b; /*RIGHT */ ip1=&b; /*WRONG */ ip2=&(ip1+2); /*WRONG*/

  11. Pointers and Addresses • pointers and references: “&” vs “*” • * is used on a pointer to access the variable it points to. int x =1,y[2]={2,3}; int * ip1,*ip2,*ip3; ip1=&x; *ip1=0; //x=0; printf(“%d\t%d\n”,x,y[0]); ip2=&y[0]; *ip2=1; //y[0]=1; printf(“%d\n”,y[0]); ip3=y; //ip3=&y[0]; *ip3=0; //y[0]=0; printf(“%d\n”,y[0]);

  12. Pointers and Addresses • pointers and references: “&” vs “*” • “* pointer” has a datatype which is the same as that of the variable the pointer points to. • int *a; • every pointer points to a specific data type except void *; • void * is used to hold the address of any data type.

  13. Pointers and Function Arguments • Still pass-by-value with pointers • Dereferencing the value of the pointer gives a memory address • The memory’s content can change • Note: • Note argument types when passing parameters • swap (int*, int *); • swap(&x,&y); /*pass addresses of two integer variables*/

  14. Address Arithmetic • Pointers can use arithmetic operations int *ip1, *ip2, a[8]; ip1 = &a[7]; ip2 = &a[0]; What about: ip2 – ip1 + 1 ip1 – ip2 ? ip1 == NULL? ip1 > ip2 ?

  15. Pointer example 1 #include<stdio.h> intmain() { float i=10, *j; void *k; k=&i; j=k; printf("%f\n", *j); return 0; }

  16. Pointer example 2 #include <stdio.h> #include <stdlib.h> main() { intx, *p; p = &x; *p = 0; printf("x is %d\n", x); printf("*p is %d\n", *p); *p += 1; printf("x is %d\n", x); (*p)++; printf("x is %d\n", x); return 0; }

  17. Pointer example 3 #include <stdio.h> int main(void) { char ch = 'c'; char *chptr = &ch; inti = 20; int*intptr = &i; float f = 1.20000; float *fptr = &f; char *ptr = "I am a string"; printf("\n [%c], [%d], [%f], [%c], [%s]\n", *chptr, *intptr, *fptr, *ptr, ptr); return 0; }

  18. Pointers and Function Arguments • C passes arguments to functions by value; • swap(int x, int y); /* change local copies of x, y*/ • swap(int * x, int * y); /*use pointer to change the content of two variables */

  19. Parameter Passing -- Example void swap (int x, int y) { int temp; temp = x; x = y y = temp; } void swap (int *x, int *y) { int temp; temp = *x; *x = *y *y = temp; }