Solution for Exercise 06_08-howdy.c
06_08-howdy.c
#include <stdio.h>
int main()
{
char string[] = "Hello there, output!\n";
char *s;
int offset;
/* initialize the pointer */
s = string;
offset = 0;
while( *(s+offset) != '\0' )
{
putchar( *(s+offset) );
offset++;
}
return 0;
}
Output
Hello there, output!
Notes
* The stdlib.h header file isn't required because the malloc() function isn't used in this code.
* The string is stored in array string[]. The string's contents used in your solution can be anything.
* The code uses char pointer s to track characters within the string.
* Variable offset is used to make this code work like accessing elements in an array. Technically, this variable isn't necessary. Because pointer s is initialized to string[], it can always be re-initialized; you have no risk of losing it. Therefore, offset is unnecessary, but required as part of the exercise to make this code work more like accessing elements in an array.
* Pointer s is initialized to the start of the string: s = string Brackets aren't required as the reference is to the entire string, not a single element.
* The similarity between array notation and pointer expression appears as the while loop's condition: *(s+offset) != '\0' The value of offset is incremented within the loop, which processes each location in the string.
* A similar format is used in the putchar() function: *(s+offset) This type of format is akin to string[offset]
* It's okay if you didn't use an offset-like variable in your solution. My point is to show the two formats as similar, which helps to understand how pointers are used to access information within a buffer.
Copyright © 1997-2025 by QPBC.
All rights reserved
