[英]Structures and Functions [on hold]
bank* b1 = bank_constructor("BankA", 3);
In this part, program gives me an eror that is initialization makes pointer from integer without a cast.在这部分中,程序给了我一个错误,即初始化从 integer 生成指针而没有进行强制转换。 How can I solve this problem.
我怎么解决这个问题。
This is full part of my code;这是我的代码的完整部分;
#include<stdio.h>
#include<locale.h>
#include<windows.h>
#include<string.h>
typedef struct
{
char name;
int password;
int amoount;
int accyear;
}account;
typedef struct
{
char name;
int maxacc;
int curacc;
account** allacc;
}bank;
bank* bank_construtor(char *name, int maxacc);
int main()
{
bank* b1 = bank_constructor("BankA", 3);
}
bank* bank_construtor(char *name, int maxacc)
{
bank* ret = (bank*) calloc(1, sizeof(bank*));
strcpy(ret->name,*name);
strcpy(ret->name,maxacc);
return ret;
}
You have multiple problems with the bank_construtor
function. bank_construtor
function 存在多个问题。
You attempt to copy single characters with a function for copying strings:您尝试使用 function 复制单个字符以复制字符串:
strcpy(ret->name,*name);
ret->name
and *name
are both single characters and strcpy
copies strings ("arrays" of characters). ret->name
和*name
都是单个字符, strcpy
复制字符串(字符的“数组”)。
You can't use strcpy
to copy or create or append numbers to strings (or in your case single characters).您不能使用
strcpy
复制或创建 append 数字到字符串(或在您的情况下为单个字符)。 So the statement所以声明
strcpy(ret->name,maxacc);
doesn't make any sense.没有任何意义。
calloc(1, sizeof(bank*))
allocates memory for a pointer to a bank
structure, not enough memory for a full bank
structure itself. calloc(1, sizeof(bank*))
为指向bank
结构的指针分配 memory,对于完整的bank
结构本身来说 memory 不够。 You should use sizeof(bank)
.您应该使用
sizeof(bank)
。
You don't include <stdlib.h>
which properly declares the calloc
functions.您不包含正确声明
calloc
函数的<stdlib.h>
。 That means your call and the value returned could be invalid.这意味着您的调用和返回的值可能无效。 This is the reason one should not cast the return of
malloc
, calloc
or similar functions .这就是不应该
malloc
、 calloc
或类似函数的返回的原因。
You call the bank_constructor
but declare and define the bank_construtor
function.您调用
bank_constructor
,但声明和定义bank_construtor
function。 Not the same name function.不是同名 function。 This typo could be the one that leads to the error you ask about.
这个错字可能是导致您询问的错误的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.