English 中文(简体)
如何在C中为民族解放军设置一个指导点?
原标题:How to set a pointer to struct to NULL in C?
  • 时间:2011-11-03 14:40:03
  •  标签:
  • c
  • null

我有一阵.,出于我印刷这一阵列时的任何原因,在最后有一个零点,从而导致该守则最终逐字印出民族解放军。

我是否可以删除最后的记忆层?

For example:

typedef struct
{
    char *name;
} B;

typedef struct
{
    B *var;
} A;

int main() {
    int num = 5; //for example
    A *foo = malloc(sizeof(A));
    B *bar = malloc(num * sizeof(B));
    for (int i = 0; i < num; i++) {
        bar[i] = *create_b(&bar[i]); // some function that works.
    }
    foo->var = bar;
    while (foo->var != NULL) {
        printf("This is %s
",foo->var->name);
            foo->var++; 
    }
}

一切物品都只印制了罚款,但休息时间结束时不想要印刷。 类似:

This is A
This is B
This is C
This is D
This is F
This is

显然,该阵列只有5个元素,最后1个没有印刷。

最佳回答

你的印刷机是:

foo->var = bar;
while (foo->var != NULL) {
    printf("This is %s
",foo->var->name);
    foo->var++; 
}

foo->var将永远不会等于NUL,因为你只是重新点名,因此,你最终将读到<条码>-阵列的过去,而你的申请很可能坠毁。

If you replace the while loop with for (int i = 0; i < num; i++), it will print the correct number of elements.

问题回答

您可以做以下工作:foo->var++/code>,因为在设定为NUL的阵列中没有位置。 此外,使用<代码>++/代码>的改动foo->var,在路程<代码>foo->var在阵列开始时不再有点,你不能再次进入阵列。

你们需要为一些末期标识分配记忆,就像插图一样,照相机的特性>,以标志座标的结尾。

如下:

int main() {
    int num = 5; //for example
    A *foo = malloc(sizeof(A));
    B *bar = malloc((num + 1) * sizeof(B));  // +1 for array terminator
    for (int i = 0; i < num; i++) {
        bar[i] = *create_b(&bar[i]); // some function that works.
    }
    bar[i].name = NULL;  // Use this as a marker to mean end of array
    foo->var = bar;
    for (B *tmp = foo->var; tmp->name != NULL; tmp++) {
        printf("This is %s
",tmp->name);
    }
}

<><>Edit>/strong> 守则中有一些错误。

Your 问题很可能在职能中产生,而不是你所言。

Edit:no, that sorry, 或许是错误的。

但是,这肯定是你想要的:

bar[i] = *create_b(&bar[i]);

你们在酒吧的地址都穿过,并且把它等同于收益价值点到什么?





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签