即使我没有改变,为什么int的值会发生变化?

时间:2015-03-23 20:35:17

标签: c

我正在尝试将值保存到字符串中的int数组中,但是当我运行它时,即使我不更改它,第五个元素的值也会更改。

这是我的计划。它接受一个论点。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(int argc, char **argv)
{
    char *mainDest = argv[1];
    char *busy;
    int a = 0;
    int d = 0;
    int *addr;
    //int addr[5];
    int i = 0;
    int b = 0;
    int c = 0;
    addr = (int *) malloc(sizeof(int));
    busy = (char *) malloc(2);
    while(mainDest[a] != '\0')
        {
            if(mainDest[a] != ':')
            {
                printf("%c\n", mainDest[a]);
                busy[b] = mainDest[a];
                b++;
            }
            if(mainDest[a] == ':')
            {
                if(isdigit(busy[0]) == 1){
                    printf("converting to integer %s\n", busy);
                    printf("i is %d\n", i);
                    addr[i] = atoi(busy);
                    printf("address is %d\n", addr[i]);
                    printf("5th address is %d\n", addr[4]);
                    i++;
                    b = 0;
                }
                else
                {
                    printf("5th address is %d\n", addr[4]);
                    addr[i] = 0;
                    i++;
                    b = 0;
                }
                if(mainDest[a] == ':' && mainDest[a+1] == ':' )
                {
                    printf("a is %d\n", a);
                    printf("i is %d\n", i);
                    addr[i] = 0;
                    i++;
                    a++;
                }
            }
            a++;
        }
        if(isdigit(busy[0]) == 1)
        {
            printf("converting to integer %s\n", busy);
            printf("i is %d\n", i);
            addr[i] = atoi(busy);
            i++;
            b = 0;
        }
        else
        {
            addr[i] = 0;
            i++;
            b = 0;
        }
        printf("address is %d\n", addr[i]);
        printf("5th address is %d\n", addr[4]);
        printf("how many i's: %d\n", i);
        printf("After while loop.\n");
    while(c != i+1)
    {
        printf("%d\n", addr[c]);
        c++;
    } 
    free(addr);
}

我从终端运行 - ./a.out 8:7:6:5:4:3:2:1

8
converting to integer 8
i is 0
address is 8
5th address is 56
7
converting to integer 7
i is 1
address is 7
5th address is 55
6
converting to integer 6
i is 2
address is 6
5th address is 54
5
converting to integer 5
i is 3
address is 5
5th address is 53
4
converting to integer 4
i is 4
address is 4
5th address is 4
3
converting to integer 3
i is 5
address is 3
5th address is 51
2
converting to integer 2
i is 6
address is 2
5th address is 50
1
converting to integer 1
i is 7
address is 0
5th address is 49
how many i's: 8
After while loop.
8
7
6
5
49
3
2
1
0

正如你所看到的,我总是打印出第五个元素的值,即使我没有存储任何东西来看它为什么会改变。结果是每次调用它都会改变。任何建议都有帮助,谢谢。

1 个答案:

答案 0 :(得分:2)

首先,您尚未检查是否存在程序参数。这样做应该是常规的。

if (argc < 2)
    { /*error*/ }

您没有为int[]数组分配足够的内存。我建议使用#define表示元素数量或const int

#define ELEMS 5

除此之外,您已经从malloc()(不必要)投放了返回值,但没有检查返回值(必要)。

addr = malloc(ELEMS * sizeof(int));
if (addr == NULL)
    { /*error*/ }

在这两种情况下,添加代码与其可能导致的悲伤相比很简单。