Quantcast

is this mingw error?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

is this mingw error?

Chan Oak
here source code

#include <stdio.h>
#define MAX 100
char aaa[MAX];
int addstr(char a)
{
    static int cur = 0;
    if (cur > MAX)
        return 0;
    else
        aaa[cur] = a;
   
    return 1;
}
int main()
{
    char a;
    int i;
   
    while ((a = getchar()) != EOF)
    {
        addstr(a);
    }
    printf("%c", aaa[0]);
    printf("hello\n");
}

here is output

a
^Z
hello
the global array aaa[0] is modified yet it does not print aaa[0].
if I put printf inside function addstr to print aaa[0], it does print

is this compiler bug?

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: is this mingw error?

Emanuel Falkenauer
Hi Chan,

I think that first of all, you should be aware that the people who create compilers are extremely serious and, therefore, compiler errors are extremely rare. To give you a sense of proportion, in 30+ years of very heavy programming (i.e. millions of lines of code), I have really identified just FOUR TRUE compiler bugs (which have all been promptly corrected in the next release) - all the rest were my own bugs. In short: the first thing to check is definitely your own code.

Here are some pointers to look at:
- From your example it's not clear WHAT the aaa array should actually contain, as you don't specify what the getchar() actually gets: is it the char 'a' (i.e. the one actually printed!) or not? Please do not confuse a (the variable) with 'a' (the ASCII char constant)
- Furthermore, you never increment the "cur" variable in addstr(), which means that each new char input will simply overwrite aaa[0] - i.e. it's not ADDstr: it's really SETstr0
- Finally, if you actually were incrementing cur in addstr (which you aren't), it would eventually reach MAX, which would overflow the aaa[MAX] array (because it only goes up to [MAX-1]), causing all sorts of unpredictable mess.

I really don't want to offend you, and I actually applaud your enthusiasm... but it looks like some really basic C issues still elude you.

Best,

Emanuel


On 27-Feb-17 05:24, Chan Oak wrote:
here source code

#include <stdio.h>
#define MAX 100
char aaa[MAX];
int addstr(char a)
{
    static int cur = 0;
    if (cur > MAX)
        return 0;
    else
        aaa[cur] = a;
   
    return 1;
}
int main()
{
    char a;
    int i;
   
    while ((a = getchar()) != EOF)
    {
        addstr(a);
    }
    printf("%c", aaa[0]);
    printf("hello\n");
}

here is output

a
^Z
hello
the global array aaa[0] is modified yet it does not print aaa[0].
if I put printf inside function addstr to print aaa[0], it does print

is this compiler bug?


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot


_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at 
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: [hidden email]


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: is this mingw error?

Peter Rosin
In reply to this post by Chan Oak


On 2017-02-27 05:24, Chan Oak wrote:

> here source code
>
> #include <stdio.h>
> #define MAX 100
> char aaa[MAX];
> int addstr(char a)
> {
>     static int cur = 0;
>     if (cur > MAX)
>         return 0;
>     else
>         aaa[cur] = a;
>    
>     return 1;
> }
> int main()
> {
>     char a;
>     int i;
>    
>     while ((a = getchar()) != EOF)
>     {
>         addstr(a);
>     }
>     printf("%c", aaa[0]);
>     printf("hello\n");
> }
>
> here is output
>
> a
> ^Z
> hello
> the global array aaa[0] is modified yet it does not print aaa[0].
> if I put printf inside function addstr to print aaa[0], it does print
>
> is this compiler bug?

I suspect that aaa[0] contains a newline after the while loop and that
this is why you do not see what is actually printed before hello.

Try this instead.

#include <stdio.h>

char aaa[100];

int addstr(char a)
{
        static int cur;

        if (cur >= sizeof(aaa) / sizeof(aaa[0]))
                return 0;

        aaa[cur++] = a;
        return 1;
}

int main(void)
{
        char a;
   
        while ((a = getchar()) != EOF)
        {
                addstr(a);
        }

        printf("%s", aaa);
        printf("hello\n");

        return 0;
}

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: is this mingw error?

KHMan
In reply to this post by Chan Oak
On 2/27/2017 12:24 PM, Chan Oak wrote:

> here source code
>
> #include <stdio.h>
> #define MAX 100
> char aaa[MAX];
> int addstr(char a)
> {
>      static int cur = 0;
>      if (cur > MAX)
>          return 0;
>      else
>          aaa[cur] = a;
>
>      return 1;
> }
> int main()
> {
>      char a;
>      int i;
>
>      while ((a = getchar()) != EOF)
>      {
>          addstr(a);
>      }
>      printf("%c", aaa[0]);
>      printf("hello\n");
> }
>
> here is output
>
> a
> ^Z
> hello
> the global array aaa[0] is modified yet it does not print aaa[0].
> if I put printf inside function addstr to print aaa[0], it does print

In addstr(), cur is not much of a use unless you increment it.
aaa[0] ends up with the last character before the ^Z

Try:
====
   printf("%c%d", aaa[0], aaa[0]);

Modified Output:
================
a
^Z

10hello

Windows command prompt does line-by-line stdin. So the last
character before the end-of-input ^Z is a newline, which should be
a CRLF or an LF (apparently I got LF only, so LF it is). The last
aaa[0] will be LF which is ASCII 10, as shown above. The empty
line after ^Z is the output of %c (which is LF), the 10 is the
output of %d (ASCII value of LF).

--
Cheers,
Kein-Hong Man (esq.)
Selangor, Malaysia


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: is this mingw error?

Foster Douglas-Ellert
In reply to this post by Chan Oak
Hello there!

You overwrite the first letter every time. The last thing in there is EOF, I think. So that one is printed out. Increment "cur" somewhere. Increment is "cur++" or "++cur" or "cur +=1;" or "cur = cur + 1;".

I hope that helps.

I'm not very good in english, interpersonal stuff might sound a bit clumsy.

Regards
Foster


2017-02-27 5:24 GMT+01:00 Chan Oak <[hidden email]>:
here source code

#include <stdio.h>
#define MAX 100
char aaa[MAX];
int addstr(char a)
{
    static int cur = 0;
    if (cur > MAX)
        return 0;
    else
        aaa[cur] = a;
   
    return 1;
}
int main()
{
    char a;
    int i;
   
    while ((a = getchar()) != EOF)
    {
        addstr(a);
    }
    printf("%c", aaa[0]);
    printf("hello\n");
}

here is output

a
^Z
hello
the global array aaa[0] is modified yet it does not print aaa[0].
if I put printf inside function addstr to print aaa[0], it does print

is this compiler bug?

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: is this mingw error?

Brian Rayne
If you're going to print aaa[] as a string, you'll need to make sure it is properly terminated.  You can have addstr() bounds check against the array's size before appending the input character and then afterward, append a NULL character to kill two birds with one stone.

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: is this mingw error?

Chan Oak
In reply to this post by KHMan
it is ascii 10, linefeed.  I got it.  apparently by looking at it, thought it should print 'a'
instead the last character the getchar gets was newline.  I should investigate more before asking but I appreciate all of your replies. 

Thanks!.

2017-02-27 3:27 GMT-05:00 KHMan <[hidden email]>:
On 2/27/2017 12:24 PM, Chan Oak wrote:
> here source code
>
> #include <stdio.h>
> #define MAX 100
> char aaa[MAX];
> int addstr(char a)
> {
>      static int cur = 0;
>      if (cur > MAX)
>          return 0;
>      else
>          aaa[cur] = a;
>
>      return 1;
> }
> int main()
> {
>      char a;
>      int i;
>
>      while ((a = getchar()) != EOF)
>      {
>          addstr(a);
>      }
>      printf("%c", aaa[0]);
>      printf("hello\n");
> }
>
> here is output
>
> a
> ^Z
> hello
> the global array aaa[0] is modified yet it does not print aaa[0].
> if I put printf inside function addstr to print aaa[0], it does print

In addstr(), cur is not much of a use unless you increment it.
aaa[0] ends up with the last character before the ^Z

Try:
====
   printf("%c%d", aaa[0], aaa[0]);

Modified Output:
================
a
^Z

10hello

Windows command prompt does line-by-line stdin. So the last
character before the end-of-input ^Z is a newline, which should be
a CRLF or an LF (apparently I got LF only, so LF it is). The last
aaa[0] will be LF which is ASCII 10, as shown above. The empty
line after ^Z is the output of %c (which is LF), the 10 is the
output of %d (ASCII value of LF).

--
Cheers,
Kein-Hong Man (esq.)
Selangor, Malaysia


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Loading...