Pointers in C

Variables that point to other variables

%d)\n".swap function that does not work void swap1(int x. } . x = y.y)=(%d. printf("Later (x. printf("Initially (x. y). x.%d)\n". int y) { int temp = x. y). y = temp. x.y)=(%d.

swap1(a. printf("Initially (a. b).%d)\n". a. b). . b).b)=(%d. printf("After swp (a.b)=(%d.%d)\n". a. b = 15.Calling swap1 function a = 10.

} . *p1 = *p2.swap function that works void swap2(int *p1. int *p2) { int temp = *p1. *p2 = temp.

%d)\n".Calling swap2 function printf("Initially (a. a. printf("After swp (a. b). &b).%d)\n".b)=(%d. . b).b)=(%d. swap2(&a. a.

i. return sum. i++) sum = sum + zz[i]. sum = 0. } . for (i=0.Function to calculate sum of an array int sumf(int zz[]. i<n. int n) { int sum.

30. m)). printf("sum = %d\n". . m = sizeof(c)/sizeof(c[0]). 50}. 40. sumf(c. 20. int c[] = {10.Calling sumf function int m.

i<n. i++) sum = sum + zz[i]. i. *sump = sum.Function to calculate sum of an array void sumf2(int zz[]. int *sump) { int sum. int n. sum = 0. for (i=0. } .

a). sumf2(c. 30. int c[] = {10. m = sizeof(c)/sizeof(c[0]). m. printf("sum = %d\n". 20. 40. m. &a). .Calling sumf2 function int a. 50}.

. b. m.Declare variables and pointers int a. int *p1.

20. m = sizeof(c)/sizeof(c[0]).Declare and initialize array and calculate number of elements in it int c[] = {10. . 40. 30. 50}.

Printing the address of a variable int x. &x). . printf("address of x = %p\n". x = 12. &x). printf("address of x = %d\n".

p = &x. . int *p. x = 12.Making a pointer variable point to an integer variable int x.

printf("value of x = %d\n". p = &x. *p).Print value of a variable through a pointer int x. int *p. x = 12. .

int *p.Changing value of a variable through a pointer int x. x = 12. p = &x. printf("value of x = %d\n". *p = 20. *p). .

Sign up to vote on this title
UsefulNot useful