Skip to content

Commit

Permalink
Representing array using a pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
jainkhere committed Mar 22, 2022
1 parent 6781044 commit 5e12c94
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions pointer-operations_arrays.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* @file pointer-operations_arrays.c
* @author kunal jain ([email protected])
* @brief This programs shows how to represent array using
* pointers
* @version 0.1
* @date 2022-03-07
*
* @copyright Copyright (c) 2022
*
*/

#include<stdio.h>

void test_illegal_pointer_assignment() {
int* p;
// Without initializing pointer with a null value,
// below assignment - *p = 100, could be dangerous
// as we are trying access and alter the value present
// at an address that we know nothing about.

// A pointer should always be set to null before we assign
// value to that pointer.
// That prevents any dangerous alterations we might perform
// on the memory space.
p = NULL;


printf("Address of p -> %d\n", &p);
printf("Value of p -> %d\n", p);
printf("Value at p -> %d\n", *p);

printf("Before assignment\n");

// On some compilers your code may crash here if you
// don't assign a NULL value to pointer.
*p = 100;

printf("After assignment\n");

printf("Address of p -> %d\n", &p);
printf("Value of p -> %d\n", p);
printf("Value at p -> %d\n", *p);
}

int main(void) {

test_illegal_pointer_assignment();

char a[] = "HelloWorld";
char b[] = "HelloCS240";

// print a[6] and b[5]
printf("Value of a[6] -> %c\n", a[6]);
printf("Value of b[5] -> %c\n", b[5]);

// Print values without using array name
// TYPE *pointer-name = array-name
// TYPE -> type of element of array
char* pa = a;
// pa + k -> a[k]
char* pb = b;

// *pa -> a[0], *(pa + 0) -> a[0]
// *(pa + i) -> a[i]
printf("Value of a[6] -> %c\n", *(pa + 6));
printf("Value of b[5] -> %c\n", *(pb + 5));

char* c[2];
c[0] = pa;
c[1] = pb;

// print values without pa and pb

// *(pa + i) -> a[i]
// *(c[0] + i) -> a[i]
// *c[0] -> *(pa) -> *(&a[0]) -> Value at address of a[0] -> a[0]
printf("Value of a[6] -> %c\n", *(c[0] + 6));
printf("Value of b[5] -> %c\n", *(c[1] + 5));

// Represent c using pointer
// TYPE *pointer-name = array-name
// TYPE -> type of element of array
char**pc = c;


return 0;
}

0 comments on commit 5e12c94

Please sign in to comment.