Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql C-将数组作为参数传递并更改大小和内容_Mysql_C_Arrays_Pointers - Fatal编程技术网

Mysql C-将数组作为参数传递并更改大小和内容

Mysql C-将数组作为参数传递并更改大小和内容,mysql,c,arrays,pointers,Mysql,C,Arrays,Pointers,更新:我解决了问题(向下滚动) 我正在编写一个小型C程序,我想做以下工作: 这个程序连接到一个mysql数据库(工作非常好),我想对数据库中的数据做些什么。每个查询大约有20-25行,我创建了自己的结构,它应该包含来自查询每一行的信息 因此,我的结构如下所示: typedef struct { int timestamp; double rate; char* market; char* currency; } Rate; int main(int argc,

更新:我解决了问题(向下滚动)


我正在编写一个小型C程序,我想做以下工作:

这个程序连接到一个mysql数据库(工作非常好),我想对数据库中的数据做些什么。每个查询大约有20-25行,我创建了自己的结构,它应该包含来自查询每一行的信息

因此,我的结构如下所示:

typedef struct {
    int timestamp;
    double rate;
    char* market;
    char* currency;
} Rate;
int main(int argc, char **argv)
{
    Rate *rates = ?; // don't know how to initialize it
    (void) do_something_with_rates(&rates);

    // the size here should be ~20
    printf("size of rates: %d", sizeof(rates)/sizeof(Rate));
}
我想将一个空数组传递给函数,函数应该根据查询返回的行数计算数组的大小。例如,一个SQL查询返回20行,因此数组应该包含20个my
Rate
struct的对象

我想要这样的东西:

typedef struct {
    int timestamp;
    double rate;
    char* market;
    char* currency;
} Rate;
int main(int argc, char **argv)
{
    Rate *rates = ?; // don't know how to initialize it
    (void) do_something_with_rates(&rates);

    // the size here should be ~20
    printf("size of rates: %d", sizeof(rates)/sizeof(Rate));
}
函数
的速率(Rate**rates)
必须是什么样子


编辑:我按照Alex说的做了,我让我的函数以
size\u t
的形式返回数组的大小,并以
Rate**rates
的形式将数组传递给函数


在函数中,您可以访问和更改诸如
(*rates)[i]之类的值。例如,timestamp=123

在C中,内存是动态或静态分配的

类似于
整数50\u的数字[50]
是静态分配的。不管怎样,大小都是50个整数,因此编译器知道数组的大小(以字节为单位)
sizeof(五十个数字)
将在此处为您提供200字节

动态分配:
int*bunch\u of\u number=malloc(sizeof(int)*可变大小)
。正如您所看到的,
variable_size
不是常数,因此编译器无法在不执行程序的情况下计算出数组的大小
sizeof(一串数字)
在32位系统上为您提供4个字节,在64位系统上为您提供8个字节。只有程序员知道数组有多大。在您的例子中,是谁编写了
使用速率做某事()
,但您要么不返回该信息,要么接受一个大小参数,这就是在丢弃该信息

现在还不清楚
do\u something\u with\u rates()
是如何准确声明的,但是类似于:
void do\u something\u with\u rates(Rate**rates)
这样的函数不知道
rates
有多大。我建议这样做:
void do\u something\u与\u rates(大小数组大小、速率**速率)
。无论如何,按照你的要求,它离工作还有很长的路要走。可能的解决办法如下:

您需要返回新阵列的大小:

size_t do_something_with_rates(size_t old_array_size, Rate **rates) {
    Rate **new_rates;
    *new_rates = malloc(sizeof(Rate) * n); // allocate n Rate objects

    // carry out your operation on new_rates

    // modifying rates
    free(*rates); // releasing the memory taken up by the old array
    *rates = *new_rates // make it point to the new array

    return n; // returning the new size so that the caller knows
}

int main() {
    Rate *rates = malloc(sizeof(Rate) * 20);
    size_t new_size = do_something_with_rates(20, &rates);
    // now new_size holds the size of the new array, which may or may not be 20

    return 0;
}
或传入要设置的函数的大小参数:

void do_something_with_rates(size_t old_array_size, size_t *new_array_size, Rate **rates) {
    Rate **new_rates;
    *new_rates = malloc(sizeof(Rate) * n); // allocate n Rate objects
    *new_array_size = n; // setting the new size so that the caller knows

    // carry out your operation on new_rates

    // modifying rates
    free(*rates); // releasing the memory taken up by the old array
    *rates = *new_rates // make it point to the new array
}

int main() {
    Rate *rates = malloc(sizeof(Rate) * 20);
    size_t new_size;
    do_something_with_rates(20, &new_size, &rates);
    // now new_size holds the size of the new array, which may or may not be 20

    return 0;
}

为什么需要将旧尺寸作为参数传递?

void do_something_with_rates(Rate **rates) {
    // You don't know what n is. How would you
    // know how many rate objects the caller wants
    // you to process for any given call to this?
    for (size_t i = 0; i < n; ++i)
        // carry out your operation on new_rates
}
void do\u something\u与费率(费率**费率){
//你不知道n是什么,你会怎么做
//知道调用方需要多少速率对象
//您是否需要处理对此的任何给定调用?
对于(尺寸i=0;i
当您有一个尺寸参数时,一切都会发生变化:

void do_something_with_rates(size_t size, Rate **rates) {
    for (size_t i = 0; i < size; ++i) // Now you know when to stop
        // carry out your operation on new_rates
}
void do\u something\u与费率(大小、费率**费率){
for(size\u ti=0;i
这是您的程序的一个非常基本的缺陷

我还想让函数更改数组的内容

size_t do_something_with_rates(size_t old_array_size, Rate **rates) {
    Rate **new_rates;
    *new_rates = malloc(sizeof(Rate) * n); // allocate n Rate objects

    // carry out some operation on new_rates
    Rate *array = *new_rates;
    for (size_t i = 0; i < n; ++i) {
        array[i]->timestamp = time();
        // you can see the pattern
    }

    return n; // returning the new size so that the caller knows
}
size\u t doo\u something\u与\u rates(size\u t old\u array\u size,Rate**rates){
费率**新费率;
*new_rates=malloc(sizeof(Rate)*n);//分配n个Rate对象
//对新的利率进行一些操作
速率*数组=*新的_速率;
对于(尺寸i=0;itimestamp=time();
//你可以看到图案
}
return n;//返回新的大小以便调用者知道
}

在C语言中,内存可以是动态分配的,也可以是静态分配的

类似于
整数50\u的数字[50]
是静态分配的。不管怎样,大小都是50个整数,因此编译器知道数组的大小(以字节为单位)
sizeof(五十个数字)
将在此处为您提供200字节

动态分配:
int*bunch\u of\u number=malloc(sizeof(int)*可变大小)
。正如您所看到的,
variable_size
不是常数,因此编译器无法在不执行程序的情况下计算出数组的大小
sizeof(一串数字)
在32位系统上为您提供4个字节,在64位系统上为您提供8个字节。只有程序员知道数组有多大。在您的例子中,是谁编写了
使用速率做某事()
,但您要么不返回该信息,要么接受一个大小参数,这就是在丢弃该信息

现在还不清楚
do\u something\u with\u rates()
是如何准确声明的,但是类似于:
void do\u something\u with\u rates(Rate**rates)
这样的函数不知道
rates
有多大。我建议这样做:
void do\u something\u与\u rates(大小数组大小、速率**速率)
。无论如何,按照你的要求,它离工作还有很长的路要走。可能的解决办法如下:

您需要返回新阵列的大小:

size_t do_something_with_rates(size_t old_array_size, Rate **rates) {
    Rate **new_rates;
    *new_rates = malloc(sizeof(Rate) * n); // allocate n Rate objects

    // carry out your operation on new_rates

    // modifying rates
    free(*rates); // releasing the memory taken up by the old array
    *rates = *new_rates // make it point to the new array

    return n; // returning the new size so that the caller knows
}

int main() {
    Rate *rates = malloc(sizeof(Rate) * 20);
    size_t new_size = do_something_with_rates(20, &rates);
    // now new_size holds the size of the new array, which may or may not be 20

    return 0;
}
或传入要设置的函数的大小参数:

void do_something_with_rates(size_t old_array_size, size_t *new_array_size, Rate **rates) {
    Rate **new_rates;
    *new_rates = malloc(sizeof(Rate) * n); // allocate n Rate objects
    *new_array_size = n; // setting the new size so that the caller knows

    // carry out your operation on new_rates

    // modifying rates
    free(*rates); // releasing the memory taken up by the old array
    *rates = *new_rates // make it point to the new array
}

int main() {
    Rate *rates = malloc(sizeof(Rate) * 20);
    size_t new_size;
    do_something_with_rates(20, &new_size, &rates);
    // now new_size holds the size of the new array, which may or may not be 20

    return 0;
}

为什么需要将旧尺寸作为参数传递?

void do_something_with_rates(Rate **rates) {
    // You don't know what n is. How would you
    // know how many rate objects the caller wants
    // you to process for any given call to this?
    for (size_t i = 0; i < n; ++i)
        // carry out your operation on new_rates
}
void do\u something\u与费率(费率**费率){
//你不知道n是什么,你会怎么做
//知道调用方需要多少速率对象
//您是否需要处理对此的任何给定调用?
对于(尺寸i=0;i
当您有一个尺寸参数时,一切都会发生变化:

void do_something_with_rates(size_t size, Rate **rates) {
    for (size_t i = 0; i < size; ++i) // Now you know when to stop
        // carry out your operation on new_rates
}
void do\u something\u与费率(大小、费率**费率){
for(size\u ti=0;i
这是一个非常基本的fla