Reputation: 26317
I'm getting the wrong letters printed when I run a function like this:
#include <stdio.h>
void getletters(char *one, char *two) {
scanf("%c %c",&one, &two);
/* if i print one and two here, they are correct). */
}
int main(void) {
char one, two;
getinput(&one, &two);
char *pone = &one;
char *ptwo = &two;
printf("your letters are %c and %c", *pone, *ptwo); /* These are coming out as the wrong characters */
}
Do I have incorrect syntax? Am I using pointers incorrectly?
Upvotes: 1
Views: 128
Reputation: 988
scanf("%c %c",&one, &two");
What type of argument does the conversion specifier %c
expect? Compare this to the type of &one
and &two
.
Upvotes: 0
Reputation: 10415
Try this.
#include <stdio.h>
void getletters(char *one, char *two) {
scanf("%c %c",one, two);
}
int main (int argc, char const* argv[])
{
char one, two;
getletters(&one, &two);
printf("your letters are %c and %c\n",one, two); /* These are coming out as the wrong characters */
return 0;
}
Upvotes: 0
Reputation: 5539
Try this
void getletters(char *one, char *two) {
scanf("%c %c",one, two);
/* if i pint one and two here, they are correct). */
}
one and two are already pointers in getLetters so no need to pass their address to scanf
Moreover in main, you are trying to pass pointers to printf function this is wrong you have to pass arguments by values:
printf("your letters are %c and %c", *pone, *ptwo);
Upvotes: 0
Reputation: 385224
void getletters(char *one, char *two) {
scanf("%c %c",&one, &two");
}
You accept pointers-to-char, then use the &
operator to get pointers-to-pointers-to-char, then provide them to a function expecting pointers-to-char.
You also have a typo in it.
Instead, write:
void getletters(char *one, char *two) {
scanf("%c %c",one, two);
}
Upvotes: 1
Reputation: 564531
In your scanf function, you don't need to take the address of your variables. Try:
void getletters(char *one, char *two) {
scanf("%c %c", one, two); // one and two are already pointers...
}
Upvotes: 1