结构体指针(C编程)问题的阵 [英] Array of Struct Pointers (C Programming) Issues
问题描述
所以我想弄清楚如何做一些不同的事情,我没有用C多,所以任何帮助将非常AP preciated工作。
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
我已经得到它使用malloc和分配内存,这种方法工作:
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;
}
在一个单独的方法我试图集合中的所有的值设置为-1(CLEAR)
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);
}
返回的能力:
int set_capacity( set_t set ) {
int capacity = set->capacity;
return capacity;
}
然后打印设置:
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!
推荐答案
一个很好的资源是Ç动态成长阵列
1
您可以阅读有关为size_t。 什么是用C为size_t?
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_free( set_t *set); Looks good to me.
set_init();是的,但没有
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;
}
在调用函数
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安培; 5
// looks good but again better ways of doing this.
set_size( set_t set );
set_capacity( set_t set );
内存管理的更好的方法,如在这里的例子。 ç动态增长的数组
6
阅读所有有关的printf();
http://www.tutorialspoint.com/c_standard_library/c_function_printf.htm
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\n", set.array[i]);
// using printf print the data_t aka "int" item in the array
}
}
希望这有助于。摹
Hope this helps. G
这篇关于结构体指针(C编程)问题的阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!