关于C循环的问题
我不太确定为什么这没有返回应有的结果,也许你们中的一个人可以帮助我。我在 C 中有以下 for 循环:
for (i=0; i<nrow; i++) {
dat[k]=l.0;
k++;
}
现在,您可能会认为这会将 dat
的所有值(其中有 nrow
值)设置为 1.0;相反,它被设置为 0。程序编译良好,一切顺利。内存已正确分配,并且 dat
被定义为 double
。
这有产生 0 的原因吗?我猜测 0 来自 dat
变量的初始化(因为我使用 calloc
进行内存分配,据说它将变量初始化为 0(但并非总是如此)) 。
编辑:请注意,有一个特定的原因(这很重要)我没有将其定义为 dat[i]
。此外。 k
被定义为一个整数并被初始化为 0。
编辑 2:下面是整个代码:
#include "stdio.h"
#include "stdlib.h"
#define NCH 81
// Generate swap-mode data for bonds for input.conf file
int main()
{
int i,j,k;
int **dat2;
double *dat;
int ns = 500;
int nrow = NCH*(ns-1);
dat = (double*) calloc(nrow, sizeof(double));
dat2 = (int**) calloc(nrow,sizeof(int*));
/*for (i=0; i<nrow; i++) {
dat2[i] = (int*) calloc(2, sizeof(int));
for (j=0; j<2; j++)
dat2[i][j] = 0;
}*/
k=0;
printf("\nBreakpoint\n");
/*for (i=0; i<81; i++) {
for (j=0; j<250; j++) {
dat[k] = j+1;
k++;
}
for (j=251; j>1; j++) {
dat[k] = j-1;
k++;
}
}*/
FILE *inp;
inp = fopen("input.out", "w");
for (i=0; i<nrow; i++) {
dat[k]=1.0;
k++;
}
//fprintf(inp, "%lf\n", dat[i]);
printf("%d", dat[nrow]);
printf("\nDone\n");
fclose(inp);
return 0;
}
谢谢! 阿米特
I'm not exactly sure why this isn't returning what it should be, perhaps one of you could help me out. I have the following for loop in C:
for (i=0; i<nrow; i++) {
dat[k]=l.0;
k++;
}
Now, you would think that this would set all values of dat
(of which there are nrow
values) to 1.0; Instead, it is being set to 0. The program compiles fine and everything goes smoothly. The memory is properly allocated and dat
is defined as a double
.
Is there a reason this is yielding 0? I'm guessing the 0 is coming from the initialization of the dat
variable (since I used calloc
for memory allocation, which supposedly initializes variables to 0 (but not always)).
EDIT: Please note that there is a specific reason (this is important) that I'm not defining it as dat[i]
. Additionally. k
was defined as an integer and was initialized to 0.
EDIT 2: Below is the entire code:
#include "stdio.h"
#include "stdlib.h"
#define NCH 81
// Generate swap-mode data for bonds for input.conf file
int main()
{
int i,j,k;
int **dat2;
double *dat;
int ns = 500;
int nrow = NCH*(ns-1);
dat = (double*) calloc(nrow, sizeof(double));
dat2 = (int**) calloc(nrow,sizeof(int*));
/*for (i=0; i<nrow; i++) {
dat2[i] = (int*) calloc(2, sizeof(int));
for (j=0; j<2; j++)
dat2[i][j] = 0;
}*/
k=0;
printf("\nBreakpoint\n");
/*for (i=0; i<81; i++) {
for (j=0; j<250; j++) {
dat[k] = j+1;
k++;
}
for (j=251; j>1; j++) {
dat[k] = j-1;
k++;
}
}*/
FILE *inp;
inp = fopen("input.out", "w");
for (i=0; i<nrow; i++) {
dat[k]=1.0;
k++;
}
//fprintf(inp, "%lf\n", dat[i]);
printf("%d", dat[nrow]);
printf("\nDone\n");
fclose(inp);
return 0;
}
Thanks!
Amit
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
无效,因为 dat 的第 n 行元素不存在。
is not valid, because the nrow'th element of dat doesn't exist.
您确定“k”从零开始吗?
在您发布的示例中,您使用的是 l 而不是 1 - 这只是一个拼写错误吗?
Are you sure you are starting 'k' at zero?
In the sample you posted you are using l not 1 - is this just a typo?
那应该有效。
That should work.
有两件事:
我假设 l.0 是一种类型,而你的 l 实际上是 1。
其次,为什么你在 for 循环中使用 k 而不是使用 i?尝试使用这个代替:
Two things:
I'm assuming the l.0 is a type and your l is actually a 1.
Secondly, why are you using k in your for loop instead of using i? Try using this instead:
要么这有效,要么你的编译器/硬件有问题。
Either this works your compiler/hardware is bugged.
当您
printf("%d", dat[nrow])
时,dat[nrow]
尚未设置为 1。在 for 循环中,条件为我< nrow
所以它在它之前。您需要i <= nrow
。when you
printf("%d", dat[nrow])
,dat[nrow]
has not been set to 1. In the for loop the condition isi < nrow
so it's before it. You needi <= nrow
.