|
|
|
|
Bonjour,
J'ai un soucis depuis ce matin en utilisant les structures. J'ai tout le temps une erreur de syntaxe en affectant une valeur dans une variable structurée.
J'ai une structure définie comme ceci:
struct db_integers{ // Octets
char *boolean; // 1 bit
char *octet; // 1
char *word; // 2
char *db_word; // 3
char *long_word; // 4
char *six_four_word; // 8
};
typedef struct db_integers integers;
integers mysql_integers; mysql_integers->boolean="BOOL"; mysql_integers->octet="TINYINT"; mysql_integers->word="SMALLINT"; mysql_integers->db_word="MEDIUMINT"; mysql_integers->long_word="INT"; mysql_integers->six_four_word="BIGINT";
mysql_types.h:4: error: erreur de syntaxe before '->' token
mysql_integers->boolean="BOOL";
Configuration: Gcc 3.3.5
Salut,
#include <stdio.h>
#include <stdlib.h>
main()
{
struct db_integers{ // Octets
char *boolean; // 1 bit
char *octet; // 1
};
struct db_integers *mysql_integers; // pointeur
mysql_integers=malloc(sizeof(struct db_integers));
mysql_integers->boolean="BOOL";
mysql_integers->octet="TINYINT";
printf("%s\n",mysql_integers->boolean);
printf("%s\n",mysql_integers->octet);
} |
Et avec typedef
#include <stdio.h>
#include <stdlib.h>
main()
{
struct db_integers{ // Octets
char *boolean; // 1 bit
char *octet; // 1
};
typedef struct db_integers integers;
integers *mysql_integers; //pointeur
mysql_integers=malloc(sizeof(integers));
mysql_integers->boolean="BOOL";
mysql_integers->octet="TINYINT";
printf("%s\n",mysql_integers->boolean);
printf("%s\n",mysql_integers->octet);
}
Et ça marche. J'ai ajouté malloc puisque à l'éxécution j'avais le message Erreur de segmentation |
En C, tu ne peux pas affecter des chaînes de caractères avec l'opérateur " = ". Tu es obligé de passer par une fonction de la bibliothèque string.h : strcpy(chaine_destinataire,chaine_source)
|
Ok, alors j'ai essayé vos conseils.
|
De même quand j'essaie la methode lami20j dans le fichier c principal mais avant le main() j'ai aussi des erreurs:
champollion.c:11: error: conflicting types for `mysql_integers' champollion.c:10: error: previous declaration of `mysql_integers' champollion.c:11: attention : data definition has no type or storage class champollion.c:13: error: erreur de syntaxe before '->' token Par contre dans le main() ça passe.... Ce langage va me rendre fou O_o |
Ah déjà pour l'histoire de strcpy() je comprends mieux :-)
|
Salut,
//a.c
#include <stdio.h>
#include <stdlib.h>
main()
{
test();
}
---------------------------------------------------------
//b.c
test()
{
struct db_integers
{ // Octets
char *boolean; // 1 bit
char *octet; // 1
};
typedef struct db_integers integers;
integers *mysql_integers;
mysql_integers=malloc(sizeof(integers));
mysql_integers->boolean="BOOL";
mysql_integers->octet="TINYINT";
printf("%s\n",mysql_integers->boolean);
printf("%s\n",mysql_integers->octet);
}
après je l'ai compilé gcc -c a.c gcc -c b.c gcc -o ab a.o b.o |
Salut Lami20j,
|
J'ai en quelque sortes résolu le problème..
|
Pour la première version de son programme ca ne marchait pas car il utilisait des -> au lieu d'utiliser l'opérateur '.' :
typedef struct db_integers integers; integers mysql_integers; mysql_integers.boolean="BOOL"; 1) L'opérateur . permet d'accéder au champ d'une structure. 2) L'opérateur * permet d'accéder au contenu d'une zone pointe (*add_mystructj équivaut à mystruct) 3) Rq : (son opérateur inverse est & : &mystruct équivaut à add_mystrcut). 4) L'opérateur -> est une combinaison des deux opérateurs '*' et '.' . (*add_mystruct).champ équivaut add_struct->champ Simple non ? |
Ok ok, merci à tous....
struct machin{
int a;
double b;
};
struct machin var_machin;
var_machin.a=3;
int main()
{
}
Mais ça, ça passe:
struct machin{
int a;
double b;
};
struct machin var_machin;
void initialize_var_machin(void)
{
var_machin.a=3;
}
int main()
{
initialize_var_machin();
}
Pour le premier j'ai cette erreur: essai.c:8: error: erreur de syntaxe before '.' token Bizzare vous ne trouvez pas? |
Mouais c'est vrai que c'est bizarre. En même temps les variables globales c'est mal... Et à la limite faire une fonction qui initialise ta structure c'est une bonne chose :
#ifndef MYSTRUCT #define MYSTUCT typedef struct machin * pstruct; pstruct new_struct(); void set_a(pstruct s,int val); void set_b(pstruct s,double vall); void del_struct(pstruct s); #endif mystruct.c #include "mystruct.h"
struct machin{
int a;
double b;
};
//....
etc... |
C'est vrai, c'est pas mal comme ça.
|
Bonsoir kilian et mamiemando,
#include <stdio.h>
struct machin{
int a;
};
struct machin var_machin;
main()
{
var_machin.a=3;
printf("%d\n",var_machin.a);
}
|
Re,
#include <stdio.h>
struct machin{
int a;
char nom[10];
};
/*affectation globale */
struct machin var_machin ={ 3, "kilian" };
main()
{
printf("%d\n",var_machin.a);
printf("%s\n",var_machin.nom);
}
|