Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 为什么即使我没有';我不能改变它吗?_C - Fatal编程技术网

C 为什么即使我没有';我不能改变它吗?

C 为什么即使我没有';我不能改变它吗?,c,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数组,但是当我运行它时,第五个元素的值会发生变化,即使我没有改变它

这是我的节目。它接受一个论点

#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);
}

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

首先,您没有检查是否有程序参数。这样做应该是例行公事

if (argc < 2)
    { /*error*/ }
除此之外,您已经从
malloc()
(不必要)中强制转换了返回值,但没有检查返回值(必要)


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

addr=(int*)malloc(sizeof(int))应为
addr=malloc(5*sizeof*addr)。。。或者按照@chux的建议,使用指定给的类型的大小。
if (argc < 2)
    { /*error*/ }
#define ELEMS 5
addr = malloc(ELEMS * sizeof(int));
if (addr == NULL)
    { /*error*/ }