Solution for Exercise 18-7
ex1807
#include <stdio.h> int main() { char hello[] = "Hello!"; int i = 0; while(hello[i]) { printf("%c at %p\n",hello[i],&hello[i]); i++; } return(0); }
Output
H at 0x7ffee4038a15
e at 0x7ffee4038a16
l at 0x7ffee4038a17
l at 0x7ffee4038a18
o at 0x7ffee4038a19
! at 0x7ffee4038a1a
Notes
* As with other examples where the %p
placeholder is used, the addresses you see output will be different from what's shown here.
* The format &hello[n]
displays the address of element n in an array.
* The &
operator isn't necessary when referring to an array as a whole because an array name by itself, such as hello
, represents the array's base address in memory. In that case, &hello
is redundant. It may not cause any warnings, but it's unnecessary and potentially confusing. This topic is addressed in Chapter 19.
Copyright © 1997-2024 by QPBC.
All rights reserved