English 中文(简体)
动态内存位置 C - 参数内注 *
原标题:Dynamic memory location C - argument int *
  • 时间:2012-05-26 21:17:56
  •  标签:
  • c
  • memory

我有一个代码:

void Read(int T[], int len) {
    int i;
    T = (int*) malloc(len * sizeof *T);
    for (i=0;i<len;i++) {
        scanf("%d", &T[i]);
    }
}

我用这种方式:

int *T;
Read(T,len);

接下来,我想写我的桌子:

void Write(int T[], int len) {
    int i;
    for(i=0;i<len;i++) {
        printf("%d, ", T[i]);
    }
    printf("
");
    return;
}

使用它:

Write(T,len);   

它给了我错误的结果。 我几乎可以肯定这个问题与“ 强/ 强/ 强” 有关, 但我无法解决这个问题 。

提前感谢

最佳回答

其中一个问题可能是您在 Read 函数中重新修改本地变量 T :

int *T;
Read(T, len);
// ...

void Read(int T[], int len) {
    int i;
    T = (int*) malloc(len * sizeof *T);
    // ...
}

T Read 中的 是真实 T 变量的复制件。 您为副本指定了新值, 但原件保持不变 。

要实际修改外部 T , 请将一个指针切换到外部 :

int *T;
Read(&T, len);
// ...

void Read(int **T, int len) {
    int i;
    *T = (int*) malloc(len * sizeof *T);
    for (i=0;i<len;i++) {
        scanf("%d", &(*T)[i]);
    }
}
问题回答

尝试以下功能( 函数 < code> write 未改变) :

void Read(int T[], int len) {
    int i;
    for (i=0;i<len;i++) {
        scanf("%d", &T[i]);
    }
}

T = (int*) malloc(len * sizeof *T);
Read(T,len);

Write(T,len);   

<强度 > UPDATE : 但如果您想要 read 来分配内存, 它必须接收指针的指针 :

void Read(int *T[], int len) {
    int i;
    T = (int**) malloc(len * sizeof **T)

    for (i=0;i<len;i++) {
        scanf("%d", &(*T)[i]); /* not the easiest expression in the language */
    }        
}

Read(&T, len);
Write(T, len);

实际上,修改 Read 直接返回指针比较容易,但也许这不是你想要的: int *Read(int)len {{__/code>。

内特的解决方案是好的 和花花公子 但我发现它有点隐约。

C本身是相当模糊的 因为它是,我不认为有什么理由 使它 这么多两倍。 我建议你使用以下:

将您的 lese read(int T[], int len) 的签名更改为 int* read(int T[], int len)

这样做有多重优势。 它不仅使您的代码更容易读取, 而且还消除了下流读数所带有的“ 隐藏” 副作用( 部分), 并明确指出了您的意图, 这样您就不会那么容易忘记拨打 < code> free () , 仅仅因为您看到一个指针被退回, 从而提示函数中可能存在一种隐蔽的动态分配 。 另外, 您需要担心的间接因素更少 。

然后,您可以做以下工作:

int* Read(int T[], int len) {
    int i;
    T = (int*) malloc(len * sizeof(T));
    for (i=0; i<len; ++i) {
        scanf("%d", &T[i]);
    }
    return T; // Basically the only change!
}

现在,在调用您的 Read () 的函数中:

int main() {

const int size = 5;
int *T = 0; // It s not needed to do a prior declaration and initialization
            // but it does silence a warning which is a good thing.
T = Read(T, size);
Write(T, size);

// Do more stuff and free() whenever needed

return 0;
}




相关问题
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 ...