https://github.com/dmitry/learn-c
Learning C language
https://github.com/dmitry/learn-c
Last synced: 5 months ago
JSON representation
Learning C language
- Host: GitHub
- URL: https://github.com/dmitry/learn-c
- Owner: dmitry
- Created: 2013-01-13T17:25:28.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2014-01-14T01:30:29.000Z (over 12 years ago)
- Last Synced: 2025-06-11T07:12:07.218Z (about 1 year ago)
- Size: 129 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
Here are my notes for the C language, that I've made for a quick reference in the future. It something like a cheatsheet, but very personal.
```c
int a[4] = {1,2,3,4};
int a[] = {1,2,3,4};
int a[] = {0};
int *pa;
int **pb;
```
take third element of an array
`a[3] == *(a + 3) == 3[a] == *(&a[0] + 3)` - basically array variable is a pointer on a first element of array
```c
if (a[3] == *(a + 3) &&
a[3] == 3[a] &&
a[3] == *(&a[0] + 3)) {
printf("yey");
}
```
`*pa` - get value
`pa` - get or set memory location
`pa = a`
`pa = &a[0]` - the same as above
`*a` - get first element of array
`int &b` - reference, cannot be NULL or uninitialized
`pa = &b` - get address of variable and assign it to pointer
`**pb = &pa` - reference on reference
### Function pointer (similar to C++ templates)
```c
#include
int referenced(int val) {
return 2 * val;
}
void execute(int (*ptr)(int)) {
printf("%d\n", ptr(3));
}
int main() {
int (*ptr_func)(int) = &referenced;
execute(ptr_func);
return 1;
}
```
## Debugging
* strace
* ltrace
* dtrace
* ktrace
## References
* http://en.cppreference.com/w/