我正在尝试使用main中的值来malloc结构。我一直在寻找方法,但找不到答案。我有3种类型的硬币,其价格我想放入 ret 。如何从结构中声明 ret ?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
typedef struct
{
double *ret;
}coin;
void ini(int a)
{
ret = (double*)malloc(a*sizeof(double));
}
int main(void){
long int a=250;
int n_coins=3;
coin *m = (coin*)malloc(n_coins*sizeof(coin));
ini(a);
m[0].ret[0] = 2000;
printf("%lf", m[0].ret[0]);
return 0;
}
答案 0 :(得分:2)
首先,return
是C中的保留keyword
,您不能将保留关键字用作变量名。
其次,如果要为其他函数中的任何数据类型的数组分配内存,则在函数中声明一个变量,调用malloc
,通过malloc分配所需的空间并返回第一个元素的地址如果你没有返回地址,被调用的函数(这里是main()
)将不知道分配的空间,并且它无法访问分配的内存空间。你可以这样做:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
typedef struct
{
double *var;
}moeda;
double *ini(int n)
{
double *arr;
arr = malloc(n*sizeof(*arr));
return arr;
}
int main(void){
long int a=250;
moeda m;
m.var=ini(a);
m.var[0] = 2000;
printf("%lf", m.var[0]);
return 0;
}
答案 1 :(得分:2)
如果我有你的代码并且必须改进它,我会去
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
// The kernel style guide https://www.kernel.org/doc/html/v4.10/process/coding-style.html discourages typedefs for structs
typedef struct moeda {
double *return_value;
} moeda;
// return a struct here:
moeda initialize_return(int a)
{
moeda ret;
ret.return_value = malloc(a*sizeof(double));
return ret;
}
int main(void) {
long int a=250;
moeda m = initialize_return(a);
m.return_value[0] = 2000;
printf("%lf", m.return_value[0]);
return 0;
}
(最好用英文标识所有标识符。)
这将是第一步。然后我可能会意识到结构并不是真正需要的并取代它:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
double * initialize_double_array(int a)
{
return malloc(a*sizeof(double));
}
int main(void) {
long int a=250;
double * arr = initialize_double_array(a);
arr[0] = 2000;
printf("%lf", arr[0]);
return 0;
}
OTOH,如果所述结构中还有其他字段,我可能会决定它们是否应该与该数组一起初始化。
一些变种:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
// The kernel style guide https://www.kernel.org/doc/html/v4.10/process/coding-style.html discourages typedefs for structs
struct moeda {
int num_values;
double *values;
};
// only fill a struct here:
// i. e. take a pre-initialized struct and work with it:
void moeda_alloc_values(struct moeda * data)
{
data->return_value = malloc(data->num_values * sizeof(double));
}
// return a struct here:
struct moeda initialize_moeda(int num)
{
struct moeda ret;
ret.num_values = num;
ret.return_value = malloc(num * sizeof(double));
// or just moeda_alloc_values(&ret);
return ret;
}
int main(void) {
long int a=250;
struct moeda m = initialize_return(a);
m.return_value[0] = 2000;
printf("%lf", m.return_value[0]);
struct moeda m2;
m2.num_values = 20;
moeda_alloc_values(&m2);
m2.return_value[0] = 2000;
printf("%lf", m2.return_value[0]);
return 0;
}
struct returns函数的优点是返回后你有一个“容易填充”的结构。
通过其上的指针修改结构的另一个函数的优点是它可以处理任何可能预先填充的,可能是malloced的结构,并且它可以在单个字段上工作,而不必考虑所有字段。
答案 2 :(得分:1)
返回是c中的关键字。您不能将其用作变量名称。 我也不清楚这个问题。 什么是&#34; moeda m;&#34;莫达在这儿?如果这不是英语C,我很抱歉。
答案 3 :(得分:1)
我假设您只是意味着您正在尝试从main中调用的函数为结构分配内存。我为了清楚起见更改了您的变量名称。 所以,首先,正如其他人所说,你不能使用return作为变量名。我还建议使用结构的大小而不仅仅是double,因为将来你可能在结构中有多个变量。
如果你想使用main中的函数,你必须传递一个指向函数的指针,然后使用malloc为它分配内存然后返回它。或者您可以将结构指针全局化为另一个选项。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "stdafx.h"
#include <malloc.h>
typedef struct
{
double number;
}example;
example *allocateMemory(int a, example *s)
{
s = (example*)malloc(a * sizeof(example));
return s;
}
int main() {
long int a = 250;
example *structure = NULL;
structure = allocateMemory(a, structure);
structure[0].number = 2000;
printf("%lf\n", structure[0].number);
//cleaning up memory
free(structure);
structure = NULL;
return 0;
}
只是其他一些注释,我让我的示例结构等于null,因为编译器抱怨未初始化的局部变量。
在你的代码中,你有这个。
m.retorno[0] = 2000;
但我假设你想访问struct数组中的第一个数字,所以应该是:
structure[0].number = 2000;
答案 4 :(得分:0)
对于这个具体的例子,我会做类似以下的事情:
is_split_point
答案 5 :(得分:0)
以下提议的代码:
注意:代码必须一致,可读并执行所需的功能
现在,建议的代码:
// for ease of readability and understanding:
// 1) insert a space:
// after commas,
// after semicolons,
// inside brackets,
// inside parens,
// around C operators
// 2) separate code blocks
// ( 'for' 'if' 'else' 'while' 'do...while' 'switch' 'case' 'default' )
// via a single blank line
// 3) variable (and parameter) names should indicate
// 'content' or 'usage' (or better, both)
#include <stdio.h> // printf(), perror()
#include <stdlib.h> // malloc(), free(), exit(), EXIT_FAILURE
// do not include header files those contents are not used
//#include <math.h>
//#include <string.h>
// added 'sCOIN' tag name to make it easier to use debugger
// since most debuggers use the tag name to reference fields inside a struct
typedef struct sCOIN
{
double *ret;
} coin;
int main( void )
{
// 'malloc()' expects its parameters to be of type 'size_t'
size_t n_coins=3;
coin mycoin;
// do not cast the returned value from 'malloc()', 'calloc()', 'realloc()'
// as the returned type is 'void*' which can be assigned to any pointer
//coin *m = (coin*)malloc(n_coins*sizeof(coin));
mycoin.ret = malloc( n_coins * sizeof( double ) );
// always check to assure the operation was successful
if( !mycoin.ret )
{
// 'perror()' outputs the enclosed text
// and the text of why the system thinks the error occurred
// to 'stderr'
perror( "malloc failed" );
exit( EXIT_FAILURE );
}
// implied else, malloc successful
// the field in 'coin' is declared a DOUBLE so assign a double
// not a integer. I.E include a decimal point '.'
mycoin.ret[ 0 ] = 2000.0;
printf( "%lf", mycoin.ret[ 0 ] );
// code should always clean up after itself
// I.E. don't leave a mess nor depend on the OS to cleanup.
free( mycoin.ret );
return 0;
}