Pointers

  1. char a[10]. a is simply a pointer to the address of the first character
  2. a[5] is *(a + 5). Shifts the pointer after 5 locations (depending on the data type on address), and then point to the values.

Examples

#include <stdio.h>
#include <string.h>
int main() {
    char a[100], b[100];
    printf("Enter two strings\n");
    scanf("%s", a);
    scanf("%s", b);

    char res[300] = "";
    strcat(res, a);
  	strcat(res, " & ");
    strcat(res, b);
    printf("%s", res);
}

rstrip

#include <stdio.h>
#include <string.h>

void py_rstrip(char inp[]) {
    int length = (int) strlen(inp);
    int i = length - 1;  // Start from the last character before the null terminator
    for (; i >= 0; i--) {
        if (inp[i] != ' ') {  // Break the loop when a non-space character is found
            break;
        }
    }
    inp[i + 1] = '\0';  // Set the null terminator after the last non-space character
}


int main() {
    char s1[] = "   Hello   World    ";
    py_rstrip(s1);
    printf("-%s-\n", s1);
}

Pointers to Functions

#include <stdio.h>

int add(int a, int b) {
    return a + b;
}

int main() {
    int (*func_ptr)(int, int) = &add;
    int result = (*func_ptr)(2, 3);
    printf("%d\n", result);
}
Yiming Zhang
Yiming Zhang
Quantitative Researcher Associate, JP Morgan