Jim's
Tutorials

Spring 2018
course
site

C

Thu Jan 25

We looked at a C "hello world" program, and then added some pointer stuff.

I've fleshed that out and uploaded it here.

/*************************************************
 * hello.c 
 *
 *  $ gcc --version
 *  ...
 *  Apple LLVM version 9.0.0 (clang-900.0.39.2)
 *  Target: x86_64-apple-darwin17.3.0
 *  ...
 *
 *  $ gcc hello.c -o hello
 *  $ ./hello
 *  Hello!
 *   i is 6
 *   *iptr is 6
 *   j is 7
 *   *jptr is 7
 *   sizeof(i) is 4
 *   sizeof(iptr) is 8
 *   iptr is 0x7ffee7143388
 *   jptr is 0x7ffee7143384
 *   &iptr is 0x7ffee7143378
 *   &jptr is 0x7ffee7143370
 *
 *  Here's what's going on inside.
 *  (Compare the 0x... hex values above with the addresses below.)
 *
 *  variable  address (of byte)     value
 *  --------  -------------------   -------
 *   i        0x7ffee3da2388    |1  6 
 *            0x7ffee3da2387    |2  (int, 4 bytes)
 *            0x7ffee3da2386    |3
 *            0x7ffee3da2385    |4
 *   j        0x7ffee3da2384  |1    7
 *            0x7ffee3da2383  |2    (int stored in 4 bytes)
 *            0x7ffee3da2382  |3
 *            0x7ffee3da2381  |4
 *            0x7ffee3da2380     .  the compiler left these empty
 *            0x7ffee3da237f     .
 *            0x7ffee3da237e     .
 *            0x7ffee3da237d     .
 *            0x7ffee3da237c     .
 *            0x7ffee3da237b     .
 *            0x7ffee3da237a     .
 *            0x7ffee3da2379     .
 *  iptr      0x7ffee3da2378   |1   0x7ffee3da2388
 *            0x7ffee3da2377   |2   (int* stored in 8 bytes)
 *            0x7ffee3da2376   |3
 *            0x7ffee3da2375   |4
 *            0x7ffee3da2374   |5
 *            0x7ffee3da2373   |6
 *            0x7ffee3da2372   |7
 *            0x7ffee3da2371   |8
 *  jptr      0x7ffee3da2370 |1     0x7ffee3da2384
 *            0x7ffee3da236f |2     (int* stored in 8 bytes)
 *            0x7ffee3da236e |3
 *            0x7ffee3da236d |4
 *            0x7ffee3da236e |5
 *            0x7ffee3da236c |6
 *            0x7ffee3da236b |7
 *            0x7ffee3da236a |8
 *
 * Jim Mahoney | cs.marlboro.college | Jan 2018 | MIT License 
 **************************************************************/

#include <stdio.h>

int main(){

  // ------ declare variables (i.e. allocate memory on stack) ------
  int i;        
  int j;
  int* iptr;    // also could be written as "int *iptr"
  int* jptr; 

  // ------ put values into variables ------
  i = 6;        

  iptr = &i;    // &i means "get address of i", which is put in iptr
  jptr = &j;

  *jptr = 7;    // *jptr means "follow pointer", so this same as "j = 7"

  // ------ print stuff out ------
  printf("Hello!\n");
  printf(" i is %i\n", i);
  printf(" *iptr is %i\n", *iptr);
  printf(" j is %i\n", j);
  printf(" *jptr is %i\n", *jptr);
  printf(" sizeof(i) is %lu\n", sizeof(i));
  printf(" sizeof(iptr) is %lu\n", sizeof(iptr));
  printf(" iptr is %p\n", iptr);
  printf(" jptr is %p\n", jptr);
  printf(" &iptr is %p\n", &iptr);
  printf(" &jptr is %p\n", &jptr);

  return 0;
}
https://cs.marlboro.college /cours /spring2018 /jims_tutorials /web_tech /c_jan25
last modified Sat December 21 2024 6:37 pm

attachments [paper clip]

  last modified size
TXT hello.c Sat Dec 21 2024 06:37 pm 2.9K