C 使用函数和;分配
我在使用函数使程序的第二个calloc正确读取文件时遇到问题。要调用的函数是C 使用函数和;分配,c,function,calloc,C,Function,Calloc,我在使用函数使程序的第二个calloc正确读取文件时遇到问题。要调用的函数是colAlloc(variables) 我的代码如下: void readSpaces (FILE * ptrF, char fileN [], double ** m, int * r) { ptrF = fopen (fileN, "r"); char s; int i; int ctrS = 0; int c; int cVal; for (s = get
colAlloc(variables)
我的代码如下:
void readSpaces (FILE * ptrF, char fileN [], double ** m, int * r)
{
ptrF = fopen (fileN, "r");
char s;
int i;
int ctrS = 0;
int c;
int cVal;
for (s = getc (ptrF); s != EOF; s = getc (ptrF))
{
if (s == ' ' || s == '\t' || s == '\t')
{
++ctrS;
}
}
cVal = ctrS / * r;
c = cVal;
colAlloc (ptrF, fileN, m, &r, &cVal);
/**something is not working here so the program is giving a run-time error once it needs to read column**/
fclose (ptrF);
}
//allocate memory for column
void colAlloc (FILE * ptrF, char fileN [], double ** m, int ** r, int ** s) //file pointer, file name, matrix, row, spaces;
{
int i;
int c;
c = & s;
for (i = 0; i < * r; i ++ )
{
m [i] = (double *) calloc (c, sizeof (double));
if (m [i] == NULL)
{
printf ("\nSorry, not enough memory!\n\n");
exit (0);
}
}
printf ("Cols >> %d.\n\n", c);
for (i = 0; i < * r; i ++)
{
free (m [i]);
}
}
void readSpaces(FILE*ptrF,char fileN[],double**m,int*r)
{
ptrF=fopen(文件“r”);
字符s;
int i;
int ctrS=0;
INTC;
int cVal;
对于(s=getc(ptrF);s!=EOF;s=getc(ptrF))
{
如果(s=''| | s='\t'| | s=='\t')
{
++CTR;
}
}
cVal=ctrS/*r;
c=cVal;
colAlloc(ptrF、文件、m、r和cVal);
/**这里有些东西不工作,所以程序在需要读取列时会给出运行时错误**/
fclose(ptrF);
}
//为列分配内存
void colAlloc(FILE*ptrF,char fileN[],double**m,int**r,int**s)//文件指针,文件名,矩阵,行,空格;
{
int i;
INTC;
c=&s;
对于(i=0;i<*r;i++)
{
m[i]=(双*)calloc(c,sizeof(双));
如果(m[i]==NULL)
{
printf(“\n内存不足!\n\n”);
出口(0);
}
}
printf(“Cols>>%d.\n\n”,c);
对于(i=0;i<*r;i++)
{
自由(m[i]);
}
}
当我调用readSpaces(ptrF,fileN,m,r)
函数中的函数时,程序就崩溃了。我认为我调用函数是错误的,并且混淆了指针的使用和对适当变量的引用调用
如果能帮上点忙,我将不胜感激
感谢(i=0;i
r是int**
,因此i
将int(i
)与int*
(*r
)进行比较。换句话说,您正在与一个地址进行比较,而该地址不是您想要的地址。for(i=0;i<*r;i++)
r是
int**
,因此i
将int(i
)与int*
(*r
)进行比较。换句话说,你是在和一个地址进行比较,而这个地址不是你想要的代码>==>ints代码>初学者的错误是假定字符类型为char
。请阅读您使用的函数的手册页,查看要使用的函数类型,getc
的函数类型是int
代码>==>ints代码>初学者的错误是假定字符类型为char
。请阅读您使用的函数的手册页,查看getc
要使用的类型是int
。到目前为止,我能够在没有警告消息的情况下编译程序,但是我仍然收到运行时错误。我的错误现在很可能出现在函数colAlloc(…)
中。有什么建议吗?到目前为止,我能够在没有警告消息的情况下编译程序,但是我仍然得到一个运行时错误。我的错误现在很可能出现在函数colAlloc(…)
中。有什么建议吗?
for (i = 0; i < * r; i ++ )