תכנות — שיעור 114 22/April/2010    

C Pointers

להלן כמה דוגמאות "טובות" ו"רעות".


==== Good Examples ====

good example: good1.c
     1	int main(int argc, char ** argv) {
     2	  int  one, two, three;
     3	  int  *pn = &one;
     4	  *pn = 1; /* Now:  one == 1 */
     5	  pn = &two;
     6	  *pn = 2; /* Now:  two == 2 */
     7	  three = one + (*pn);
     8	return three;                    }
gcc -Wall -o /tmp/dum good1.c

good example: good2.c
     1	#include <stdlib.h>
     2	
     3	void setvars(int *p_n, char **p_s, int **p_pa)
     4	{
     5	  enum { N = 7 };
     6	  *p_n = N;
     7	  *p_s = "foo";
     8	  *p_pa = malloc( N * sizeof(int) );
     9	}
    10	
    11	int main(int argc, char ** argv) {
    12	  int n; 
    13	  char *s;
    14	  int *pa;
    15	  setvars(&n, &s, &pa);
    16	return 0;                        }
gcc -Wall -o /tmp/dum good2.c

good example: good3.c
     1	#include <stdlib.h>
     2	
     3	void set_malloc(int **p_pn)
     4	{ *p_pn = malloc( 3 * sizeof(int) );  }
     5	
     6	void set_realloc(int **p_pn)
     7	{
     8	  int *pn;  
     9	  pn = *p_pn; /* get the pointer to int */
    10	  pn = realloc(pn, 7 * sizeof(int));
    11	  *p_pn = pn; /* put the new pointer to caller */
    12	}
    13	
    14	int main(int argc, char ** argv) {
    15	  int  *pn;
    16	  set_malloc(&pn);
    17	  set_realloc(&pn);
    18	return 0;                        }
gcc -Wall -o /tmp/dum good3.c

==== Bad Examples ====

Bad example: bad1.c
     1	int main(int argc, char ** argv) {
     2	  int *pn = 13; /* pointer should not be assigned simple int */
     3	return *pn; /* make it used */   }
gcc -Wall -o /tmp/dum bad1.c
bad1.c: In function ‘main’:
bad1.c:2: warning: initialization makes pointer from integer without a cast

Bad example: bad2.c
     1	int main(int argc, char **argv) {
     2	  int *pn;
     3	  pn = 13; /* pointer should not be assigned simple int */
     4	return *pn; /* make it used */   }
gcc -Wall -o /tmp/dum bad2.c
bad2.c: In function ‘main’:
bad2.c:3: warning: assignment makes pointer from integer without a cast

Bad example: bad3.c
     1	int main(int argc, char **argv) {
     2	  int m, n;
     3	  int *pn = &m; /* OK */
     4	  n = pn; /* int should not be assigned a pointer */
     5	return *pn; /* make it used */   }
gcc -Wall -o /tmp/dum bad3.c
bad3.c: In function ‘main’:
bad3.c:4: warning: assignment makes integer from pointer without a cast

Bad example: bad4.c
     1	#include <stdlib.h>
     2	void set_malloc(int *pn)
     3	{
     4	   pn = malloc(sizeof(int)); /* OK, but no one will use it :( */
     5	}
     6	
     7	int main(int argc, char **argv) {
     8	  int *pn;
     9	  set_malloc(pn); /* Legal in C, but useless */
    10	  *pn = 7; /* Will fail at runtime !! */
    11	return *pn; /* make it used */   }
gcc -Wall -o /tmp/dum bad4.c
bad4.c: In function ‘main’:
bad4.c:9: warning: ‘pn’ is used uninitialized in this function

תרגילים

  1. הורד ופתח את examples.tar.gz במדריך (directory) חדש ונקי, הרץ
    ./compgb.sh
    והבן את התוצאות.
  2. השלם תרגיל קודם pdb-v6.c
  3. עבור שוב על תכנית הדוגמא args.c משעור קודם והבן את השמוש במערך של המחרוזות
    char **argv
    שהוא בעצם מצביע למצביעים על מחרוזות.

חזרה לעמוד האם