关于C循环的问题

发布于 2024-10-13 07:26:56 字数 1539 浏览 4 评论 0原文

我不太确定为什么这没有返回应有的结果,也许你们中的一个人可以帮助我。我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

私野 2024-10-20 07:26:56
printf("%d", dat[nrow]);

无效,因为 dat 的第 n 行元素不存在。

printf("%d", dat[nrow]);

is not valid, because the nrow'th element of dat doesn't exist.

悲欢浪云 2024-10-20 07:26:56

您确定“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?

吐个泡泡 2024-10-20 07:26:56
for (i=0; i<nrow; i++) {
    dat[k]=1.0;
    k++;
}

那应该有效。

for (i=0; i<nrow; i++) {
    dat[k]=1.0;
    k++;
}

That should work.

可遇━不可求 2024-10-20 07:26:56

有两件事:

我假设 l.0 是一种类型,而你的 l 实际上是 1。

其次,为什么你在 for 循环中使用 k 而不是使用 i?尝试使用这个代替:

for (i=0; i<nrow; i++) {
    dat[i]=1.0;
}

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:

for (i=0; i<nrow; i++) {
    dat[i]=1.0;
}
魂归处 2024-10-20 07:26:56

要么这有效,要么你的编译器/硬件有问题。

k = 0;

for (i=0; i < nrow; i++) {
    dat[k++] = 1.0f;
}

printf("%d, %d", dat[0], dat[nrow - 1]);

Either this works your compiler/hardware is bugged.

k = 0;

for (i=0; i < nrow; i++) {
    dat[k++] = 1.0f;
}

printf("%d, %d", dat[0], dat[nrow - 1]);
倾城泪 2024-10-20 07:26:56

当您 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 is i < nrow so it's before it. You need i <= nrow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文