Pointers

Prerequisites:
Used In
Contents
  1. Basics
    • pointer to an Array
    • Accessing Next variable
    • Dynamically allocating memory
    • Accessing values inside a struct pointer
  2. Generic Pointer
  3. Pass by Reference
  4. Constant pointer
  5. Pointer to a constant
  6. Pointer to pointer
  7. Function Pointer

1. Basics

Pointer is a variable that holds the memory address of another variable

#example

int *x;

here x is a integer pointer to an integer

int value = *x ; // get value of the variable the x point to 
int address = x ; // get the address of the variable the x point to

#completeCode 1

#include <stdio.h>
int main() {
  int x = 10;
  int *ptr;
  ptr = &x;
  printf("Value of x = %d\n", x);
  printf("Value of variable ptr points to  = %d\n", *ptr);
  printf("Address of x = %p\n", &x);
  printf("Value of  ptr = %p\n", ptr);
}
Output

Value of x = 10
Value of variable ptr points to  = 10
Address of x = 0x7ffc99f1581c
Value of  ptr = 0x7ffc99f1581c

You can see that ptr holds the address of x and *ptr gives the value of x.

Reassgning the pointer

#include <stdio.h>
int main(){
    int x = 5;
    int *ptr = &x;
    printf("Value of x = %d\n",*ptr);
    printf("Address of x = %p\n",ptr);
    int y = 10;
    ptr = &y;
    printf("Value of y = %d\n",*ptr);
    printf("Address of y = %p\n",ptr);
}
Accessing Next Variable
#include <stdio.h>
int main(void) {
  const int a = 10, b = 12;
  int *p;
  p = &a;
  printf("%d", *(p + 1));
}

output Using gcc

12

output Using clang

-794418688

In this program the i was able to access the variable b by using *(p+1) because the gcc assigned contiguous memory location for the two variables.

Dont use this

This might not work all the time .

Changing Values using Pointer

 #include <stdio.h>
int main(){
    int x = 5;
    int *ptr = &x;
    printf("Value of x= %d\n",x);
    *ptr = 10;
    printf("Value of x after modification = %d\n",x);
}
Output

Value of x= 5
Value of x after modification = 10

Pointer to an array

#include <stdio.h>
int main(int argc, char *argv[]) {
	  int A[2] = {1, 3};
	  int *p = A;
	  printf("%d\n", *p);
	  printf("%d", *(p + 1));
}

Dynamically allocating memory

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
	char *string;
	string = (char *)malloc(sizeof(char) * 3);
	string[0] = 'h';
	string[1] = 'i';
	string[2] = '\0'; // Null-terminate the string
	printf("%s\n", string);
	return 0;
}

Accessing values inside a struct pointer

 #include <stdio.h>
struct somethin {
  int value;
  int value2;
};

int main(){
    struct somethin s =  {10 ,20};
    struct somethin *ptr = &s; 
    printf(ptr->value == s.value ? "True" : "False");
}
True
#include <stdio.h>
#include <stdlib.h>
struct somethin {
  int value;
  char *name;
};
int main(int argc, char *argv[]) {
  struct somethin *new_struct = malloc(sizeof(struct somethin));
  new_struct->value = 10;
  printf("value = %d", (*new_struct).value);
  return 0;
}

2. Generic Pointer

It has a void datatype

void *ptr;
int main() {
  int x = 10;
  void *ptr;
  ptr = &x;
  printf("The value of x =%d ", *(int *)ptr);
}

Advanced

3. Pass by Reference

#include <stdio.h>

void print_value(int *x) {
    printf("Value: %d\n", *x);
}

int main(){
    int value = 10;
    print_value(&value);
    return 0;
}
Value: 10

#explenation

4. Constant pointer

the address that is pointing to, cannot be changed
#syntax

<type-of-pointer> *const <name-of-pointer>

char x = 'x' ;
char *const ptr = &x; 

Assigning new value to that pointer will result in error as the following

#include <stdio.h>

int main() {
    char x = 'x';
    char *const ptr = &x; 
    char y = 'y';
    ptr = &y;  // this will throw an error like "variable 'ptr' declared const here"
}

the following is possible

#include <stdio.h>

int main() {
    char x = 'x';
    char *const ptr = &x; 
    *ptr = 'y';
    printf("x is now : %c\n", x);
}
x is now:  y
#include <stdio.h>

int main() {
    char x = 'x';
    char *const ptr = &x; 
    char y = 'y';
    ptr = &y;  // not possible 
    *ptr = 'z'; // possible 
}

5. Pointer to a contant

pointer cannot change the value at the address pointed by it

const <type-of-pointer> *<name-of-pointer>;

if we compare this with ![[#^de91e5]]
#example

#include<stdio.h>

int main(void)
{
    char ch = 'c';
    const char *ptr = &ch;
    ch = 'a'; // possible 
    *ptr = 'b' ; // not possible 
    return 0;
}

6. Pointer to pointer

You will have to dereference[1] it twise

#include <stdio.h> 

int main(){
    char x = 'x';
    char *ptr = &x;
    char **ptr_to_ptr = &ptr; 
    printf("Value of x: %c " , **ptr_to_ptr); 
}

7. Function Pointer

#include <stdio.h>
void print_sum(int a, int b) {
    printf("Sum: %d\n", a + b);
}
int main() {
    void (*func_ptr)(int, int) = print_sum; 
    func_ptr(5, 10); 
    return 0;
}

References

  1. Source 1
  2. Source 2 PDF

  1. accessing the value stored at the memory address that the pointer is holding ↩︎