ansi c

Discussion in 'C Programming' started by Richard Bos, Jan 23, 2008.

  1. Richard Bos

    Richard Bos Guest

    wrote:

    > #include stio.h
    > struct{int integer; char caracter;} realstruct;
    > int main()
    > {
    > realstruct.integer = 1;
    > realstruct.caracter = "someword";
    > }
    > printf("%d %c", realstruct.integer, realstruct.caracter)
    > system("pause");
    >
    > The problem is the output of printf realstruct.caracter, i see a
    > strange character in a command prompt, i tried also to use %s but i
    > got compiler error.
    > Have you some advice?


    Yes. Post real code. Don't retype - copy and paste, including any
    compiler messages. There are several things that _could_ be wrong with
    your code (most likely being missing indirections or the wrong output
    specifier), but as it stands, it cannot possibly compile let alone give
    output, and it is impossible to determine which of the various possible
    errors in your code is the real one.

    Richard
     
    Richard Bos, Jan 23, 2008
    #1
    1. Advertising

  2. Richard Bos

    Guest

    Dear all.
    i have a little problem with this code, i use visual studio2005 (ansi
    c)

    #include stio.h
    struct{int integer; char caracter;} realstruct;
    int main()
    {
    realstruct.integer = 1;
    realstruct.caracter = "someword";
    }
    printf("%d %c", realstruct.integer, realstruct.caracter)
    system("pause");

    The problem is the output of printf realstruct.caracter, i see a
    strange character in a command prompt, i tried also to use %s but i
    got compiler error.
    Have you some advice?
    thanks a lot
     
    , Jan 23, 2008
    #2
    1. Advertising

  3. Richard Bos

    Mark Bluemel Guest

    wrote:
    > Dear all.
    > i have a little problem with this code, i use visual studio2005 (ansi
    > c)
    >
    > #include stio.h


    Do mean "stdio.h"? You should really use cut and paste to post your real
    code...

    > struct{int integer; char caracter;} realstruct;


    So the structure called realstruct can contain an integer and a single
    character...

    > int main()
    > {
    > realstruct.integer = 1;


    You successfully assigned an integer into the structure.

    > realstruct.caracter = "someword";


    is "someword" a single character?

    No.

    Then it won't be dealt with properly will it? What will happen is that
    the pointer ("someword" in this context will be evaluated as a pointer)
    will be converted to an integer value (in some manner) and that value
    assigned into the char "caracter".

    Didn't the compiler give you any warnings?

    > }
    > printf("%d %c", realstruct.integer, realstruct.caracter)
    > system("pause");
    >
    > The problem is the output of printf realstruct.caracter, i see a
    > strange character in a command prompt, i tried also to use %s but i
    > got compiler error.


    If you wanted to use "%s" you'd need "caracter" to be a pointer or an
    array of characters.

    > Have you some advice?


    Try using a better textbook?
     
    Mark Bluemel, Jan 23, 2008
    #3
  4. wrote:
    > Dear all.
    > i have a little problem with this code, i use visual studio2005 (ansi
    > c)


    Note that Visual Studio 2005 (and 2008) roughly support ISO C89 and
    don't include much of the newer available features and data types.

    >
    > #include stio.h
    > struct{int integer; char caracter;} realstruct;


    I assume this "caracter" field is an array?

    > int main()
    > {
    > realstruct.integer = 1;
    > realstruct.caracter = "someword";


    Where is the declaration of the local variable? And you are putting a
    string into a single char.

    > }
    > printf("%d %c", realstruct.integer, realstruct.caracter)
    > system("pause");


    Shouldn't this be inside main()?

    >
    > The problem is the output of printf realstruct.caracter, i see a
    > strange character in a command prompt, i tried also to use %s but i
    > got compiler error.
    > Have you some advice?
    > thanks a lot
    >
    >


    Since this code is wrong at so many levels and does not compile like
    this we cannot give accurate answers, so please provide the correct code.

    - Jensen
     
    Jensen Somers, Jan 23, 2008
    #4
  5. Richard Bos

    Guest

    On 23 Gen, 17:11, Jensen Somers <> wrote:
    > wrote:
    > > Dear all.
    > > i have a little problem with this code, i use visual studio2005 (ansi
    > > c)

    >
    > Note that Visual Studio 2005 (and 2008) roughly support ISO C89 and
    > don't include much of the newer available features and data types.
    >
    >
    >
    > > #include stio.h
    > > struct{int integer; char caracter;} realstruct;

    >
    > I assume this "caracter" field is an array?
    >
    > > int main()
    > > {
    > >    realstruct.integer = 1;
    > >    realstruct.caracter = "someword";

    >
    > Where is the declaration of the local variable? And you are putting a
    > string into a single char.
    >
    > > }
    > > printf("%d %c", realstruct.integer, realstruct.caracter)
    > > system("pause");

    >
    > Shouldn't this be inside main()?
    >
    >
    >
    > > The problem is the output of printf realstruct.caracter, i see a
    > > strange character in a command prompt, i tried also to use %s but i
    > > got compiler error.
    > > Have you some advice?
    > > thanks a lot

    >
    > Since this code is wrong at so many levels and does not compile like
    > this we cannot give accurate answers, so please provide the correct code.
    >
    > - Jensen


    #include <stdio.h>

    struct tagstruttura{
    int serie;
    char organizzatore;
    int partecipanti;
    } struttura;

    int main()
    {

    struttura.serie = 2;
    struttura.organizzatore = "cazzo";
    struttura.partecipanti = 100;
    printf("%d\n %c\n %d\n %X\n",
    struttura.serie,struttura.organizzatore,struttura.partecipanti);
    system("pause\n");


    I don't got any warnings in my compiler.
    thanks a lot
     
    , Jan 23, 2008
    #5
  6. Richard Bos

    Guest

    On 23 Gen, 17:37, wrote:
    > On 23 Gen, 17:11, Jensen Somers <> wrote:
    >
    >
    >
    >
    >
    > > wrote:
    > > > Dear all.
    > > > i have a little problem with this code, i use visual studio2005 (ansi
    > > > c)

    >
    > > Note that Visual Studio 2005 (and 2008) roughly support ISO C89 and
    > > don't include much of the newer available features and data types.

    >
    > > > #include stio.h
    > > > struct{int integer; char caracter;} realstruct;

    >
    > > I assume this "caracter" field is an array?

    >
    > > > int main()
    > > > {
    > > >    realstruct.integer = 1;
    > > >    realstruct.caracter = "someword";

    >
    > > Where is the declaration of the local variable? And you are putting a
    > > string into a single char.

    >
    > > > }
    > > > printf("%d %c", realstruct.integer, realstruct.caracter)
    > > > system("pause");

    >
    > > Shouldn't this be inside main()?

    >
    > > > The problem is the output of printf realstruct.caracter, i see a
    > > > strange character in a command prompt, i tried also to use %s but i
    > > > got compiler error.
    > > > Have you some advice?
    > > > thanks a lot

    >
    > > Since this code is wrong at so many levels and does not compile like
    > > this we cannot give accurate answers, so please provide the correct code..

    >
    > > - Jensen

    >
    > #include <stdio.h>
    >
    >         struct tagstruttura{
    >                             int serie;
    >                             char organizzatore;
    >                             int partecipanti;
    >                           } struttura;
    >
    > int main()
    > {
    >
    >         struttura.serie = 2;
    >         struttura.organizzatore = "cazzo";
    >         struttura.partecipanti = 100;
    >         printf("%d\n %c\n %d\n %X\n",
    > struttura.serie,struttura.organizzatore,struttura.partecipanti);
    >         system("pause\n");
    >
    > I don't got any warnings in my compiler.
    > thanks a lot- Nascondi testo tra virgolette -
    >
    > - Mostra testo tra virgolette -


    I forgot........
    if i change %c in %s tha compiler give me this Unhandled exception at
    0x10227c2f (msvcr80d.dll) in testc.exe: 0xC0000005: Access violation
    reading location 0x00000060.

    thanks
     
    , Jan 23, 2008
    #6
  7. Richard Bos

    Mark Bluemel Guest

    wrote:
    > #include <stdio.h>
    >
    > struct tagstruttura{
    > int serie;
    > char organizzatore;


    "organizzatore" has storage for a single character.

    > int partecipanti;
    > } struttura;
    >
    > int main()
    > {
    >
    > struttura.serie = 2;
    > struttura.organizzatore = "cazzo";


    What makes you think this is valid? What text book are you referring to?

    > struttura.partecipanti = 100;
    > printf("%d\n %c\n %d\n %X\n",
    > struttura.serie,struttura.organizzatore,struttura.partecipanti);


    What do you expect to come out? Why?

    Where is the value which the "%X" is intended to format?

    For a quick fix, which may not be what you really want, try :-

    #include <stdio.h>

    struct tagstruttura{
    int serie;
    char *organizzatore; /* note the '*' */
    int partecipanti;
    } struttura;

    int main()
    {

    struttura.serie = 2;
    struttura.organizzatore = "cazzo";
    struttura.partecipanti = 100;
    printf("%d\n %s\n %d\n", /* note "%s" not "%c"
    * and I've removed "%X"
    */
    struttura.serie,struttura.organizzatore,struttura.partecipanti);
    system("pause\n");
    }
     
    Mark Bluemel, Jan 23, 2008
    #7
  8. Richard Bos

    Guest

    On 23 Gen, 17:44, Mark Bluemel <> wrote:
    > wrote:
    > > #include <stdio.h>

    >
    > >    struct tagstruttura{
    > >                        int serie;
    > >                        char organizzatore;

    >
    > "organizzatore" has storage for a single character.
    >
    > >                        int partecipanti;
    > >                      } struttura;

    >
    > > int main()
    > > {

    >
    > >    struttura.serie = 2;
    > >    struttura.organizzatore = "cazzo";

    >
    > What makes you think this is valid? What text book are you referring to?
    >
    > >    struttura.partecipanti = 100;
    > >    printf("%d\n %c\n %d\n %X\n",
    > > struttura.serie,struttura.organizzatore,struttura.partecipanti);

    >
    > What do you expect to come out? Why?
    >
    > Where is the value which the "%X" is intended to format?
    >
    > For a quick fix, which may not be what you really want, try :-
    >
    > #include <stdio.h>
    >
    >         struct tagstruttura{
    >                             int serie;
    >                             char *organizzatore; /* note the '*' */
    >                             int partecipanti;
    >                           } struttura;
    >
    > int main()
    > {
    >
    >         struttura.serie = 2;
    >         struttura.organizzatore = "cazzo";
    >         struttura.partecipanti = 100;
    >         printf("%d\n %s\n %d\n",      /* note "%s" not "%c"
    >                                         * and I've removed "%X"
    >                                         */
    > struttura.serie,struttura.organizzatore,struttura.partecipanti);
    >         system("pause\n");
    >
    >
    >
    > }- Nascondi testo tra virgolette -
    >
    > - Mostra testo tra virgolette -


    thanks, it works, but why i must use "*" pointer? i forgot to remove
    %X from my code ( i'm trying a lot of example in the same main() :)
    thanks again, i'm a newbye but c is wonderful........
     
    , Jan 23, 2008
    #8
  9. Jensen Somers wrote:
    > wrote:
    >> #include stio.h
    >> struct{int integer; char caracter;} realstruct;

    >
    > I assume this "caracter" field is an array?
    >
    >> int main()
    >> {
    >> realstruct.integer = 1;
    >> realstruct.caracter = "someword";

    >
    > Where is the declaration of the local variable?


    If you mean "Where is realstruct declared?", the answer is this line
    above main:

    >> struct{int integer; char caracter;} realstruct;
     
    Philip Potter, Jan 23, 2008
    #9
  10. Richard Bos

    Rachael Guest

    On 23 Jan, 16:58, wrote:

    > thanks, it works, but why i must use "*" pointer?


    Because the char type only stores single characters, like 'a' or 'b'.
    Pointer types store locations in memory.

    If you do
    char c = 'a';
    you are writing into the variable c the value of the character
    'a' (which is just a number).

    If you do
    char * pc = "cazzo";
    you are writing into the variable pc (which is a char pointer) the
    *location* of the beginning of the string "cazzo", which is stored in
    memory somewhere. You are not storing the whole string "cazzo" in pc,
    because no single variable (i.e. not an array) in C can store a whole
    string.

    (Note single quotes for characters and double quotes for string
    literals)
     
    Rachael, Jan 23, 2008
    #10
  11. Richard Bos

    santosh Guest

    wrote:

    > On 23 Gen, 17:44, Mark Bluemel <> wrote:
    >> wrote:
    >> > #include <stdio.h>

    >>
    >> > struct tagstruttura{
    >> > int serie;
    >> > char organizzatore;

    >>
    >> "organizzatore" has storage for a single character.
    >>
    >> > int partecipanti;
    >> > } struttura;

    >>
    >> > int main()
    >> > {

    >>
    >> > struttura.serie = 2;
    >> > struttura.organizzatore = "cazzo";

    >>
    >> What makes you think this is valid? What text book are you referring
    >> to?
    >>
    >> > struttura.partecipanti = 100;
    >> > printf("%d\n %c\n %d\n %X\n",
    >> > struttura.serie,struttura.organizzatore,struttura.partecipanti);

    >>
    >> What do you expect to come out? Why?
    >>
    >> Where is the value which the "%X" is intended to format?
    >>
    >> For a quick fix, which may not be what you really want, try :-
    >>
    >> #include <stdio.h>
    >>
    >> struct tagstruttura{
    >> int serie;
    >> char *organizzatore; /* note the '*' */
    >> int partecipanti;
    >> } struttura;
    >>
    >> int main()
    >> {
    >>
    >> struttura.serie = 2;
    >> struttura.organizzatore = "cazzo";
    >> struttura.partecipanti = 100;
    >> printf("%d\n %s\n %d\n",      /* note "%s" not "%c"
    >> * and I've removed "%X"
    >> */
    >> struttura.serie,struttura.organizzatore,struttura.partecipanti);
    >> system("pause\n");
    >>
    >>
    >>
    >> }- Nascondi testo tra virgolette -
    >>
    >> - Mostra testo tra virgolette -

    >
    > thanks, it works, but why i must use "*" pointer?


    Without the * 'organizzatore' is simply declared as a char. A char can
    only store a single character of the implementation's character set
    like say, 'v' or '<' etc.

    With the * 'organizzatore' is declared as an object of type char *,
    which can point to one or more characters. And that is exactly what the
    line:

    struttura.organizzatore = "cazzo";

    in main() does. It sets 'organizzatore' to point to an anonymous array
    of char which contains the string "cazzo".

    If these terms confuse you, then you need to read the first few chapters
    of whatever textbook you are using. Pointers, arrays and strings are
    quite subtle in C and a newsgroup is no place to adequately explain
    them.

    <snip>
     
    santosh, Jan 23, 2008
    #11
  12. Richard Bos

    santosh Guest

    Rachael wrote:

    > On 23 Jan, 16:58, wrote:
    >
    >> thanks, it works, but why i must use "*" pointer?

    >
    > Because the char type only stores single characters, like 'a' or 'b'.
    > Pointer types store locations in memory.
    >
    > If you do
    > char c = 'a';
    > you are writing into the variable c the value of the character
    > 'a' (which is just a number).
    >
    > If you do
    > char * pc = "cazzo";
    > you are writing into the variable pc (which is a char pointer) the
    > *location* of the beginning of the string "cazzo", which is stored in
    > memory somewhere. You are not storing the whole string "cazzo" in pc,
    > because no single variable (i.e. not an array) in C can store a whole
    > string.


    If you don't mind about portability or sanity you can stuff a small
    enough string into all the basic C types.

    <snip>
     
    santosh, Jan 23, 2008
    #12
  13. Richard Bos

    Guest

    In article <fn81gk$pn9$>,
    santosh <> wrote:

    >If you don't mind about portability or sanity you can stuff a small
    >enough string into all the basic C types.


    If your string is small enough, you can even do it portably (though the
    sanity barrier does stay intact).
    --------
    #undef NDEBUG
    #include <assert.h>
    #include <string.h>

    /*This typedef can refer to any complete type you like*/
    typedef double *my_type;

    int main(void)
    {
    my_type b;
    char *bogus_string;

    /*Icky pointless code, but completely legal and portable*/
    bogus_string=(char *)&b;
    strcpy(bogus_string,"");

    /*assert will never fire*/
    assert(strcmp(bogus_string,"")==0);

    return 0;
    }
    --------


    dave

    --
    Dave Vandervies dj3vande at eskimo dot com
    Correction: Number of *solicited* emails deleted: 29. You deleted everything
    you asked for. Here is an equivalent measure:
    rm /var/spool/mail/* --Richard Heathfield in comp.programming
     
    , Jan 23, 2008
    #13
  14. wrote:
    > Dear all.
    > i have a little problem with this code, i use visual studio2005 (ansi
    > c)


    I tremble at the thought that I am feeding a troll, since it is almost
    inconceivable that your 'code' could have been written in good faith.

    > #include stio.h

    ^^^^^^^^^^^^^^^^
    That line is, obviously, gibberish.

    > struct{int integer; char caracter;} realstruct;

    ^^^^^^^^^^^^^
    Here you specify that the 'caracter' member holds a single character.

    > int main()
    > {
    > realstruct.integer = 1;
    > realstruct.caracter = "someword";

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    Here you try to assign (a pointer to) a literal string into a single
    character. This is, rather obviously, a gross error.
    > }


    At this point, the closing '{' for main has been reached. Every
    statement after that brace is outside a function and an obvious error.

    > printf("%d %c", realstruct.integer, realstruct.caracter)


    Leaving off semicolons is not very smart.

    > system("pause");
    >
    > The problem is the output of printf realstruct.caracter,


    No, the problem is that you don't understand what a char is. Or even
    what a C program is. Or even what a programming language is.

    > i see a
    > strange character in a command prompt, i tried also to use %s but i
    > got compiler error.


    Be glad your compiler told you that a char is not a string. It didn't
    have to.

    > Have you some advice?


    a) Pay attention to the _contents_ of the diagnostic message, not just
    that you have one.
    b) Get an elementary C textbook and start reading at the first page.
    Programming is not something you do by typing random characters and then
    complaining when they don't result in a compilable program.

    Compare your code to

    [1]
    #include <stdio.h>

    struct
    {
    int integer;
    char *caracter;
    } realstruct;

    int main(void)
    {
    realstruct.integer = 1;
    realstruct.caracter = "someword";

    printf("%d %s\n", realstruct.integer, realstruct.caracter);
    return 0;
    }

    or
    [2]
    #include <stdio.h>

    struct test_struct
    {
    int integer;
    char *caracter;
    };

    int main(void)
    {
    struct test_struct realstruct = { 1, "someword" };
    printf("%d %s\n", realstruct.integer, realstruct.caracter);
    return 0;
    }

    or, if you have C99-style initializations, so you need know only the
    relevant member names
    [3]
    #include <stdio.h>

    struct test_struct
    {
    int integer;
    char *caracter;
    };

    int main(void)
    {
    struct test_struct realstruct = {.integer = 1,.caracter =
    "someword" };
    printf("%d %s\n", realstruct.integer, realstruct.caracter);
    return 0;
    }



    If you don't know the relevant string at compile time that you want
    caracter to point to, then you have more problems. But, frankly, you
    are nowhere close to being able to understand those problems, much less
    how to solve them.
     
    Martin Ambuhl, Jan 23, 2008
    #14
  15. Richard Bos

    Army1987 Guest

    yqyq22 wrote:

    > #include <stdio.h>
    >
    > struct tagstruttura{
    > int serie;
    > char organizzatore;
    > int partecipanti;
    > } struttura;
    >
    > int main()
    > {
    >
    > struttura.serie = 2;
    > struttura.organizzatore = "cazzo";

    What the f*** is being organized? Probably right that...
    > struttura.partecipanti = 100;

    Wow, it'll be a somewhat big orgy...

    (For non-Italian speakers, look up "cazzo" somewhere...)

    > printf("%d\n %c\n %d\n %X\n",
    > struttura.serie,struttura.organizzatore,struttura.partecipanti);
    > system("pause\n");
    >
    >
    > I don't got any warnings in my compiler.

    Then raise the warning level. It allowed you to store a pointer to char in
    a char.

    --
    Army1987 (Replace "NOSPAM" with "email")
     
    Army1987, Jan 23, 2008
    #15
  16. Richard Bos

    Guest

    On 23 Gen, 21:48, Army1987 <> wrote:
    > yqyq22 wrote:
    > > #include <stdio.h>

    >
    > >    struct tagstruttura{
    > >                        int serie;
    > >                        char organizzatore;
    > >                        int partecipanti;
    > >                      } struttura;

    >
    > > int main()
    > > {

    >
    > >    struttura.serie = 2;
    > >    struttura.organizzatore = "cazzo";

    >
    > What the f*** is being organized? Probably right that...>    struttura..partecipanti = 100;
    >
    > Wow, it'll be a somewhat big orgy...
    >
    > (For non-Italian speakers, look up "cazzo" somewhere...)
    >
    > >    printf("%d\n %c\n %d\n %X\n",
    > > struttura.serie,struttura.organizzatore,struttura.partecipanti);
    > >    system("pause\n");

    >
    > > I don't got any warnings in my compiler.

    >
    > Then raise the warning level. It allowed you to store a pointer to char in
    > a char.
    >
    > --
    > Army1987 (Replace "NOSPAM" with "email")


    THANKS TO ALL. now is clear
     
    , Jan 24, 2008
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2FuZHk=?=

    VB code and Sql Server Ansi Padding

    =?Utf-8?B?U2FuZHk=?=, May 11, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,477
    =?Utf-8?B?U2FuZHk=?=
    May 11, 2005
  2. Edwin Knoppert

    Ansi BSTR from dll, how to obtain in ASP.NET

    Edwin Knoppert, Dec 7, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    420
    Edwin Knoppert
    Dec 7, 2005
  3. Replies:
    1
    Views:
    491
  4. Replies:
    11
    Views:
    1,073
    Keith Thompson
    Apr 28, 2008
  5. Frank Iannarilli

    pre-ansi to ansi c++ conversion?

    Frank Iannarilli, Jul 21, 2009, in forum: C++
    Replies:
    2
    Views:
    416
Loading...

Share This Page