Sunteți pe pagina 1din 5

Array to Pointers

How to declare an array?


dataType arrayName[arraySize];

For example,

float mark[5];

How to initialize an array?


int mark[5] = {19, 10, 8, 17, 9};

int mark[] = {19, 10, 8, 17, 9};

Change Value of Array elements


1. int mark[5] = {19, 10, 8, 17, 9}

2. // make the value of the third element to -1


3. mark[2] = -1;
4.
5. // make the value of the fifth element to 0
6. mark[4] = 0;

Input and Output Array Elements


1. take input and store it in the 3rd element
2. scanf("%d", &mark[2]);
Array Input/Output
1. // Program to take 5 values from the user and store them in an array
2. // Print the elements stored in the array
3. #include <stdio.h>
4.
5. int main() {
6. int values[5];
7.
8. printf("Enter 5 integers: ");
9.
10. // taking input and storing it in an array
11. for(int i = 0; i < 5; ++i) {
12. scanf("%d", &values[i]);
13. }
14.
15. printf("Displaying integers: ");
16.
17. // printing elements of an array
18. for(int i = 0; i < 5; ++i) {
19. printf("%d\n", values[i]);
20. }
21. return 0;
22. }

Example 2: Calculate Average


1. // Program to find the average of n numbers using arrays
2.
3. #include <stdio.h>
4. int main()
5. {
6. int marks[10], i, n, sum = 0, average;
7.
8. printf("Enter number of elements: ");
9. scanf("%d", &n);
10.
11. for(i=0; i<n; ++i)
12. {
13. printf("Enter number%d: ",i+1);
14. scanf("%d", &marks[i]);
15.
16. // adding integers entered by the user to the sum variable
17. sum += marks[i];
18. }
19.
20. average = sum/n;
21. printf("Average = %d", average);
22.
23. return 0;
24. }
Pointers
Pointer Syntax
1. int* p;

Assigning addresses to Pointers


1. int* pc, c;
2. c = 5;
3. pc = &c;

Changing Value Pointed by Pointers


1. int* pc, c;
2. c = 5;
3. pc = &c;
4. c = 1;
5. printf("%d", c); // Output: 1
6. printf("%d", *pc); // Ouptut: 1

Let's take another example.


1. int* pc, c;
2. c = 5;
3. pc = &c;
4. *pc = 1;
5. printf("%d", *pc); // Ouptut: 1
6. printf("%d", c); // Output: 1

Working of Pointers
7. #include <stdio.h>
8. int main()
9. {
10. int* pc, c;
11.
12. c = 22;
13. printf("Address of c: %p\n", &c);
14. printf("Value of c: %d\n\n", c); // 22
15.
16. pc = &c;
17. printf("Address of pointer pc: %p\n", pc);
18. printf("Content of pointer pc: %d\n\n", *pc); // 22
19.
20. c = 11;
21. printf("Address of pointer pc: %p\n", pc);
22. printf("Content of pointer pc: %d\n\n", *pc); // 11
23.
24. *pc = 2;
25. printf("Address of c: %p\n", &c);
26. printf("Value of c: %d\n\n", c); // 2
27. return 0;
28. }

Relationship Between Arrays and Pointers


Let's write a program to print addresses of array elements.
1. #include <stdio.h>
2. int main() {
3. int x[4];
4. int i;
5.
6. for(i = 0; i < 4; ++i) {
7. printf("&x[%d] = %p\n", i, &x[i]);
8. }
9.
10. printf("Address of array x: %p", x);
11.
12. return 0;
13. }

&x[1] is equivalent to x+1 and x[1] is equivalent to *(x+1).

Pointers and Arrays


#include <stdio.h>
int main() {
int i, x[6] = {2,4,5,6,7,8}, sum = 0;
printf("Enter 6 numbers: ");
for(i = 0; i < 6; ++i) {
// Equivalent to scanf("%d", &x[i]);
scanf("%d", &x[i]);

// Equivalent to sum += x[i]


sum += *(x+i);
printf("the sum is %d \n",sum);
}
printf("Sum = %d", sum);
return 0;
}

Arrays and Pointers


1. #include <stdio.h>
2. int main() {
3. int x[5] = {1, 2, 3, 4, 5};
4. int* ptr;
5.
6. // ptr is assigned the address of the third element
7. ptr = &x[2];
8.
9. printf("*ptr = %d \n", *ptr); // 3
10. printf("*(ptr+1) = %d \n", *(ptr+1)); // 4
11. printf("*(ptr-1) = %d", *(ptr-1)); // 2
12.
13. return 0;
14. }

Assignment 4:
1. Store Numbers and Calculate Average Using Arrays
2. Write a program in C to add numbers using call by reference.
3. Write a program in C to find the maximum number between two numbers using
a pointer.
4. Write a program in C to swap elements using call by reference.
5. Write a program in C to find the factorial of a given number using pointers.
6. Write a C program to reverse an array using pointers.
7. Write a C program to access two dimensional array using pointers.
8. Write a C program to add two matrix using pointers.
9. Write a C program to multiply two matrix using pointers.
10. Write a C program to copy one array to another using pointers.

S-ar putea să vă placă și