Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

So i'm trying to figure out how to do a few different things and I haven't worked with C that much, so any help would be much appreciated.

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

I have gotten this method working which uses malloc and allocates memory:

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;

}

In a separate method 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++){
        set->array = CLEAR;
    }
    set->size = 0;

}

Return the Size of the set:

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

Return the capacity:

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

And then print the set:

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

If anyone could walk me through a couple of these or give me a little assistance on how these can work, that would be awesome. Thanks guys!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
129 views
Welcome To Ask or Share your Answers For Others

1 Answer

A good resource is C dynamically growing array

1


You can read about size_t. What is size_t in 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); Looks good to me.

set_init(); yes but no

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;
}

In the calling function

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

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 & 5


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

Better ways of managing memory such as in the example here. C dynamically growing array

6

read all about 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
", set.array[i]);
        // using printf print the data_t aka "int" item in the array
    }
}

Hope this helps. G


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...