scanf with pointers as arguments
Hi
Can anybody plz tell me what's wrong with this code!
I am getting SEGMENTATION FAULT. ofcourse, if i use &i, &s as arguments in scanf, it's work fine as usual. can't i use pointers which hold the address of i and s. plz clarify this. THANKS IN ADVANCE.
int i,*iptr = &i;
short s,*shptr = &s;
printf("enter 2 values:\n");
scanf("%d %d",iptr,shptr);
printf("%d %d\n",*iptr,*shptr);
printf("%d %d\n",i,s);Hi!
Will the problem is not the pointer but the type. Since you use short you have to adjust that.
That works for me:
#include<stdio.h>
int main()
{
int i,*iptr = &i;
short int s,*shptr = &s;
printf("enter 2 values:\n");
scanf("%d %hd",iptr,shptr);
printf("%d %hd\n",*iptr,*shptr);
printf("%d %hd\n",i,s); return 0;
}
Quoted 19 lines · Krishna Mohan
Hi
Can anybody plz tell me what's wrong with this code!
I am getting SEGMENTATION FAULT. ofcourse, if i use &i, &s as arguments in scanf, it's work fine as usual. can't i use pointers which hold the address of i and s. plz clarify this. THANKS IN ADVANCE.int i,*iptr = &i; short s,*shptr = &s; printf("enter 2 values:\n"); scanf("%d %d",iptr,shptr); printf("%d %d\n",*iptr,*shptr);
printf("%d %d\n",i,s);-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Hi
thanqs alot benjamin sobotta.
it's working fine. i need to give exact pointer type. Regards
krishna mohan
Quoted 42 lines · Benjamin Sobotta
Hi!
Will the problem is not the pointer but the type. Since you use short you have to adjust that.
That works for me:#include<stdio.h>
int
main()
{int i,*iptr = &i; short int s,*shptr = &s; printf("enter 2 values:\n"); scanf("%d %hd",iptr,shptr); printf("%d %hd\n",*iptr,*shptr); printf("%d %hd\n",i,s);return 0;
}Quoted 19 lines · Krishna Mohan
Hi
Can anybody plz tell me what's wrong with this code!
I am getting SEGMENTATION FAULT. ofcourse, if i use &i, &s as arguments in scanf, it's work fine as usual. can't i use pointers which hold the address of i and s. plz clarify this. THANKS IN ADVANCE.int i,*iptr = &i; short s,*shptr = &s; printf("enter 2 values:\n"); scanf("%d %d",iptr,shptr); printf("%d %d\n",*iptr,*shptr);
printf("%d %d\n",i,s);-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html