Les membres ayant 30 points peuvent parler sur les canaux annonces, projets et hs du chat.
La shoutbox n'est pas chargée par défaut pour des raisons de performances. Cliquez pour charger.

Forum Casio - Autres questions


Index du Forum » Autres questions » listes en c (gint)
Pedrobzh Hors ligne Membre Points: 616 Défis: 0 Message

listes en c (gint)

Posté le 10/11/2020 07:37

bonjour je voudrais utiliser les listes, mais j'ai un doute, il faut utiliser malloc()
int game_map[50][6;]



Kbd2 Hors ligne Membre Points: 269 Défis: 0 Message

Citer : Posté le 10/11/2020 08:51 | #


There are two main ways to use arrays in C

The first is to allocate the array on the stack - this is useful for smaller arrays. The size of the array must be known at compile time, through constants or literals. The declaration is <type> <name>[<size>][<size2>][<size3>] ... for arrays of any dimension.
They can be initialised through a literal upon declaration (in which case the size can be omitted), e.g. int arr[] = {0, 1, 2, 3, 4}; or char arr[2][3] = { {'a', 'b', 'c'}, {'d', 'e', 'f'} };.
They can also have each element set individually, in which case the initial data will be garbage, e.g.
int arr[5];
for(int i = 0; i < 5; i++)
{
    arr[i] = i;
};

The second is to allocate memory on the heap for an array. This is the better way to do it for large arrays or arrays where the size is not known at compilation time. The declaration is <type> *<name> = malloc(<size> * <size2> * <size3> ... * sizeof(<type>)) for arrays of any dimension.
One-dimensional arrays of this type can be accessed as normal, e.g.
#include <stdlib.h>
char *arr = malloc(10 * sizeof(char));
arr[5] = 'A';
free(arr);

The downside of using the heap to store an array (or indeed passing an array to any function, which will decay it into a pointer to that array) is that the array is always one-dimensional, therefore multi-dimensional arrays must be manually accessed, e.g.
#include <stdlib.h>
int height = 5, width = 6;
int *arr = malloc(height * width * sizeof(int));
for(int y = 0; y < height; y++)
{
    for(int x = 0; x < width; x++)
    {
        arr[y * width + x] = x * y; // Note the use of y * width + x
    }
}
free(arr);

You must also manually free allocated arrays when you are finished with them, through free(<name>).

These are the basics of the main types of array, there are nuances with more complex arrays such as arrays of pointers to allocated objects, but for basic types this should cover you.

(These resources are in English, feel free to translate or find some in French )
https://www.tutorialspoint.com/cprogramming/c_arrays.htm
https://www.embedded.com/allocating-arrays/
Lephenixnoir En ligne Administrateur Points: 24145 Défis: 170 Message

Citer : Posté le 10/11/2020 09:11 | #


The size of the array must be known at runtime, through constants or literals.

La taille devrait vraiment être fixe, les tableaux de taille variable (VLA) sont des mauvaises idées dans quasiment toutes les situations. Linux les a notablement complètement éradiqués de son code source. Sur la calculatrice le tas est un peu buggé donc ça pourrait se discuter, mais dans le fond de l'histoire il n'y a pas de bonne raison.
Mon graphe (24 Mars): (gint#27 ; (Rogue Life || HH2) ; PythonExtra ; serial gint ; Boson X ; ...) || (shoutbox v5 ; v5)
Pedrobzh Hors ligne Membre Points: 616 Défis: 0 Message

Citer : Posté le 10/11/2020 13:17 | #


donc je suis obligé d'utiliser malloc? ou je peu utiliser ma technique?
pensés suicidaires
   90%
Lephenixnoir En ligne Administrateur Points: 24145 Défis: 170 Message

Citer : Posté le 10/11/2020 13:58 | #


On ne comprend rien à ce que tu veux faire. D'une part les listes ça n'existe pas en C.

D'autre part tu n'expliques absolument rien donc on doit tout deviner et y'a plein de possibilitées :
• Peut-être que tu parles de tableau multi-dimensionnels ?
• Peut-être que tu parles de liste au sens Python où on peut rajouter des éléments ?
• Peut-être que tu veux manipuler un tableau comme une variable de première classe ?

C'est simple : ton topic ne contient pas de question. Il y a une remarque écrite à l'arrache et une ligne de code écrite à l'arrache, ce qui ne constitue pas une question sérieuse. Explique ce que tu veux obtenir, comment tu penses le faire avec le langage, ce que tu as testé, et les problèmes que tu as rencontrés avec ces tests. C'est toujours la même formule. >_>
Mon graphe (24 Mars): (gint#27 ; (Rogue Life || HH2) ; PythonExtra ; serial gint ; Boson X ; ...) || (shoutbox v5 ; v5)

LienAjouter une imageAjouter une vidéoAjouter un lien vers un profilAjouter du codeCiterAjouter un spoiler(texte affichable/masquable par un clic)Ajouter une barre de progressionItaliqueGrasSoulignéAfficher du texte barréCentréJustifiéPlus petitPlus grandPlus de smileys !
Cliquez pour épingler Cliquez pour détacher Cliquez pour fermer
Alignement de l'image: Redimensionnement de l'image (en pixel):
Afficher la liste des membres
:bow: :cool: :good: :love: ^^
:omg: :fusil: :aie: :argh: :mdr:
:boulet2: :thx: :champ: :whistle: :bounce:
valider
 :)  ;)  :D  :p
 :lol:  8)  :(  :@
 0_0  :oops:  :grr:  :E
 :O  :sry:  :mmm:  :waza:
 :'(  :here:  ^^  >:)

Σ π θ ± α β γ δ Δ σ λ
Veuillez donner la réponse en chiffre
Vous devez activer le Javascript dans votre navigateur pour pouvoir valider ce formulaire.

Si vous n'avez pas volontairement désactivé cette fonctionnalité de votre navigateur, il s'agit probablement d'un bug : contactez l'équipe de Planète Casio.

Planète Casio v4.3 © créé par Neuronix et Muelsaco 2004 - 2024 | Il y a 92 connectés | Nous contacter | Qui sommes-nous ? | Licences et remerciements

Planète Casio est un site communautaire non affilié à Casio. Toute reproduction de Planète Casio, même partielle, est interdite.
Les programmes et autres publications présentes sur Planète Casio restent la propriété de leurs auteurs et peuvent être soumis à des licences ou copyrights.
CASIO est une marque déposée par CASIO Computer Co., Ltd