I found the following sample code, demonstrating the use of sscanf()
on TutorialPoints:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int day, year;
char weekday[20], month[20], dtm[100];
strcpy( dtm, "Saturday March 25 1989" );
sscanf( dtm, "%s %s %d %d", weekday, month, &day, &year );
printf("%s %d, %d = %s\n", month, day, year, weekday );
return(0);
}
I'm confused about the use of the ampersand in &day
, &year
.
From what I've read, the ampersand is used in this manner on a pointer variable in order to say "don't change where the pointer is pointing to but rather change the value at the address the pointer is already pointing to".
Here it's funny though, because day
and year
are not pointers. They are just simple ints
. Why can't it be written as:
...
sscanf( dtm, "%s %s %d %d", weekday, month, day, year );
...
?
you misunderstand.
&
gets the address of something, i.e.&date
returns a pointer to date.