9 Triple use for Asterisk We have now seen three distinct meanings of the

# 9 triple use for asterisk we have now seen three

This preview shows page 9 - 13 out of 13 pages.

9 Triple use for Asterisk (*) We have now seen three distinct meanings of the symbol *. As Multiplication operator: x * y => x times y In declaration * tells the compiler that a new variable is to be a pointer (read as “pointer to”) Thus, in this case, it is a part of the name of the type of the variable. As unary indirection operator : It provides the content of the memory location specified by a pointer. It mean “follow the pointer”. It can also stand on the left side of an assignment. Here the type depends on the variable being pointed – char in the above case. It is a common mistake by students to interpret the above as a pointer type. int * p x = * p * p = ‘K’ 10 Example 3: /* computes the area and circumference of a circle, given its radius */ #include <stdio.h> void area_circum (double radius, double *area, double *circum); int main (void) {      double radius, area, circum ;      printf ("Enter the radius of the circle > ") ;      scanf ("%lf", &radius) ;      area_circum (radius, &area, &circum) ;       printf ("The area is %f and circumference is %f\n", area, circum) ;      system("pause");      return 0; } void area_circum (double radius, double *area, double *circum) {      *area = 3.14 * radius * radius ;      *circum = 2 * 3.14 * radius ;  } 11 Example 4: /* Takes three integers and returns their sum, product and average */ #include<stdio.h> void myfunction(int a,int b,int c,int *sum,int *prod, double *average); int main (void) { int n1, n2, n3, sum, product; double av_g; printf("Enter three integer numbers > "); scanf("%d %d %d",&n1, &n2,&n3); myfunction(n1, n2, n3, &sum, &product, &av_g);  printf("\nThe sum = %d\nThe product = %d\nthe avg = %f\n",sum,product,av_g); system("pause"); return 0; }   void myfunction(int a,int b,int c,int *sum,int *prod, double *average) {      *sum=a+b+c;      *prod=a*b*c;      *average=(a+b+c)/3.0; 12 Example 5: /* takes the coefficients of quadratic equation  #### You've reached the end of your free preview.

Want to read all 13 pages?

• Spring '10
• baleh
• Pointer, Return statement, Void type
• • •  