Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://www.programiz.com/c-programming/c-pointers
- #include <stdio.h>
- int main(){
- //Ampressed used to give memory location aka container where value from scanf should be saved
- //scanf("%d", &var);
- //pointers are used to store memory locations of other variables and make them accessible
- //if we stored memory locations in integers there would be no way to access them,
- //int *pc; same as int* pc;
- int *pc;
- int c;
- c = 22;
- printf("Address of c: &c = %d\n", &c);
- printf("Value of c: c = %d\n\n", c);
- pc = &c;
- printf("pc = &c\n\n");
- printf("Address of pointer pc: pc = %d\n", pc);
- printf("Content of pointer pc: *pc = %d\n\n", *pc);
- c = 11;
- printf("c = 11\n\n");
- printf("Address of pointer pc: pc = %d\n", pc);
- printf("Content of pointer pc: *pc = %d\n\n", *pc);
- *pc = 2;
- printf("*pc = 2\n\n");
- printf("Address of c: &c = %d\n", &c);
- printf("Value of c: c = %d\n\n", c);
- printf("=====================================\n\n");
- /* an array with 5 elements */
- double balance[5] = { 1000.0, 2.0, 3.4, 17.0, 50.0 };
- double *p;
- int i;
- // array variable name without index is same as refference to first element (&balance[0])
- p = balance;
- /* output each array element's value */
- printf("Array values using indexes\n");
- for (i = 0; i < 5; i++) {
- printf("p[%d]: %f\n", i, p[i]);
- }
- printf("Array values using pointer\n");
- for (i = 0; i < 5; i++) {
- printf("*(p + %d) : %f\n", i, *(p + i));
- }
- printf("Array values using balance as address\n");
- for (i = 0; i < 5; i++) {
- printf("*(balance + %d) : %f\n", i, *(balance + i));
- }
- printf("%d\n", sizeof(double));
- for (i = 0; i < 5; i++) {
- printf("&balance[%d] = %d\n", i, &balance[i]);
- }
- for (i = 0; i < 5; i++) {
- printf("*(balance + i) = *(%d + %d) = *(%d) = %f\n", balance, i, (balance + i), *(balance + i));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement