I've created a struct
to group two pointer variables so I can store the address on those. Then I'm instantiating the struct and referring to the two variables from the main
contest.
Comparing the two strings based on useroutput and return the desired result.
The code is here
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <ctype.h>
typedef struct
{
char *name;
char *name2;
} names;
int main()
{
names nm;
printf("Please enter first string: ");
scanf("%s", nm.name)
printf("Please enter second string: ");
scanf("%s", nm.name2);
if(strcmp(nm.name, nm.name2) == 0)
printf("Strings %s and %s do match\n", nm.name, nm.name2);
else
printf("Strings %s and %s do not match\n", nm.name, nm.name2);
return 0;
}
I've tried to printf("%s", &nm.name);
I am a novice C user. Thanks!
Example that works without struct
and pointer variables
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <ctype.h>
int main()
{
char name[20];
printf("Please enter first string: ");
scanf("%s", &name);
char name2[20];
printf("Please enter second string: ");
scanf("%s", &name2);
if(strcmp(name, name2) == 0)
printf("Strings %s and %s do match\n", name, name2);
else
printf("Strings %s and %s do not match\n", name, name2);
return 0;
}
gcc -Wall -Wextra -Wformat -pedantic -o strcmp strcmp.c
strcmp.c: In function ‘main’:
strcmp.c:10:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Wformat]
strcmp.c:11:2: warning: ISO C90 forbids mixed declarations and code [-pedantic]
strcmp.c:13:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Wformat]
strcmp.c:15:2: warning: implicit declaration of function ‘strcmp’ [-Wimplicit-function-declaration]