-5
#include<stdio.h>
#include<string.h>

#define print(x){ \
                while(*x != -1){ \
                  printf("\n %d \n",*x); \
                  x++; \
                } 

void int_copy(int* ptrA, int* ptrB, int nbr)
{
    memcpy(ptrA,ptrB,nbr);
}

int main()
{
    int stringa[40] = {100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, -1};
    int stringb[40];

    memset(stringb, 0, sizeof(stringb));

    int *ptr;
    int *ptr1;
    int len = 0;

    ptr = stringa;
    ptr1 = stringb;

    while(*ptr != -1)
    {
        *ptr++;
        len++;
    }

    printf("\n len : %d \n",len);

    int_copy(stringa, stringb, len);

    print(ptr1)

    return 0;
}

我在尝试执行它时出错。

4

3 回答 3

3

除了其他人向您指出的问题之外,我建议您计算宏中的左大括号和右大括号的数量。

#define print(x){ \
            while(*x != -1){ \
              printf("\n %d \n",*x); \
              x++; \
            } 
于 2013-07-11T14:53:56.063 回答
2

当您调用 时int_copy, 的值为len0。

此外,其中的代码int_copy已损坏,无法缩放要复制的字符数。

它应该是:

void int_copy(int* ptrA, const int* ptrB, int nbr)
{
  memcpy(ptrA, ptrB, nbr * sizeof *ptrB);
}

由于memcpy()复制chars,您必须按比例放大int.

于 2013-07-11T14:29:15.463 回答
1

你的问题是

void int_copy(int* ptrA,int* ptrB,int nbr){
     memcpy(ptrA,ptrB,nbr);
}

您确定lenints的数量stringa并将其传递为nbr。但是其中memcpy()被解释为要复制的缓冲区长度(以字节为单位),因此,只有第一部分stringa被复制为stringb不包括“-1”。改成memcpy(ptrA,ptrB,nbr*sizeof(int))

于 2013-07-11T14:32:47.917 回答