2017-03-25 35 views
0

例如:A1 = 12345,A2 = 222 我想使用memcpy使A1 = 12322輸入INT爲A1和A2,然後使用memcpy切換A1和A2之間的一些數量,但不能因此

我知道它與內存字節有關,但顯然我不完全明白內存是如何工作的...

#include <stdio.h> 
#include <stdlib.h> 
#include <memory.h> 
int main() { 
    size_t n; 
    printf("enter how many int\n"); 
    while(scanf("%d",&n) != EOF){ 
     int *A1 = (int *)malloc(sizeof(int)*n); 
     int *A2 = (int *)malloc(sizeof(int)*n); 

     printf("enter A1 number\n"); 
     scanf("%d",A1); 

     printf("enter A2 number\n"); 
     scanf("%d",A2); 

     memcpy(A1+3,A2+2,sizeof(int)); 
     printf("A1= %d\n",*A1); 

    free(A1); 
    free(A2); 
    } 
} 
+0

你永遠不會初始化'N'。 – ForceBru

+0

'int'將所有數字存儲在同一個變量中,並以二進制格式存儲。您不能通過建立索引來索引或複製十進制數字。 –

+0

我不知道你是如何從'12345'和'222'獲得'12322'的。 –

回答

0

這樣的事情最好用字符串來代替整數。

我冒昧地將自己的邏輯略微化了一下,例如:我剝去了循環。 (保持簡單第一,當效果很好:去)

#include <stdio.h> 
#include <stdlib.h> 
#include <errno.h> 
#include <string.h> 
#include <limits.h> 
//#include <memory.h> 

int main(void) 
{ 
    size_t n; 
    int err; 
    char format[16] = {'\0'}; 

    // check the bitsize to evaluate how much memory we need 
    // for teh numebr of decimal digits in an int 
    n = sizeof(int) * CHAR_BIT; 
    // 2^32 = 4294967296 which are 10 characters plus EOS 
    if (n == 32) { 
    n = 11; 
    } 
    // 2^64 = 18446744073709551616 which are 20 characters plus EOS 
    else if (n == 64) { 
    n = 21; 
    } 
    // I wanted to keep it simple, so fail at <32 bit integers 
    else { 
    fprintf(stderr, "Unknown int-size %zu, please check\n", n); 
    return EXIT_FAILURE; 
    } 

    // we need to limit the maximum length for scanf() to n -1. This 
    // is not possible to do directly we need to build the format string manually 
    err = snprintf(format, sizeof(format), "%%%zus", n - 1); 
    if (err < 0) { 
    fprintf(stderr, "snprintf() failed wile preparing the format for scanf\n"); 
    return EXIT_FAILURE; 
    } 

    // we need byte arrays, not integer arrays, hence 'char'. 
    // sizeof(char) is always 1 (one) by definition. 
    // BTW: don't cast malloc() in C (but do it in C++) 

    // calloc() sets the memory to 0 which is quite useful here 
    char *A1 = calloc(n, 1); 
    if (A1 == NULL) { 
    fprintf(stderr, "Failure to allocate %zu bytes for A1\n", n); 
    return EXIT_FAILURE; 
    } 
    char *A2 = calloc(n, 1); 
    if (A2 == NULL) { 
    fprintf(stderr, "Failure to allocate %zu bytes for A2\n", n); 
    return EXIT_FAILURE; 
    } 

    printf("enter A1 number\n"); 
    // check return of scanf() (well, always check the returns of every 
    // function (with the accepted excepteion of printf()) 
    errno = 0; 
    err = scanf(format, A1); 
    if (err != 1) { 
    if (err == EOF) { 
     fprintf(stderr, "The error %s occured while scanning for A1\n", 
       strerror(errno)); 
    } else { 
     fprintf(stderr, "An error occured while scanning for A1\n"); 
    } 
    return EXIT_FAILURE; 
    } 

    printf("enter A2 number\n"); 
    err = scanf(format, A2); 
    errno = 0; 
    if (err != 1) { 
    if (err == EOF) { 
     fprintf(stderr, "The error %s occured while scanning for A1\n", 
       strerror(errno)); 
    } else { 
     fprintf(stderr, "An error occured while scanning for A1\n"); 
    } 
    return EXIT_FAILURE; 
    } 
    // it is memcpy(destination, source, number of bytes to be copied) 
    // Here we copy 2 bytes from A2 to A1 with some offsets, please adjust to your needs 

    // You will get into trouble if you don't check if your offsets are inside 
    // the allocated memories! 
    memcpy(A1 + 2, A2 + 1, 2); 

    printf("A1= %s\n", A1); 

    // if you want a binary number from it do something in the line of e.g.: 
    int binary_number = atoi(A1); 
    printf("A1 as an int = %d\n", binary_number); 

    free(A1); 
    free(A2); 

    return EXIT_SUCCESS; 
} 
+0

嘿〜感謝您的代碼,但我從一開始就一直運行stderr味精,不管我輸入什麼數字,它只是不斷顯示stderr味精。 –

+0

btw,有沒有簡單的方法來解決我的代碼? 我想通過使用malloc來保持int *,也許將A1和A2轉換爲char以便它們轉換爲memcpy? –

+0

@LingLingLing哦? C&P錯字?不,如預期的那樣按照輸入1234和222工作。你會得到哪些消息?不,如果你想(需要)使用'memcpy()',沒有簡單的方法來修復你的代碼。如果你想保持int指針,你可以用'int'作一個字符串,例如''nnnnnf()',然後用例如'atoi()'返回。 – deamentiaemundi

相關問題