O'Reilly logo

C Primer Plus, Fourth Edition by Stephen Prata

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 10

1: What will this program print?
#include <stdio.h>
int main(void)
{
  int ref[] = {8, 4, 0, 2};
  int *ptr;
  int index;

  for (index = 0, ptr = ref; index < 4; index++, ptr++)
     printf("%d %d\n", ref[index], *ptr);
  return 0;
}
A1: The printout is this:
8 8
4 4
0 0
2 2
2: In question 1, how many elements does ref have?
A2: The array ref has 4 elements because that is the number of values in the initialization list.
3: In question 1, ref is the address of what? What about ref + 1? What does ++ref point to?
A3: The array name ref points to the first element of the array, the integer 8. The expression ref + 1 points to the second element, the integer 4. The construction ++ref is not a valid C expression; ref is a constant, not a variable.
4: What ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required