c programming

R

rachid elmaazouz

#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??
 
E

Eric Sosman

#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-

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'.
 
K

Keith Thompson

rachid elmaazouz said:
#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-
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.
 
B

Barry Schwarz

#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?
 
K

Keith Thompson

Keith Thompson said:
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()!)
 
H

Hans Vlems

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.


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


Don't ISBNs have non-numeric characters?








--
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
 
E

elisa morena

#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-
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
 
B

Ben Bacarisse

elisa morena said:
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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top