-
-
Save telliott99/6ebe0d739ee62b732ab2 to your computer and use it in GitHub Desktop.
pointer demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
void f1(int *ptr, int n) { | |
printf("f1\n"); | |
for (int i = 0; i < n; i++) { | |
printf("%d ", ptr[i]); | |
} | |
printf("\n"); | |
} | |
void f2(int *ptr, int n) { | |
printf("f2\n"); | |
for (int i = 0; i < n; i++) { | |
int v = ptr[i]; | |
ptr[i] = v*v; | |
} | |
} | |
void f3(int *ptr, double *ptr2, int n) { | |
printf("f3\n"); | |
for (int i = 0; i < n; i++) { | |
int v = ptr[i]; | |
ptr2[i] = sqrt(sqrt(v)); | |
} | |
} | |
int main(int argc, char** argv) { | |
const int n = 5; | |
int a[n] = { 1, 2, 3, 4, 5 }; | |
f1(a,n); | |
f2(a,n); | |
for (int i = 0; i < n; i++) { | |
printf("%d ", a[i]); | |
} | |
printf("\n"); | |
double b[n]; | |
f3(a,b,n); | |
for (int i = 0; i < n; i++) { | |
printf("%3.2f ", b[i]); | |
} | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment