Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #73470 > unrolled thread

Array/Pointer.

Started byMehdiAmini <atorrses@gmail.com>
First post2015-10-21 10:39 +0000
Last post2015-10-24 13:13 -0700
Articles 6 — 5 participants

Back to article view | Back to comp.lang.c


Contents

  Array/Pointer. MehdiAmini <atorrses@gmail.com> - 2015-10-21 10:39 +0000
    Re: Array/Pointer. Richard Heathfield <rjh@cpax.org.uk> - 2015-10-21 12:16 +0100
    Re: Array/Pointer. James Kuyper <jameskuyper@verizon.net> - 2015-10-21 12:15 -0400
      Re: Array/Pointer. MehdiAmini <atorrses@gmail.com> - 2015-10-23 11:30 +0000
        Re: Array/Pointer. raltbos@xs4all.nl (Richard Bos) - 2015-10-24 15:47 +0000
          Re: Array/Pointer. Keith Thompson <kst-u@mib.org> - 2015-10-24 13:13 -0700

#73470 — Array/Pointer.

FromMehdiAmini <atorrses@gmail.com>
Date2015-10-21 10:39 +0000
SubjectArray/Pointer.
Message-ID<n07q16$tn6$1@dont-email.me>
// The following code is based on a C code in a site.
// Does this code work as I expect, since online compilers give contradictory results.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int data[6]={0};
    int i=0;
   
    printf("Enter elements: \n");
    for(i=0;i<5;i++)
        scanf("%d",data+i);
    printf("You entered: \n");
    for(i=0;i<5;i++)
        printf("%d\n",*(data+i));
    return EXIT_SUCCESS;
}


-- 
--------------------------------- --- -- -
Posted with NewsLeecher v7.0 Beta 7
Web @ http://www.newsleecher.com/?usenet
------------------- ----- ---- -- -

[toc] | [next] | [standalone]


#73473

FromRichard Heathfield <rjh@cpax.org.uk>
Date2015-10-21 12:16 +0100
Message-ID<n07s2j$66q$1@dont-email.me>
In reply to#73470
On 21/10/15 11:39, MehdiAmini wrote:
> // The following code is based on a C code in a site.
> // Does this code work as I expect, since online compilers give contradictory results.

It depends on what you expect. If you expect that, as long as the user 
plays along and doesn't try to enter illegal values, the program will 
accept five ints and then display them, then yes, it does what you expect.

If you expect something else, then no, it probably doesn't do what you 
expect.

The code is perfectly ordinary C, and there's nothing odd about it. 
Apart from the failure to check the return value of scanf and the fact 
that your array is very slightly larger than it needs to be, it's even 
fairly good code.

What objections do some of these online compilers have to the code?

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

[toc] | [prev] | [next] | [standalone]


#73496

FromJames Kuyper <jameskuyper@verizon.net>
Date2015-10-21 12:15 -0400
Message-ID<n08die$c7t$1@dont-email.me>
In reply to#73470
On 10/21/2015 06:39 AM, MehdiAmini wrote:
> // The following code is based on a C code in a site.
> // Does this code work as I expect, since online compilers give contradictory results.
> 
> #include <stdio.h>
> #include <stdlib.h>
> 
> int main(void)
> {
>     int data[6]={0};
>     int i=0;
>    
>     printf("Enter elements: \n");
>     for(i=0;i<5;i++)
>         scanf("%d",data+i);

"The scanf function returns the value of the macro EOF if an input
failure occurs before the first conversion (if any) has completed.
Otherwise, the scanf function returns the number of input items
assigned, which can be fewer than provided for, or even zero, in
the event of an early matching failure." (7.21.6.4p3)

Thus, if everything goes right, scanf() should return 1. You need to
decide what you want your program to do if it returns EOF or 0 - and
then add the code needed to make sure that happens.
-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


#73613

FromMehdiAmini <atorrses@gmail.com>
Date2015-10-23 11:30 +0000
Message-ID<n0d5on$uk6$2@dont-email.me>
In reply to#73496
In reply to "James Kuyper" who wrote the following:

> On 10/21/2015 06:39 AM, MehdiAmini wrote:
> > // The following code is based on a C code in a site.
> > // Does this code work as I expect, since online compilers give 
> > contradictory results.
> > 
> > #include <stdio.h>
> > #include <stdlib.h>
> > 
> > int main(void)
> > {
> >     int data[6]={0};
> >     int i=0;
> > 
> >     printf("Enter elements: \n");
> >     for(i=0;i<5;i++)
> >         scanf("%d",data+i);
> 
> "The scanf function returns the value of the macro EOF if an input
> failure occurs before the first conversion (if any) has completed.
> Otherwise, the scanf function returns the number of input items
> assigned, which can be fewer than provided for, or even zero, in
> the event of an early matching failure." (7.21.6.4p3)
> 
> Thus, if everything goes right, scanf() should return 1. You need to
> decide what you want your program to do if it returns EOF or 0 - and
> then add the code needed to make sure that happens.
> --
> James Kuyper

The following code considers the output of scanf:


#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

void my_err2(const char* const message) 
{ 
    assert(message!=NULL); 
    fprintf(stderr, "%s\n", message); 
    exit(EXIT_FAILURE); 
}

int main(void)
{
    int data[6]={0};
    int i=0;
    int r;
   
    printf("Enter elements: \n");
    for(i=0;i<5;i++)
    {
        r=scanf("%d",data+i);
	 if(r!=1)
	     my_err2("scanf failed\n");
    }
    printf("You entered: \n");
    for(i=0;i<5;i++)
        printf("%d\n",*(data+i));
    return EXIT_SUCCESS;
}


-- 
--------------------------------- --- -- -
Posted with NewsLeecher v7.0 Beta 7
Web @ http://www.newsleecher.com/?usenet
------------------- ----- ---- -- -

[toc] | [prev] | [next] | [standalone]


#73696

Fromraltbos@xs4all.nl (Richard Bos)
Date2015-10-24 15:47 +0000
Message-ID<562ba70f.9880125@news.xs4all.nl>
In reply to#73613
MehdiAmini <atorrses@gmail.com> wrote:

> In reply to "James Kuyper" who wrote the following:
> 
> > Thus, if everything goes right, scanf() should return 1. You need to
> > decide what you want your program to do if it returns EOF or 0 - and
> > then add the code needed to make sure that happens.
> 
> The following code considers the output of scanf:

Yes, it does. And? Did you have a question about it? Is there anything
wrong with the results you're getting?
AFAICT your code is beginner's code, but correct as far as it goes.
There are several things you can do to make it more foolproof, and
certainly more user-friendly, but it does what it sets out to do, and
doesn't contain any obvious bugs - severe limitations, yes, but no
outright bugs. So... what do you expect us to say about it?

> -- 
> --------------------------------- --- -- -
> Posted with NewsLeecher v7.0 Beta 7
> Web @ http://www.newsleecher.com/?usenet
> ------------------- ----- ---- -- -

Blimey, a web-to-usenet interface that apparently _doesn't_ suck very
large chunks through a very thin straw! It quotes correctly, it even
gets the sig-sep correct, _and_ it doesn't force quoted-illegible... all
Goggle Gropes, and _particularly_ Windows Dead Mail users should very
strongly consider switching to this one.

Richard

[toc] | [prev] | [next] | [standalone]


#73733

FromKeith Thompson <kst-u@mib.org>
Date2015-10-24 13:13 -0700
Message-ID<lnfv10t483.fsf@kst-u.example.com>
In reply to#73696
raltbos@xs4all.nl (Richard Bos) writes:
> MehdiAmini <atorrses@gmail.com> wrote:
[...]
>> -- 
>> --------------------------------- --- -- -
>> Posted with NewsLeecher v7.0 Beta 7
>> Web @ http://www.newsleecher.com/?usenet
>> ------------------- ----- ---- -- -
>
> Blimey, a web-to-usenet interface that apparently _doesn't_ suck very
> large chunks through a very thin straw! It quotes correctly, it even
> gets the sig-sep correct, _and_ it doesn't force quoted-illegible... all
> Goggle Gropes, and _particularly_ Windows Dead Mail users should very
> strongly consider switching to this one.

As far as I can tell, it's not a web-to-Usenet interface.  It's a
(Windows-only) Usenet client application.  The provider also sells
access to a Usenet server.  I think the (free) client can also be used
with other Usenet servers.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web