Tableau de problèmes de pointeurs de structure (programmation C)

J’essaie donc de comprendre comment faire différentes choses et je n’ai pas beaucoup travaillé avec C, donc toute aide serait la bienvenue

typedef int data_t; typedef struct set { data_t *array; size_t capacity; size_t size; } set_t; typedef data_t* set_i_t; #define CLEAR -1 

J’ai eu cette méthode de travail qui utilise malloc et alloue de la mémoire:

 int set_init( set_t *set, int capacity ){ set->array = (data_t*)malloc(capacity * sizeof(data_t)); if(set->array == NULL){ return 1; } else{ set->capacity = capacity; set->size = 0; return 0; } } 

Et une méthode qui le libère:

 void set_free( set_t *set ){ free(set->array); set->array = NULL; set->capacity = set->size = 0; } 

Dans une méthode séparée, j’essaie de définir toutes les valeurs de l’ensemble sur -1 (CLEAR)

 void set_clear( set_t *set){ int i = 0; for (i = 0; i size; i++){ set->array = CLEAR; } set->size = 0; } 

Renvoyer la taille de l’ensemble:

 int set_size( set_t set ) { return sizeof(set->array); } 

Renvoyer la capacité:

 int set_capacity( set_t set ) { int capacity = set->capacity; return capacity; } 

Et puis imprimez le jeu:

 void set_print( set_t set ) { //Honestly don't feel like i'm ready for this one yet. } 

Si quelqu’un pouvait me guider à travers quelques-uns de ceux-ci ou me donner un peu d’aide sur la manière dont ceux-ci peuvent fonctionner, ce serait génial. Merci les gars!

Une bonne ressource est le tableau en croissance dynamic C

1


Vous pouvez lire sur size_t. Qu’est-ce que size_t en C?

 typedef int data_t; // Here you are redefining int to data_t this is then used in array. typedef struct set { data_t *array; // The address on heap where the typedef data_t is stored size_t capacity; size_t size; } set_t; typedef data_t* set_i_t; // not sure why this is here maybe you use somewhere else #define CLEAR -1 

2


set_free (set_t * set); Cela me semble correct.

set_init (); Oui mais non

 set_t set_init(int capacity) { // create it here then return it. set_t ret; ret.array = (data_t*)malloc(capacity * sizeof(data_t)); if (ret.array == NULL) return NULL; ret.capacity = capacity; ret.size = 0; return ret; } 

Dans la fonction appelante

 set_t A = set_init(5); if (A == NULL) fprintf(stderr, "could not alloc memory\n"); // :) 

3


 void set_clear( set_t *set){ // you pass the address of the struct into the function. you could also use set_i_t //int i = 0; // why do this you can do it in the for loop as you can see // for (i = 0; i < set->size; i++){ for (int i = 0; i < set->size; i++){ //set->array = CLEAR; common mistake // you are saying the address of the array. aka array[0] // this is the same as set->(array+i) set->array[i] = CLEAR; } set->size = 0; } 

4 et 5


 // looks good but again better ways of doing this. set_size( set_t set ); set_capacity( set_t set ); 

De meilleurs moyens de gérer la mémoire, comme dans l’exemple ci-dessous. C tableau en croissance dynamic

6

lire tout sur printf (); http://www.tutorialspoint.com/c_standard_library/c_function_printf.htm


 void set_print( set_t set ) { // Here you passed the struct in plain and simple no pointer...... // so you will use the '.' not the '->' // Here we can take a look at printf(); // %d is used to print int variables. // to start off you know you will have to loop through the array. for (int i = 0; i < set.size; i++) { // you know the array must be at least have one item in it. printf("%d\n", set.array[i]); // using printf print the data_t aka "int" item in the array } } 

J'espère que cela t'aides. g

Il y a eu quelques endroits où vous avez défini les arguments de la fonction avec set_t au lieu de set_t * .

Votre set_size renverrait simplement la taille du pointeur de array (c’est-à-dire toujours 4 ou 8), de sorte que nécessaire set->size

De plus, set_clear était incorrect [et ne comstackrait même pas].

J’ai ajouté quelques fonctions et mis en œuvre la fonction d’impression [redoutée :-)]. Pas de soucis …

Quoi qu’il en soit, voici le code corrigé [veuillez excuser le nettoyage de style gratuit]:

 #include  #include  typedef int data_t; typedef struct set { data_t *array; // pointer to set's data size_t capacity; // total number of data slots size_t size; // number of slots currently in use } set_t; typedef data_t *set_i_t; #define CLEAR -1 int set_init(set_t *set, int capacity) { set->array = (data_t *) malloc(capacity * sizeof(data_t)); if (set->array == NULL) { return 1; } else { set->capacity = capacity; set->size = 0; return 0; } } // And a method which frees it: void set_free(set_t *set) { free(set->array); set->array = NULL; set->capacity = set->size = 0; } // i'm trying to set all the values in the set to -1 (CLEAR) void set_clear(set_t *set) { int i = 0; for (i = 0; i < set->size; i++) { #if 0 set->array = CLEAR; #else set->array[i] = CLEAR; #endif } set->size = 0; } // Return the Size of the set: int set_size(set_t *set) { return set->size; } // Return the maximum capacity: int set_capacity_max(set_t *set) { int capacity = set->capacity; return capacity; } // Return the remaining available capacity: int set_capacity_avail(set_t *set) { int capacity = set->capacity - set->size; return capacity; } // add some data void set_append(set_t *set,int val) { // NOTES: // (1) this does _not_ check for overflow against capacity // (2) when out of capacity, we might increase capacity and do a realloc // on array #if 0 if ((set->size + 1) >= set->capacity) { set->capacity += 100; set->array = realloc(set->array,sizeof(data_t) * set->capacity); } #endif set->array[set->size++] = val; } // And then print the set: void set_print(set_t *set) { int i; int len; // Honestly don't feel like i'm ready for this one yet. // Relax, no worries ... len = 0; for (i = 0; i < set->size; i++) { len += printf(" %d",set->array[i]); if (len >= 72) { printf("\n"); len = 0; } } if (len > 0) printf("\n"); } int main(void) { set_t myset; set_init(&myset,100); set_append(&myset,17); set_append(&myset,23); set_append(&myset,37); set_print(&myset); set_free(&myset); return 0; }