c programming

Discussion in 'C Programming' started by rachid elmaazouz, Feb 26, 2011.

  1. #include<stdio.h>
    #include<stdlib.h>
    #include<malloc.h>
    #include<string.h>
    void f(int);
    typedef struct date{
    int jour;
    int moi;
    int annee;
    }date;

    typedef struct bouquin{
    int ISBN;
    char *titre;
    char *auteur;
    char *editeur;
    date dateedition;
    }bouquin;
    main()
    {int n;
    printf("Remplissage:1\n");
    printf("Affichage:2\n");
    printf("Tri:3\n");
    printf("Recherche:4\n");
    printf("Miseajour:5\n");
    printf("saisir l'operation voulu:\n");
    scanf("%d",&n);
    f(n);
    system("cat.dd");
    }

    void f(int n)
    {
    int i;
    switch(n)
    case 1: {bouquin *livre;
    i=0;
    livre=(bouquin*) malloc(10*sizeof(bouquin));
    livre->ISBN=1;
    while( livre->ISBN>0)
    { livre->titre=malloc(10*sizeof(char));
    livre->auteur=malloc(10*sizeof(char));
    livre->editeur=malloc(10*sizeof(char));
    printf("ISBN:\n");
    scanf("%d",livre.ISBN);
    printf("titre:\n");
    scanf("%s",livre->titre);
    printf("auteur:\n");
    scanf("%s",livre->auteur);
    printf("editeur:\n");
    scanf("%s",livre->editeur);
    printf("date edition:\n");
    scanf("%d%d%d",livre->(dateedition.jour),livre-
    >(dateedition.moi),livre->(dateedition.annee));

    i++;
    }
    break;
    }

    }



    i compile this code and i have the following error :

    dd.c:45: error: request for member ‘ISBN’ in something not a structure
    or union
    dd.c:53: error: expected identifier before ‘(’ token




    any idea why??
     
    rachid elmaazouz, Feb 26, 2011
    #1
    1. Advertising

  2. rachid elmaazouz

    Eric Sosman Guest

    On 2/26/2011 2:11 PM, rachid elmaazouz wrote:
    > #include<stdio.h>
    > #include<stdlib.h>
    > #include<malloc.h>
    > #include<string.h>
    > void f(int);
    > typedef struct date{
    > int jour;
    > int moi;
    > int annee;
    > }date;
    >
    > typedef struct bouquin{
    > int ISBN;
    > char *titre;
    > char *auteur;
    > char *editeur;
    > date dateedition;
    > }bouquin;
    > main()
    > {int n;
    > printf("Remplissage:1\n");
    > printf("Affichage:2\n");
    > printf("Tri:3\n");
    > printf("Recherche:4\n");
    > printf("Miseajour:5\n");
    > printf("saisir l'operation voulu:\n");
    > scanf("%d",&n);
    > f(n);
    > system("cat.dd");
    > }
    >
    > void f(int n)
    > {
    > int i;
    > switch(n)
    > case 1: {bouquin *livre;
    > i=0;
    > livre=(bouquin*) malloc(10*sizeof(bouquin));
    > livre->ISBN=1;
    > while( livre->ISBN>0)
    > { livre->titre=malloc(10*sizeof(char));
    > livre->auteur=malloc(10*sizeof(char));
    > livre->editeur=malloc(10*sizeof(char));
    > printf("ISBN:\n");
    > scanf("%d",livre.ISBN);


    scanf("%d", &livre->ISBN);

    > printf("titre:\n");
    > scanf("%s",livre->titre);
    > printf("auteur:\n");
    > scanf("%s",livre->auteur);
    > printf("editeur:\n");
    > scanf("%s",livre->editeur);
    > printf("date edition:\n");
    > scanf("%d%d%d",livre->(dateedition.jour),livre-
    >> (dateedition.moi),livre->(dateedition.annee));


    scanf("%d%d%d", &livre->dateedeition.jour,
    &livre->dateedition.moi,
    &livre->dateedition.annee);

    > i++;
    > }
    > break;
    > }
    >
    > }
    >
    >
    >
    > i compile this code and i have the following error :
    >
    > dd.c:45: error: request for member ‘ISBN’ in something not a structure
    > or union
    > dd.c:53: error: expected identifier before ‘(’ token
    >
    >
    >
    >
    > any idea why??


    You can use `.ISBN' to designate one element of a struct (or
    union), but `livre' is not a struct: It is a pointer to a struct.
    Instead, use `(*livre).ISBN', or more briefly, `livre->ISBN'.

    The `->' operator must be preceded by a pointer to a struct
    (or union) and followed by the name of the desired element.
    `(dateedition.jour)' is not a name; `dateedition.jour' is.

    Finally, the "%d" conversion specifier for scanf() must match
    an argument that is a pointer to the `int' where the converted
    number will go, not the number itself. Hence, `&livre->ISBN'
    instead of just `livre->ISBN'.

    --
    Eric Sosman
    d
     
    Eric Sosman, Feb 26, 2011
    #2
    1. Advertising

  3. rachid elmaazouz <> writes:
    > #include<stdio.h>
    > #include<stdlib.h>
    > #include<malloc.h>
    > #include<string.h>
    > void f(int);
    > typedef struct date{
    > int jour;
    > int moi;
    > int annee;
    > }date;
    >
    > typedef struct bouquin{
    > int ISBN;
    > char *titre;
    > char *auteur;
    > char *editeur;
    > date dateedition;
    > }bouquin;
    > main()
    > {int n;
    > printf("Remplissage:1\n");
    > printf("Affichage:2\n");
    > printf("Tri:3\n");
    > printf("Recherche:4\n");
    > printf("Miseajour:5\n");
    > printf("saisir l'operation voulu:\n");
    > scanf("%d",&n);
    > f(n);
    > system("cat.dd");
    > }
    >
    > void f(int n)
    > {
    > int i;
    > switch(n)
    > case 1: {bouquin *livre;
    > i=0;
    > livre=(bouquin*) malloc(10*sizeof(bouquin));
    > livre->ISBN=1;
    > while( livre->ISBN>0)
    > { livre->titre=malloc(10*sizeof(char));
    > livre->auteur=malloc(10*sizeof(char));
    > livre->editeur=malloc(10*sizeof(char));
    > printf("ISBN:\n");
    > scanf("%d",livre.ISBN);
    > printf("titre:\n");
    > scanf("%s",livre->titre);
    > printf("auteur:\n");
    > scanf("%s",livre->auteur);
    > printf("editeur:\n");
    > scanf("%s",livre->editeur);
    > printf("date edition:\n");
    > scanf("%d%d%d",livre->(dateedition.jour),livre-
    >>(dateedition.moi),livre->(dateedition.annee));

    > i++;
    > }
    > break;
    > }
    >
    > }
    >
    >
    >
    > i compile this code and i have the following error :
    >
    > dd.c:45: error: request for member ‘ISBN’ in something not a structure
    > or union
    > dd.c:53: error: expected identifier before ‘(’ token


    livre is not a struct; it's a pointer to a struct. Take a look at line
    39; you refer to the ISBN member correctly there. Once you fix that,
    you'll find you're passing an int to scanf when it expects an int*; you
    need to pass the address of livre->ISBN, not the value.

    This statement:

    scanf("%d%d%d",livre->(dateedition.jour),livre-
    >(dateedition.moi),livre->(dateedition.annee));


    is quite a mess. As posted, one of the "->" tokens is split across two
    lines. The right operand of "->" is a member name; it can't be
    something in parentheses. And once you fix those problems, you're
    passing int values to scanf; you need to pass addresses of int objects.

    Oh, and you're not checking the return value of scanf. What happens
    if the user enters "foobar" rather than three integer values?

    Other things:

    "main()" should be "int main(void)".

    <malloc.h> is not a standard header; malloc() is declared in <stdlib.h>.

    Your code's readability would be improved by some additional whitespace.
    For example, I'd write "#include <stdio.h>" rather than
    "#include<stdio.h>", and I'd add a space after each comma. Your
    indentation is inconsistent. Consider running your code through a
    formatter ("indent -kr" isn't bad if you have it). Opinions vary
    on how code should be laid out, particularly regarding placement of
    "{" and "}", but it's generally agreed that consistency is important.

    Casting the result of malloc() can hide errors. For your first malloc
    call, I'd write:
    livre = malloc(10 * sizeof *livre);

    sizeof(char) is 1 by definition.

    Calling scanf() with an unqualified "%s" format is very dangerous. It
    will accept, and attempt to store, as many characters as the user types,
    with no overflow checking. If you read into a 10-byte buffer, and the
    user types 10 or more characters (remember the terminating '\0'), you'll
    clobber some other region of memory, with arbitrarily bad results.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 26, 2011
    #3
  4. On Sat, 26 Feb 2011 11:11:33 -0800 (PST), rachid elmaazouz
    <> wrote:

    >#include<stdio.h>
    >#include<stdlib.h>
    >#include<malloc.h>
    >#include<string.h>
    >void f(int);
    >typedef struct date{
    > int jour;
    > int moi;
    > int annee;
    > }date;
    >
    >typedef struct bouquin{
    > int ISBN;
    > char *titre;
    > char *auteur;
    > char *editeur;
    > date dateedition;
    > }bouquin;
    >main()
    >{int n;
    > printf("Remplissage:1\n");
    > printf("Affichage:2\n");
    > printf("Tri:3\n");
    > printf("Recherche:4\n");
    > printf("Miseajour:5\n");
    > printf("saisir l'operation voulu:\n");
    > scanf("%d",&n);
    > f(n);
    > system("cat.dd");
    >}
    >
    >void f(int n)
    >{
    > int i;
    >switch(n)
    >case 1: {bouquin *livre;


    In addition to previous comments:

    Since this function does not return a value, how do you propose to
    retain the value in livre so that subsequent calls to this function
    can examine the values.

    > i=0;
    > livre=(bouquin*) malloc(10*sizeof(bouquin));
    > livre->ISBN=1;
    > while( livre->ISBN>0)
    > { livre->titre=malloc(10*sizeof(char));
    > livre->auteur=malloc(10*sizeof(char));
    > livre->editeur=malloc(10*sizeof(char));


    Do you really think 10 characters is adequate for a typical title,
    author name, or editor name?

    > printf("ISBN:\n");
    > scanf("%d",livre.ISBN);


    Don't ISBNs have non-numeric characters?

    > printf("titre:\n");
    > scanf("%s",livre->titre);
    > printf("auteur:\n");
    > scanf("%s",livre->auteur);
    > printf("editeur:\n");
    > scanf("%s",livre->editeur);
    > printf("date edition:\n");
    > scanf("%d%d%d",livre->(dateedition.jour),livre-
    >>(dateedition.moi),livre->(dateedition.annee));

    > i++;
    > }
    > break;
    > }
    >
    >}
    >
    >
    >
    >i compile this code and i have the following error :
    >
    >dd.c:45: error: request for member ‘ISBN’ in something not a structure
    >or union
    >dd.c:53: error: expected identifier before ‘(’ token
    >
    >
    >
    >
    >any idea why??


    --
    Remove del for email
     
    Barry Schwarz, Feb 27, 2011
    #4
  5. Keith Thompson <> writes:
    [...]
    > Calling scanf() with an unqualified "%s" format is very dangerous. It
    > will accept, and attempt to store, as many characters as the user types,
    > with no overflow checking. If you read into a 10-byte buffer, and the
    > user types 10 or more characters (remember the terminating '\0'), you'll
    > clobber some other region of memory, with arbitrarily bad results.


    And I forgot to mention, scanf() with a "%s" or "%42s" format
    doesn't read a line of input; it ignores leading whitespace,
    then reads a whitespace-delimited word. A title could plausibly
    contain whitespace. You're better off using something that reads
    a whole line at a time. (Don't used gets()!)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 27, 2011
    #5
  6. rachid elmaazouz

    Hans Vlems Guest

    On 27 feb, 07:50, Barry Schwarz <> wrote:
    > On Sat, 26 Feb 2011 11:11:33 -0800 (PST), rachid elmaazouz
    >
    >
    >
    >
    >
    > <> wrote:
    > >#include<stdio.h>
    > >#include<stdlib.h>
    > >#include<malloc.h>
    > >#include<string.h>
    > >void f(int);
    > >typedef struct date{
    > >                 int jour;
    > >                 int moi;
    > >                 int annee;
    > >               }date;

    >
    > >typedef struct bouquin{
    > >                 int ISBN;
    > >                 char *titre;
    > >                 char *auteur;
    > >                 char *editeur;
    > >                 date dateedition;
    > >               }bouquin;
    > >main()
    > >{int n;
    > > printf("Remplissage:1\n");
    > > printf("Affichage:2\n");
    > > printf("Tri:3\n");
    > > printf("Recherche:4\n");
    > > printf("Miseajour:5\n");
    > > printf("saisir l'operation voulu:\n");
    > > scanf("%d",&n);
    > > f(n);
    > > system("cat.dd");
    > >}

    >
    > >void f(int n)
    > >{
    > > int i;
    > >switch(n)
    > >case 1: {bouquin *livre;

    >
    > In addition to previous comments:
    >
    > Since this function does not return a value, how do you propose to
    > retain the value in livre so that subsequent calls to this function
    > can examine the values.
    >
    > >         i=0;
    > >         livre=(bouquin*) malloc(10*sizeof(bouquin));
    > >         livre->ISBN=1;
    > >         while( livre->ISBN>0)
    > >           { livre->titre=malloc(10*sizeof(char));
    > >             livre->auteur=malloc(10*sizeof(char));
    > >             livre->editeur=malloc(10*sizeof(char));

    >
    > Do you really think 10 characters is adequate for a typical title,
    > author name, or editor name?
    >
    > >             printf("ISBN:\n");
    > >             scanf("%d",livre.ISBN);

    >
    > Don't ISBNs have non-numeric characters?
    >
    >
    >
    >
    >
    > >             printf("titre:\n");
    > >             scanf("%s",livre->titre);
    > >             printf("auteur:\n");
    > >             scanf("%s",livre->auteur);
    > >             printf("editeur:\n");
    > >             scanf("%s",livre->editeur);
    > >             printf("date edition:\n");
    > >             scanf("%d%d%d",livre->(dateedition.jour),livre-
    > >>(dateedition.moi),livre->(dateedition.annee));

    > >             i++;
    > >           }
    > >          break;
    > >         }

    >
    > >}

    >
    > >i compile this code and i have the following error :

    >
    > >dd.c:45: error: request for member ISBN in something not a structure
    > >or union
    > >dd.c:53: error: expected identifier before ( token

    >
    > >any idea why??

    >
    > --
    > Remove del for email- Tekst uit oorspronkelijk bericht niet weergeven -
    >
    > - Tekst uit oorspronkelijk bericht weergeven -- Tekst uit oorspronkelijk bericht niet weergeven -
    >
    > - Tekst uit oorspronkelijk bericht weergeven -


    Even worse, there are 13 character ISBN numbers as well (ref. King's
    'C programming').
    The fact that a character sequence only contains digits doesn't mean
    it isn't a string <g>
    Hans
     
    Hans Vlems, Feb 27, 2011
    #6
  7. rachid elmaazouz

    elisa morena Guest

    On 26 fév, 20:11, rachid elmaazouz <> wrote:
    > #include<stdio.h>
    > #include<stdlib.h>
    > #include<malloc.h>
    > #include<string.h>
    > void f(int);
    > typedef struct date{
    >                  int jour;
    >                  int moi;
    >                  int annee;
    >                }date;
    >
    > typedef struct bouquin{
    >                  int ISBN;
    >                  char *titre;
    >                  char *auteur;
    >                  char *editeur;
    >                  date dateedition;
    >                }bouquin;
    > main()
    > {int n;
    >  printf("Remplissage:1\n");
    >  printf("Affichage:2\n");
    >  printf("Tri:3\n");
    >  printf("Recherche:4\n");
    >  printf("Miseajour:5\n");
    >  printf("saisir l'operation voulu:\n");
    >  scanf("%d",&n);
    >  f(n);
    >  system("cat.dd");
    >
    > }
    >
    > void f(int n)
    > {
    >  int i;
    > switch(n)
    > case 1: {bouquin *livre;
    >          i=0;
    >          livre=(bouquin*) malloc(10*sizeof(bouquin));
    >          livre->ISBN=1;
    >          while( livre->ISBN>0)
    >            { livre->titre=malloc(10*sizeof(char));
    >              livre->auteur=malloc(10*sizeof(char));
    >              livre->editeur=malloc(10*sizeof(char));
    >              printf("ISBN:\n");
    >              scanf("%d",livre.ISBN);
    >              printf("titre:\n");
    >              scanf("%s",livre->titre);
    >              printf("auteur:\n");
    >              scanf("%s",livre->auteur);
    >              printf("editeur:\n");
    >              scanf("%s",livre->editeur);
    >              printf("date edition:\n");
    >              scanf("%d%d%d",livre->(dateedition.jour),livre->(dateedition.moi),livre->(dateedition.annee));
    >
    >              i++;
    >            }
    >           break;
    >          }
    >
    > }
    >
    > i compile this code and i have the following error :
    >
    > dd.c:45: error: request for member ‘ISBN’ in something not a structure
    > or union
    > dd.c:53: error: expected identifier before ‘(’ token
    >
    > any idea why??


    Bonjour.

    qq conseils rapides :

    Dans la fonction 'f' :

    1. toujours vérifier que le malloc ne renvoie pas NULL. En cas d'échec
    d'allocation, impossible d'utiliser 'livre'
    2. toujours vérifier le retour du scanf. Pour un scanf("%d",
    &variable), le scanf renvoie 1 si la saisie est correcte, 0 sinon.
    3. si le scanf renvoie 0, vider le buffer clavier par fflush(stdin);
    sous windows ou un while(getchar()!='\n'); sous unix.
    4. le scan attend toujours l'adresse d'une variable à mettre à jour
    => livre est un pointeur sur un entier => scanf ("%d", & (livre-
    >ISBN) );

    5. pour le scanf("%d%d%d",....) vérifier que le retour vaut 3 (il y a
    3 variables à saisir)
    6. la variable livre n'est utilisable que dans le case 1: et sera
    perdue dès le break;
    => pour sa réutilisation, déplacer sa déclaration avant le switch
    et ne pas oublier de faire un free(livre) pour libérer la mémoire.

    pedro.ch
     
    elisa morena, Feb 27, 2011
    #7
  8. elisa morena <> writes:
    <snip>
    > 2. toujours vérifier le retour du scanf. Pour un scanf("%d",
    > &variable), le scanf renvoie 1 si la saisie est correcte, 0 sinon.


    A return of EOF is also possible.

    > 3. si le scanf renvoie 0, vider le buffer clavier par fflush(stdin);
    > sous windows ou un while(getchar()!='\n'); sous unix.


    fflush(stdin) in undefined so an implementation is permitted to make it
    do what you describe[1], but why limit this simple program to one kind
    of system? Your alternative is also problematic: getchar() may never
    return '\n'. int c; while ((c = getchar()) != '\n' && c != EOF); is
    what you want.

    [1] stdin is not always connected to a keyboard, and the buffer that
    gets discarded is not certain to contain only the remainder of a line.

    <snip>
    --
    Ben.
     
    Ben Bacarisse, Feb 27, 2011
    #8
  9. rachid elmaazouz

    K4 Monk Guest

    On Feb 27, 12:11 am, rachid elmaazouz <>
    wrote:

    >              printf("ISBN:\n");
    >              scanf("%d",livre.ISBN);

    this line ^^^^^^^^^^^^^^^^^^^^^^^
     
    K4 Monk, Feb 28, 2011
    #9
    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. James Lapalme

    Attribute programming

    James Lapalme, Jul 7, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,285
    James Lapalme
    Jul 7, 2003
  2. Salisha Khan
    Replies:
    1
    Views:
    11,699
    JohnFol
    Aug 1, 2003
  3. Matt
    Replies:
    35
    Views:
    10,738
    George Neuner
    Jul 22, 2004
  4. Casey Hawthorne
    Replies:
    4
    Views:
    1,056
    Jarek Zgoda
    Aug 4, 2006
  5. Joe Mayo
    Replies:
    168
    Views:
    3,500
    David Thompson
    Oct 22, 2007
Loading...

Share This Page