Skip to content

Instantly share code, notes, and snippets.

@d630
Last active May 31, 2018 04:20
Show Gist options
  • Save d630/a629cbd32989b0226f99d1f144acb118 to your computer and use it in GitHub Desktop.
Save d630/a629cbd32989b0226f99d1f144acb118 to your computer and use it in GitHub Desktop.
Some pointer stuff
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct person {
char nname[20];
char vname[20];
};
int main()
{
char str[] = "Foo bar lich";
char *poi;
poi = &str[0]; // or: poi = str;
printf("%s\n", str);
printf("%s\n", poi);
printf("%c\n", *poi);
while (*poi != '\0') {
printf("%c\n", *poi);
poi++;
}
//
int zahl = 1;
char *poi1 = &zahl;
//TODO(d630)
*poi1 = *poi1 + 459999;
printf("%d %d\n", zahl, *poi1);
*poi1 = 1 + 459999;
printf("%d %d\n", zahl, *poi1);
//
struct person p;
strcpy(p.nname, "n");
strcpy(p.vname, "v");
printf("%s %s\n", p.nname, p.vname);
struct person *ptr = &p;
printf("%s %s\n", ptr->nname, ptr->vname);
//
strcpy(ptr->vname, "hugo");
strcpy(ptr->nname, "l");
printf("%s %s\n", ptr->nname, ptr->vname);
return 0;
}
// vim: set ft=c :
#include <stdio.h>
#include <stdlib.h>
void
test_case(char *str)
{
while (*str != '\0') {
if (*str >= 65 && *str <= 90)
printf("%c upper-case letter\n", *str);
else if (*str >= 97 && *str <= 122)
printf("%c lower-case letter\n", *str);
str++;
}
}
void
test_rename(char *str, char c1, char c2)
{
while (*str != '\0') {
if (*str == c1)
*str = c2;
str++;
}
}
char *
test_cut(char *str, char c)
{
while (*str != '\0') {
if (*str == c)
break;
str++;
}
return str;
}
void
test_fill(int *arr, int l, int j)
{
for (int i = 0; i < l; i++)
arr[i] = j++;
}
void
test_print(int *arr, int len)
{
for (int i = 0; i < len; i++)
printf("%d %d\n", i, arr[i]);
}
int
main()
{
char str[] = "Hello_World!";
printf("old: <%s\n>", str);
test_rename(str, '_', ' ');
printf("new: <%s\n>", str);
test_case(str);
printf("<%s>\n", test_cut(str, 'W'));
printf("<%s>\n", test_cut(str, '_'));
// ---
int iarr[6] = {0};
int len = sizeof(iarr)/sizeof(iarr[0]);
test_print(iarr, len);
test_fill(iarr, len, 1);
test_print(iarr, len);
return 0;
}
// vim: set ft=c :
@d630
Copy link
Author

d630 commented May 29, 2018

gcc -g -O2 -std=c99 -Wall -Wextra -Wwrite-strings -o pointer pointer.c && chmod +x ./pointer && ./pointer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment