linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* scanf with pointers as arguments
@ 2004-11-17  5:50 Krishna Mohan
  2004-11-17  6:20 ` Benjamin Sobotta
  0 siblings, 1 reply; 3+ messages in thread
From: Krishna Mohan @ 2004-11-17  5:50 UTC (permalink / raw)
  To: Linux c programming list

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);




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: scanf with pointers as arguments
  2004-11-17  5:50 scanf with pointers as arguments Krishna Mohan
@ 2004-11-17  6:20 ` Benjamin Sobotta
  2004-11-17  8:56   ` Krishna Mohan
  0 siblings, 1 reply; 3+ messages in thread
From: Benjamin Sobotta @ 2004-11-17  6:20 UTC (permalink / raw)
  To: Krishna Mohan; +Cc: Linux c programming list

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;
}



On Wednesday 17 November 2004 06:50, Krishna Mohan wrote:
> 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 majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: scanf with pointers as arguments
  2004-11-17  6:20 ` Benjamin Sobotta
@ 2004-11-17  8:56   ` Krishna Mohan
  0 siblings, 0 replies; 3+ messages in thread
From: Krishna Mohan @ 2004-11-17  8:56 UTC (permalink / raw)
  To: Benjamin Sobotta; +Cc: Linux c programming list

Hi
thanqs alot benjamin sobotta.
it's working fine. i need to give exact pointer type.
Regards
krishna mohan

On Wed, 2004-11-17 at 11:50, Benjamin Sobotta wrote:
> 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;
> }
> 
> 
> 
> On Wednesday 17 November 2004 06:50, Krishna Mohan wrote:
> > 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 majordomo@vger.kernel.org
> > More majordomo info at  http://vger.kernel.org/majordomo-info.html



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-11-17  8:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-11-17  5:50 scanf with pointers as arguments Krishna Mohan
2004-11-17  6:20 ` Benjamin Sobotta
2004-11-17  8:56   ` Krishna Mohan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).