0% found this document useful (0 votes)
9 views4 pages

example pointer

The document contains multiple C programs demonstrating the use of pointers, including arrays of pointers to integers, characters, and functions. It also illustrates differences between pointers to integers and arrays, as well as printing elements from a 3-D array using pointer notation. Each program is designed to showcase specific pointer functionalities and their applications in C programming.

Uploaded by

nathakrit-c
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

example pointer

The document contains multiple C programs demonstrating the use of pointers, including arrays of pointers to integers, characters, and functions. It also illustrates differences between pointers to integers and arrays, as well as printing elements from a 3-D array using pointer notation. Each program is designed to showcase specific pointer functionalities and their applications in C programming.

Uploaded by

nathakrit-c
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

ตัวอยางโปรแกรมที่ใชพอยเตอร

// C program to demonstrate the use of array of pointers


#include <stdio.h>
int main()
{
// declaring some temp variables
int var1 = 10;
int var2 = 20;
int var3 = 30;

// array of pointers to integers


int* ptr_arr[3] = { &var1, &var2, &var3 };

// traversing using loop


for (int i = 0; i < 3; i++) {
printf("Value of var%d: %d\tAddress: %p\n", i + 1, *ptr_arr[i], ptr_arr[i]);
}

return 0;
}

// C Program to print Array of strings without array of pointers


#include <stdio.h>
int main()
{
char str[3][10] = { "Geek", "Geeks", "Geekfor" };

printf("String array Elements are:\n");

for (int i = 0; i < 3; i++) {


printf("%s\n", str[i]);
}

return 0;
}
// C program to illustrate the use of array of pointers to characters
#include <stdio.h>
int main()
{
char* arr[3] = { "geek", "Geeks", "Geeksfor" };
for (int i = 0; i < 3; i++) {
printf("%s\n", arr[i]);
}
return 0;
}

// C program to illustrate the use of array of pointers to function


#include <stdio.h>
// some basic arithmetic operations
void add(int a, int b) {
printf("Sum : %d\n", a + b);
}
void subtract(int a, int b) {
printf("Difference : %d\n", a - b);
}
void multiply(int a, int b) {
printf("Product : %d\n", a * b);
}
void divide(int a, int b) {
printf("Quotient : %d", a / b);
}
int main() {
int x = 50, y = 5;
// array of pointers to function of return type int
void (*arr[4])(int, int)
= { &add, &subtract, &multiply, ÷ };
for (int i = 0; i < 4; i++) {
arr[i](x, y);
}
return 0;
}
#include<stdio.h>
int main()
{
int arr[5] = { 1, 2, 3, 4, 5 };
int *ptr = arr;
printf("%p\n", ptr);
return 0;
}

// C program to understand difference between pointer to an integer and pointer to an


// array of integers.
#include<stdio.h>

int main()
{
// Pointer to an integer
int *p;

// Pointer to an array of 5 integers


int (*ptr)[5];
int arr[5];

// Points to 0th element of the arr.


p = arr;

// Points to the whole array arr.


ptr = &arr;

printf("p = %p, ptr = %p\n", p, ptr);

p++;
ptr++;

printf("p = %p, ptr = %p\n", p, ptr);

return 0;
}
// C program to illustrate sizes of pointer of array
#include<stdio.h>
main()
{
int arr[] = { 3, 5, 6, 7, 9 };
int *p = arr;
int (*ptr)[5] = &arr;
printf("p = %p, ptr = %p\n", p, ptr);
printf("*p = %d, *ptr = %p\n", *p, *ptr);
printf("sizeof(p) = %lu, sizeof(*p) = %lu\n", sizeof(p), sizeof(*p));
printf("sizeof(ptr) = %lu, sizeof(*ptr) = %lu\n", sizeof(ptr), sizeof(*ptr));
}

// C program to print the elements of 3-D array using pointer notation


#include<stdio.h>
main()
{ int arr[2][3][2] = {
{ {5, 10},
{6, 11},
{7, 12},
},
{ {20, 30},
{21, 31},
{22, 32},
}
};
int i, j, k;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
{
for (k = 0; k < 2; k++)
printf("%d\t", *(*(*(arr + i) + j) +k));
printf("\n");
}
}
}

You might also like