结构指针数组(C编程)问题

结构指针数组(C编程)问题,c,arrays,pointers,struct,C,Arrays,Pointers,Struct,因此,我正在试图找出如何做一些不同的事情,我还没有与C的工作,所以任何帮助将不胜感激 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并分配内存的方法开始工作: int set_init( set_t *set, int

因此,我正在试图找出如何做一些不同的事情,我还没有与C的工作,所以任何帮助将不胜感激

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并分配内存的方法开始工作:

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;
}
}
以及一种释放它的方法:

void set_free( set_t *set ){

free(set->array);
set->array = NULL;
set->capacity = set->size = 0;

}
在另一个方法中,我尝试将集合中的所有值设置为-1(清除)

返回容量:

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.
}
如果有人能给我介绍一些,或者给我一点帮助,让我了解它们的工作原理,那就太棒了。谢谢大家

好的资源是

1


您可以阅读有关尺码的信息

2


设置为自由(设置为*设置);我觉得不错

set_init();是的,但不是

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


更好的内存管理方法,如这里的示例

6

阅读有关printf()的所有信息;


void set\u打印(set\u t set){
//在这里,您以简单明了的无指针方式传递了结构。。。。。。
//所以您将使用“.”而不是“->”
//这里我们可以看一看printf();
//%d用于打印整型变量。
//要开始,您知道必须在阵列中循环。
对于(int i=0;i


希望这有帮助。G

在一些地方,您使用
set\t
而不是
set\t*
定义函数参数

您的
set\u size
将只返回
数组
指针的大小(即始终为4或8),因此需要
set->size

而且,
set\u clear
不正确[甚至无法编译]

我添加了一些函数并实现了[Dread:-)]打印函数。不用担心

不管怎样,下面是更正后的代码[请原谅这种不必要的风格清理]:

#include <stdio.h>
#include <malloc.h>

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;
}
#包括
#包括
typedef int data_t;
typedef结构集{
data_t*array;//指向集合数据的指针
size\u t capacity;//数据插槽的总数
size\u t size;//当前正在使用的插槽数
}设置;;
类型定义数据集;
#定义清晰-1
int
set_init(set_t*set,int容量)
{
设置->数组=(数据)malloc(容量*sizeof(数据));
如果(设置->数组==NULL){
返回1;
}
否则{
设置->容量=容量;
设置->大小=0;
返回0;
}
}
//以及一种释放它的方法:
无效的
设置为自由(设置为*设置)
{
自由(设置->阵列);
set->array=NULL;
设置->容量=设置->大小=0;
}
//我正在尝试将集合中的所有值设置为-1(清除)
无效的
设置清除(设置*设置)
{
int i=0;
对于(i=0;isize;i++){
#如果0
设置->数组=清除;
#否则
设置->数组[i]=清除;
#恩迪夫
}
设置->大小=0;
}
//返回集合的大小:
int
设置大小(设置*设置)
{
返回设置->大小;
}
//返回最大容量:
int
设置容量最大值(设置*设置)
{
int容量=设置->容量;
返回能力;
}
//返回剩余的可用容量:
int
设置容量可用性(设置*set)
{
int capacity=set->capacity-set->size;
返回能力;
}
//添加一些数据
无效的
set_append(set_t*set,int val)
{
//注:
//(1)这不会检查容量是否溢出
//(2)当容量不足时,我们可能会增加容量并进行realloc
//阵列上
#如果0
如果((设置->大小+1)>=设置->容量){
设置->容量+=100;
设置->阵列=realloc(设置->阵列,大小(数据)*设置->容量);
}
#恩迪夫
设置->数组[设置->大小+]=val;
}
//然后打印集合:
无效的
设置打印(设置打印*设置)
{
int i;
内伦;
//老实说,我觉得我还没准备好。
//放松,别担心。。。
len=0;
对于(i=0;isize;i++){
len+=printf(“%d”,set->array[i]);
如果(len>=72){
printf(“\n”);
len=0;
}
}
如果(len>0)
printf(“\n”);
}
int
主(空)
{
设置myset;
set_init(&myset,100);
set_append(&myset,17);
set_append(&myset,23);
set_append(&myset,37);
设置打印(&myset);
设置_free(&myset);
返回0;
}

您的问题是什么?你需要帮助的是哪一部分?你有什么特别的困难?Stackoverflow不是一个教程网站,它最适合处理特定的问题。它是否编译(我可以看到错误,但不知道你已经做了多少)。请记住,在99%的情况下,警告是错误Set::array是指针而不是数组。如果希望动态调整大小,则必须为其分配一些空间。如果不清除,则应将其设置为固定大小的数组。如果要清除集合中的值,为什么只清除与
set->size
对应的值,而不是
set->capacity
?另外,
set\u clear()
不会清除
set->array
的元素,它会更改指针本身,这可能不是您想要的。此外,您的
set\u clear()
函数会重复覆盖指针,而不是它指向的数据。现在还不清楚你是想覆盖它还是释放它
set_size()
返回指针的大小,amd64上为8字节,i386上为4字节。这几乎肯定不是你想要的
set\u capacity()
很奇怪。这非常有用,你不知道。非常欢迎。我发现(以及其他运营商告诉我的)完全正确的代码是最好的教学方法。不管怎样,你一开始就走在正确的轨道上。顺便说一句,我忘了提到总是使用
-Wall-Werror
编译。这可以节省很多时间。我真的很感激你这么做
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");
// :)
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;    
}
// looks good but again better ways of doing this.
set_size( set_t set ); 
set_capacity( set_t set ); 
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
    }
}
#include <stdio.h>
#include <malloc.h>

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